Quake II RTX doxygen  1.0 dev
utils.c File Reference
#include "shared/shared.h"
#include "common/utils.h"

Go to the source code of this file.

Functions

static qboolean match_raw (int c1, int c2, qboolean ignorecase)
 
static qboolean match_char (int c1, int c2, qboolean ignorecase)
 
static qboolean match_part (const char *filter, const char *string, size_t len, qboolean ignorecase)
 
static const char * match_filter (const char *filter, const char *string, size_t len, qboolean ignorecase)
 
qboolean Com_WildCmpEx (const char *filter, const char *string, int term, qboolean ignorecase)
 
color_index_t Com_ParseColor (const char *s, color_index_t last)
 
void Com_PlayerToEntityState (const player_state_t *ps, entity_state_t *es)
 
unsigned Com_HashString (const char *s, unsigned size)
 
unsigned Com_HashStringLen (const char *s, size_t len, unsigned size)
 
void Com_PageInMemory (void *buffer, size_t size)
 
size_t Com_FormatTime (char *buffer, size_t size, time_t t)
 
size_t Com_FormatTimeLong (char *buffer, size_t size, time_t t)
 
size_t Com_TimeDiff (char *buffer, size_t size, time_t *p, time_t now)
 
size_t Com_TimeDiffLong (char *buffer, size_t size, time_t *p, time_t now)
 
size_t Com_FormatSize (char *dest, size_t destsize, off_t bytes)
 
size_t Com_FormatSizeLong (char *dest, size_t destsize, off_t bytes)
 

Variables

const char *const colorNames [10]
 
int paged_total
 

Function Documentation

◆ Com_FormatSize()

size_t Com_FormatSize ( char *  dest,
size_t  destsize,
off_t  bytes 
)

Definition at line 465 of file utils.c.

466 {
467  if (bytes >= 10000000) {
468  return Q_scnprintf(dest, destsize, "%dM", (int)(bytes / 1000000));
469  }
470  if (bytes >= 1000000) {
471  return Q_scnprintf(dest, destsize, "%.1fM", (float)bytes / 1000000);
472  }
473  if (bytes >= 1000) {
474  return Q_scnprintf(dest, destsize, "%dK", (int)(bytes / 1000));
475  }
476  if (bytes >= 0) {
477  return Q_scnprintf(dest, destsize, "%d", (int)bytes);
478  }
479  return Q_scnprintf(dest, destsize, "???");
480 }

Referenced by BuildName(), and list_recordings().

◆ Com_FormatSizeLong()

size_t Com_FormatSizeLong ( char *  dest,
size_t  destsize,
off_t  bytes 
)

Definition at line 482 of file utils.c.

483 {
484  if (bytes >= 10000000) {
485  return Q_scnprintf(dest, destsize, "%d MB", (int)(bytes / 1000000));
486  }
487  if (bytes >= 1000000) {
488  return Q_scnprintf(dest, destsize, "%.1f MB", (float)bytes / 1000000);
489  }
490  if (bytes >= 1000) {
491  return Q_scnprintf(dest, destsize, "%d kB", (int)(bytes / 1000));
492  }
493  if (bytes >= 0) {
494  return Q_scnprintf(dest, destsize, "%d byte%s",
495  (int)bytes, bytes == 1 ? "" : "s");
496  }
497  return Q_scnprintf(dest, destsize, "unknown size");
498 }

Referenced by BuildList(), finish_download(), and format_demo_size().

◆ Com_FormatTime()

size_t Com_FormatTime ( char *  buffer,
size_t  size,
time_t  t 
)

Definition at line 391 of file utils.c.

392 {
393  int sec, min, hour, day;
394 
395  min = t / 60; sec = t % 60;
396  hour = min / 60; min %= 60;
397  day = hour / 24; hour %= 24;
398 
399  if (day) {
400  return Q_scnprintf(buffer, size, "%d+%d:%02d.%02d", day, hour, min, sec);
401  }
402  if (hour) {
403  return Q_scnprintf(buffer, size, "%d:%02d.%02d", hour, min, sec);
404  }
405  return Q_scnprintf(buffer, size, "%02d.%02d", min, sec);
406 }

Referenced by Com_TimeDiff(), and NET_Stats_f().

◆ Com_FormatTimeLong()

size_t Com_FormatTimeLong ( char *  buffer,
size_t  size,
time_t  t 
)

Definition at line 408 of file utils.c.

409 {
410  int sec, min, hour, day;
411  size_t len;
412 
413  if (!t) {
414  return Q_scnprintf(buffer, size, "0 secs");
415  }
416 
417  min = t / 60; sec = t % 60;
418  hour = min / 60; min %= 60;
419  day = hour / 24; hour %= 24;
420 
421  len = 0;
422 
423  if (day) {
424  len += Q_scnprintf(buffer + len, size - len,
425  "%d day%s%s", day, day == 1 ? "" : "s", (hour || min || sec) ? ", " : "");
426  }
427  if (hour) {
428  len += Q_scnprintf(buffer + len, size - len,
429  "%d hour%s%s", hour, hour == 1 ? "" : "s", (min || sec) ? ", " : "");
430  }
431  if (min) {
432  len += Q_scnprintf(buffer + len, size - len,
433  "%d min%s%s", min, min == 1 ? "" : "s", sec ? ", " : "");
434  }
435  if (sec) {
436  len += Q_scnprintf(buffer + len, size - len,
437  "%d sec%s", sec, sec == 1 ? "" : "s");
438  }
439 
440  return len;
441 }

Referenced by AC_Retry(), Com_TimeDiffLong(), and gtv_drop().

◆ Com_HashString()

unsigned Com_HashString ( const char *  s,
unsigned  size 
)

Definition at line 339 of file utils.c.

340 {
341  unsigned hash, c;
342 
343  hash = 0;
344  while (*s) {
345  c = *s++;
346  hash = 127 * hash + c;
347  }
348 
349  hash = (hash >> 20) ^(hash >> 10) ^ hash;
350  return hash & (size - 1);
351 }

Referenced by Cmd_AddMacro(), Cmd_AliasFind(), Cmd_AliasSet(), Cmd_Complete_f(), Cmd_Find(), Cmd_FindMacro(), Cmd_RegCommand(), Cvar_FindVar(), and Cvar_Get().

◆ Com_HashStringLen()

unsigned Com_HashStringLen ( const char *  s,
size_t  len,
unsigned  size 
)

Definition at line 361 of file utils.c.

362 {
363  unsigned hash, c;
364 
365  hash = 0;
366  while (*s && len--) {
367  c = Q_tolower(*s++);
368  hash = 127 * hash + c;
369  }
370 
371  hash = (hash >> 20) ^(hash >> 10) ^ hash;
372  return hash & (size - 1);
373 }

◆ Com_PageInMemory()

void Com_PageInMemory ( void buffer,
size_t  size 
)

Definition at line 383 of file utils.c.

384 {
385  int i;
386 
387  for (i = size - 1; i > 0; i -= 4096)
388  paged_total += ((byte *)buffer)[i];
389 }

Referenced by BSP_Load(), IMG_FreeUnused(), MOD_FreeUnused(), and S_EndRegistration().

◆ Com_ParseColor()

color_index_t Com_ParseColor ( const char *  s,
color_index_t  last 
)

Definition at line 204 of file utils.c.

205 {
206  color_index_t i;
207 
208  if (COM_IsUint(s)) {
209  i = strtoul(s, NULL, 10);
210  return i > last ? COLOR_NONE : i;
211  }
212 
213  for (i = 0; i <= last; i++) {
214  if (!strcmp(colorNames[i], s)) {
215  return i;
216  }
217  }
218 
219  return COLOR_NONE;
220 }

Referenced by Cmd_EchoEx_f(), and SCR_ParseColor().

◆ Com_PlayerToEntityState()

void Com_PlayerToEntityState ( const player_state_t *  ps,
entity_state_t *  es 
)

Definition at line 269 of file utils.c.

270 {
271  vec_t pitch;
272 
273  VectorScale(ps->pmove.origin, 0.125f, es->origin);
274 
275  pitch = ps->viewangles[PITCH];
276  if (pitch > 180) {
277  pitch -= 360;
278  }
279  es->angles[PITCH] = pitch / 3;
280  es->angles[YAW] = ps->viewangles[YAW];
281  es->angles[ROLL] = 0;
282 }

Referenced by CL_DeltaFrame(), entity_update(), and MVD_PlayerToEntityStates().

◆ Com_TimeDiff()

size_t Com_TimeDiff ( char *  buffer,
size_t  size,
time_t *  p,
time_t  now 
)

Definition at line 443 of file utils.c.

444 {
445  time_t diff;
446 
447  if (*p > now) {
448  *p = now;
449  }
450  diff = now - *p;
451  return Com_FormatTime(buffer, size, diff);
452 }

Referenced by Com_Uptime_m(), dump_time(), and SV_PrintMiscInfo().

◆ Com_TimeDiffLong()

size_t Com_TimeDiffLong ( char *  buffer,
size_t  size,
time_t *  p,
time_t  now 
)

Definition at line 454 of file utils.c.

455 {
456  time_t diff;
457 
458  if (*p > now) {
459  *p = now;
460  }
461  diff = now - *p;
462  return Com_FormatTimeLong(buffer, size, diff);
463 }

Referenced by Com_UptimeLong_m().

◆ Com_WildCmpEx()

qboolean Com_WildCmpEx ( const char *  filter,
const char *  string,
int  term,
qboolean  ignorecase 
)

Definition at line 122 of file utils.c.

124 {
125  const char *sub;
126  size_t len;
127  qboolean match;
128 
129  while (*filter && *filter != term) {
130  if (*filter == '*') {
131  // skip consecutive wildcards
132  do {
133  filter++;
134  } while (*filter == '*');
135 
136  // scan out filter part to match
137  for (sub = filter, len = 0; *filter && *filter != term && *filter != '*'; filter++, len++) {
138  // skip over escape character
139  if (*filter == '\\') {
140  filter++;
141  if (!*filter) {
142  break;
143  }
144  }
145  }
146 
147  // wildcard at the end matches everything
148  if (!len) {
149  return qtrue;
150  }
151 
152  string = match_filter(sub, string, len, ignorecase);
153  if (!string) {
154  return qfalse;
155  }
156  } else {
157  // skip over escape character
158  if (*filter == '\\') {
159  filter++;
160  if (!*filter) {
161  break;
162  }
163  match = match_raw(*filter, *string, ignorecase);
164  } else {
165  match = match_char(*filter, *string, ignorecase);
166  }
167 
168  // match single character
169  if (!match) {
170  return qfalse;
171  }
172 
173  filter++;
174  string++;
175  }
176  }
177 
178  // match NUL at the end
179  return !*string;
180 }

Referenced by FS_WildCmp(), and MVD_AutoFollow_f().

◆ match_char()

static qboolean match_char ( int  c1,
int  c2,
qboolean  ignorecase 
)
static

Definition at line 52 of file utils.c.

53 {
54  if (c1 == '?') {
55  return !!c2; // match any char except NUL
56  }
57 
58  return match_raw(c1, c2, ignorecase);
59 }

Referenced by Com_WildCmpEx(), and match_part().

◆ match_filter()

static const char* match_filter ( const char *  filter,
const char *  string,
size_t  len,
qboolean  ignorecase 
)
static

Definition at line 87 of file utils.c.

89 {
90  const char *ret = NULL;
91  size_t remaining = strlen(string);
92 
93  while (remaining >= len) {
94  if (match_part(filter, string, len, ignorecase)) {
95  string += len;
96  remaining -= len;
97  ret = string;
98  continue;
99  }
100  string++;
101  remaining--;
102  }
103 
104  return ret;
105 }

Referenced by Com_WildCmpEx().

◆ match_part()

static qboolean match_part ( const char *  filter,
const char *  string,
size_t  len,
qboolean  ignorecase 
)
static

Definition at line 61 of file utils.c.

63 {
64  qboolean match;
65 
66  do {
67  // skip over escape character
68  if (*filter == '\\') {
69  filter++;
70  match = match_raw(*filter, *string, ignorecase);
71  } else {
72  match = match_char(*filter, *string, ignorecase);
73  }
74 
75  if (!match) {
76  return qfalse;
77  }
78 
79  filter++;
80  string++;
81  } while (--len);
82 
83  return qtrue;
84 }

Referenced by match_filter().

◆ match_raw()

static qboolean match_raw ( int  c1,
int  c2,
qboolean  ignorecase 
)
static

Definition at line 30 of file utils.c.

31 {
32  if (c1 != c2) {
33  if (!ignorecase) {
34  return qfalse;
35  }
36 #ifdef _WIN32
37  // ugly hack for file listing
38  c1 = c1 == '\\' ? '/' : Q_tolower(c1);
39  c2 = c2 == '\\' ? '/' : Q_tolower(c2);
40 #else
41  c1 = Q_tolower(c1);
42  c2 = Q_tolower(c2);
43 #endif
44  if (c1 != c2) {
45  return qfalse;
46  }
47  }
48 
49  return qtrue;
50 }

Referenced by Com_WildCmpEx(), match_char(), and match_part().

Variable Documentation

◆ colorNames

const char* const colorNames[10]
Initial value:
= {
"black", "red", "green", "yellow",
"blue", "cyan", "magenta", "white",
"alt", "none"
}

Definition at line 190 of file utils.c.

Referenced by Com_ParseColor(), and SCR_Color_g().

◆ paged_total

int paged_total

Definition at line 381 of file utils.c.

Referenced by Com_PageInMemory().

Com_FormatTimeLong
size_t Com_FormatTimeLong(char *buffer, size_t size, time_t t)
Definition: utils.c:408
colorNames
const char *const colorNames[10]
Definition: utils.c:190
match_filter
static const char * match_filter(const char *filter, const char *string, size_t len, qboolean ignorecase)
Definition: utils.c:87
paged_total
int paged_total
Definition: utils.c:381
Com_FormatTime
size_t Com_FormatTime(char *buffer, size_t size, time_t t)
Definition: utils.c:391
match_part
static qboolean match_part(const char *filter, const char *string, size_t len, qboolean ignorecase)
Definition: utils.c:61
COM_IsUint
qboolean COM_IsUint(const char *s)
Definition: shared.c:330
c
statCounters_t c
Definition: main.c:30
match_raw
static qboolean match_raw(int c1, int c2, qboolean ignorecase)
Definition: utils.c:30
diff
static q_noinline int diff(uint32_t A_u32, uint32_t B_u32)
Definition: hq2x.c:55
match_char
static qboolean match_char(int c1, int c2, qboolean ignorecase)
Definition: utils.c:52
Q_scnprintf
size_t Q_scnprintf(char *dest, size_t size, const char *fmt,...)
Definition: shared.c:867