Evince
Evince is a document viewer capable of displaying multiple and single page document formats like PDF and Postscript.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
util.c File Reference
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include "common.h"
#include "private.h"
+ Include dependency graph for util.c:

Go to the source code of this file.

Macros

#define NMSGS   (sizeof(messages) / sizeof(char *))
 

Functions

int mdvi_set_logfile (const char *filename)
 
int mdvi_set_logstream (FILE *file)
 
int mdvi_set_loglevel (int level)
 
static void vputlog (int level, const char *head, const char *format, va_list ap)
 
void __debug (int mask, const char *format,...)
 
void mdvi_message (const char *format,...)
 
void mdvi_crash (const char *format,...)
 
void mdvi_error (const char *format,...)
 
void mdvi_warning (const char *format,...)
 
void mdvi_fatal (const char *format,...)
 
void * mdvi_malloc (size_t nelems)
 
void * mdvi_realloc (void *data, size_t newsize)
 
void * mdvi_calloc (size_t nmemb, size_t size)
 
void mdvi_free (void *ptr)
 
char * mdvi_strdup (const char *string)
 
char * mdvi_strncpy (char *to, const char *from, size_t length)
 
char * mdvi_strndup (const char *string, size_t length)
 
void * mdvi_memdup (const void *data, size_t length)
 
char * mdvi_strrstr (const char *haystack, const char *needle)
 
char * mdvi_build_path_from_cwd (const char *path)
 
double unit2pix_factor (const char *spec)
 
int unit2pix (int dpi, const char *spec)
 
Ulong get_mtime (int fd)
 
char * xstradd (char *dest, size_t *size, size_t n, const char *src, size_t m)
 
char * getword (char *string, const char *delim, char **end)
 
char * getstring (char *string, const char *delim, char **end)
 
static long pow2 (size_t n)
 
void dstring_init (Dstring *dstr)
 
int dstring_append (Dstring *dstr, const char *string, int len)
 
int dstring_copy (Dstring *dstr, int pos, const char *string, int len)
 
int dstring_insert (Dstring *dstr, int pos, const char *string, int len)
 
int dstring_new (Dstring *dstr, const char *string, int len)
 
void dstring_reset (Dstring *dstr)
 

Variables

static char *const messages []
 
static FILE * logfile = NULL
 
static int _mdvi_log_level
 
Uint32 _mdvi_debug_mask = 0
 

Macro Definition Documentation

#define NMSGS   (sizeof(messages) / sizeof(char *))

Definition at line 42 of file util.c.

Function Documentation

void __debug ( int  mask,
const char *  format,
  ... 
)

Definition at line 96 of file util.c.

97 {
98  va_list ap;
99 
100  va_start(ap, format);
101  if(_mdvi_debug_mask & mask) {
102  if(!DEBUGGING(SILENT)) {
103  fprintf(stderr, "Debug: ");
104  vfprintf(stderr, format, ap);
105  fflush(stderr);
106  }
107 #ifndef __GNUC__
108  /* let's be portable */
109  va_end(ap);
110  va_start(ap, format);
111 #endif
112  vputlog(LOG_DEBUG, "Debug", format, ap);
113  }
114  va_end(ap);
115 }
int dstring_append ( Dstring dstr,
const char *  string,
int  len 
)

Definition at line 472 of file util.c.

473 {
474  if(len < 0)
475  len = strlen(string);
476  if(len) {
477  if(dstr->length + len >= dstr->size) {
478  dstr->size = pow2(dstr->length + len + 1);
479  dstr->data = mdvi_realloc(dstr->data, dstr->size);
480  }
481  memcpy(dstr->data + dstr->length, string, len);
482  dstr->length += len;
483  dstr->data[dstr->length] = 0;
484  } else if(dstr->size == 0) {
485  ASSERT(dstr->data == NULL);
486  dstr->size = 8;
487  dstr->data = mdvi_malloc(8);
488  dstr->data[0] = 0;
489  }
490 
491  return dstr->length;
492 }

+ Here is the caller graph for this function:

int dstring_copy ( Dstring dstr,
int  pos,
const char *  string,
int  len 
)

Definition at line 494 of file util.c.

495 {
496  ASSERT(pos >= 0);
497  if(len < 0)
498  len = strlen(string);
499  if(len) {
500  if(pos + len >= dstr->length) {
501  dstr->length = pos;
502  return dstring_append(dstr, string, len);
503  }
504  memcpy(dstr->data + pos, string, len);
505  }
506  return dstr->length;
507 }
void dstring_init ( Dstring dstr)

Definition at line 465 of file util.c.

466 {
467  dstr->data = NULL;
468  dstr->size = 0;
469  dstr->length = 0;
470 }

+ Here is the caller graph for this function:

int dstring_insert ( Dstring dstr,
int  pos,
const char *  string,
int  len 
)

Definition at line 509 of file util.c.

510 {
511  ASSERT(pos >= 0);
512  if(pos == dstr->length)
513  return dstring_append(dstr, string, len);
514  if(len < 0)
515  len = strlen(string);
516  if(len) {
517  if(dstr->length + len >= dstr->size) {
518  dstr->size = pow2(dstr->length + len + 1);
519  dstr->data = mdvi_realloc(dstr->data, dstr->size);
520  }
521  /* make room */
522  memmove(dstr->data + pos, dstr->data + pos + len, len);
523  /* now copy */
524  memcpy(dstr->data + pos, string, len);
525  dstr->length += len;
526  dstr->data[dstr->length] = 0;
527  }
528  return dstr->length;
529 }
int dstring_new ( Dstring dstr,
const char *  string,
int  len 
)

Definition at line 531 of file util.c.

532 {
533  if(len < 0)
534  len = strlen(string);
535  if(len) {
536  dstr->size = pow2(len + 1);
537  dstr->data = mdvi_malloc(dstr->size * len);
538  memcpy(dstr->data, string, len);
539  } else
540  dstring_init(dstr);
541  return dstr->length;
542 }
void dstring_reset ( Dstring dstr)

Definition at line 544 of file util.c.

545 {
546  if(dstr->data)
547  mdvi_free(dstr->data);
548  dstring_init(dstr);
549 }

+ Here is the caller graph for this function:

Ulong get_mtime ( int  fd)

Definition at line 396 of file util.c.

397 {
398  struct stat st;
399 
400  if(fstat(fd, &st) == 0)
401  return (Ulong)st.st_mtime;
402  return 0;
403 }

+ Here is the caller graph for this function:

char* getstring ( char *  string,
const char *  delim,
char **  end 
)

Definition at line 436 of file util.c.

437 {
438  char *ptr;
439  char *word;
440  int quoted = 0;
441 
442  /* skip leading delimiters */
443  for(ptr = string; *ptr && strchr(delim, *ptr); ptr++);
444 
445  if(ptr == NULL)
446  return NULL;
447  quoted = (*ptr == '"');
448  if(quoted)
449  for(word = ++ptr; *ptr && *ptr != '"'; ptr++);
450  else
451  for(word = ptr; *ptr && !strchr(delim, *ptr); ptr++);
452  *end = (char *)ptr;
453  return word;
454 }

+ Here is the caller graph for this function:

char* getword ( char *  string,
const char *  delim,
char **  end 
)

Definition at line 418 of file util.c.

419 {
420  char *ptr;
421  char *word;
422 
423  /* skip leading delimiters */
424  for(ptr = string; *ptr && strchr(delim, *ptr); ptr++);
425 
426  if(*ptr == 0)
427  return NULL;
428  word = ptr++;
429  /* skip non-delimiters */
430  while(*ptr && !strchr(delim, *ptr))
431  ptr++;
432  *end = (char *)ptr;
433  return word;
434 }

+ Here is the caller graph for this function:

char* mdvi_build_path_from_cwd ( const char *  path)

Definition at line 323 of file util.c.

324 {
325  char *ptr;
326  char *buf = NULL;
327  size_t buf_size = 512;
328 
329  while (1) {
330  buf = mdvi_realloc (buf, buf_size);
331  if ((ptr = getcwd (buf, buf_size)) == NULL && errno == ERANGE) {
332  buf_size *= 2;
333  } else {
334  buf = ptr;
335  break;
336  }
337  }
338 
339  buf = mdvi_realloc (buf, strlen (buf) + strlen (path) + 2);
340  strcat (buf, "/");
341  strncat (buf, path, strlen (path));
342 
343  return buf;
344 }

+ Here is the caller graph for this function:

void* mdvi_calloc ( size_t  nmemb,
size_t  size 
)

Definition at line 229 of file util.c.

230 {
231  void *ptr;
232 
233  if(nmemb == 0)
234  mdvi_crash(_("attempted to callocate 0 members\n"));
235  if(size == 0)
236  mdvi_crash(_("attempted to callocate %u members with size 0\n"),
237  (unsigned)nmemb);
238  ptr = calloc(nmemb, size);
239  if(ptr == 0)
240  mdvi_fatal(_("failed to allocate %ux%u bytes\n"),
241  (unsigned)nmemb, (unsigned)size);
242  return ptr;
243 }

+ Here is the caller graph for this function:

void mdvi_crash ( const char *  format,
  ... 
)

Definition at line 135 of file util.c.

136 {
137  va_list ap;
138 
139  va_start(ap, format);
140  fprintf(stderr, "%s: %s: ",
141  program_name,
142  gettext(messages[(int)time(NULL) % NMSGS]));
143  vfprintf(stderr, format, ap);
144 #ifndef __GNUC__
145  /* let's be portable */
146  va_end(ap);
147  va_start(ap, format);
148 #endif
149  vputlog(LOG_ERROR, _("Crashing"), format, ap);
150  va_end(ap);
151  abort();
152 }

+ Here is the caller graph for this function:

void mdvi_error ( const char *  format,
  ... 
)

Definition at line 154 of file util.c.

155 {
156  va_list ap;
157 
158  va_start(ap, format);
159  fprintf(stderr, _("%s: Error: "), program_name);
160  vfprintf(stderr, format, ap);
161 #ifndef __GNUC__
162  /* let's be portable */
163  va_end(ap);
164  va_start(ap, format);
165 #endif
166  vputlog(LOG_ERROR, _("Error"), format, ap);
167  va_end(ap);
168 }

+ Here is the caller graph for this function:

void mdvi_fatal ( const char *  format,
  ... 
)

Definition at line 186 of file util.c.

187 {
188  va_list ap;
189 
190  va_start(ap, format);
191  fprintf(stderr, _("%s: Fatal: "), program_name);
192  vfprintf(stderr, format, ap);
193 #ifndef __GNUC__
194  /* let's be portable */
195  va_end(ap);
196  va_start(ap, format);
197 #endif
198  vputlog(LOG_ERROR, _("Fatal"), format, ap);
199  va_end(ap);
200 #ifndef NODEBUG
201  abort();
202 #else
203  exit(EXIT_FAILURE);
204 #endif
205 }

+ Here is the caller graph for this function:

void mdvi_free ( void *  ptr)

Definition at line 245 of file util.c.

246 {
247  if(ptr == NULL)
248  mdvi_crash(_("attempted to free NULL pointer\n"));
249  free(ptr);
250 }
void* mdvi_malloc ( size_t  nelems)

Definition at line 207 of file util.c.

208 {
209  void *ptr = malloc(nelems);
210 
211  if(ptr == NULL)
212  mdvi_fatal(_("out of memory allocating %u bytes\n"),
213  (unsigned)nelems);
214  return ptr;
215 }

+ Here is the caller graph for this function:

void* mdvi_memdup ( const void *  data,
size_t  length 
)

Definition at line 284 of file util.c.

285 {
286  void *ptr = mdvi_malloc(length);
287 
288  memcpy(ptr, data, length);
289  return ptr;
290 }
void mdvi_message ( const char *  format,
  ... 
)

Definition at line 118 of file util.c.

119 {
120  va_list ap;
121 
122  va_start(ap, format);
123  if(_mdvi_log_level >= LOG_INFO) {
124  fprintf(stderr, "%s: ", program_name);
125  vfprintf(stderr, format, ap);
126 #ifndef __GNUC__
127  va_end(ap);
128  va_start(ap, format);
129 #endif
130  }
131  vputlog(LOG_INFO, NULL, format, ap);
132  va_end(ap);
133 }
void* mdvi_realloc ( void *  data,
size_t  newsize 
)

Definition at line 217 of file util.c.

218 {
219  void *ptr;
220 
221  if(newsize == 0)
222  mdvi_crash(_("attempted to reallocate with zero size\n"));
223  ptr = realloc(data, newsize);
224  if(ptr == NULL)
225  mdvi_fatal(_("failed to reallocate %u bytes\n"), (unsigned)newsize);
226  return ptr;
227 }

+ Here is the caller graph for this function:

int mdvi_set_logfile ( const char *  filename)

Definition at line 60 of file util.c.

61 {
62  FILE *f = NULL;
63 
64  if(filename && (f = fopen(filename, "w")) == NULL)
65  return -1;
66  if(logfile != NULL && !isatty(fileno(logfile))) {
67  fclose(logfile);
68  logfile = NULL;
69  }
70  if(filename)
71  logfile = f;
72  return 0;
73 }
int mdvi_set_loglevel ( int  level)

Definition at line 85 of file util.c.

86 {
87  int old = _mdvi_log_level;
88 
90  return old;
91 }
int mdvi_set_logstream ( FILE *  file)

Definition at line 75 of file util.c.

76 {
77  if(logfile && !isatty(fileno(logfile))) {
78  fclose(logfile);
79  logfile = NULL;
80  }
81  logfile = file;
82  return 0;
83 }
char* mdvi_strdup ( const char *  string)

Definition at line 252 of file util.c.

253 {
254  int length;
255  char *ptr;
256 
257  length = strlen(string) + 1;
258  ptr = (char *)mdvi_malloc(length);
259  memcpy(ptr, string, length);
260  return ptr;
261 }

+ Here is the caller graph for this function:

char* mdvi_strncpy ( char *  to,
const char *  from,
size_t  length 
)

Definition at line 264 of file util.c.

265 {
266  strncpy(to, from, length);
267  to[length] = '\0';
268  return to;
269 }
char* mdvi_strndup ( const char *  string,
size_t  length 
)

Definition at line 271 of file util.c.

272 {
273  int n;
274  char *ptr;
275 
276  n = strlen(string);
277  if(n > length)
278  n = length;
279  ptr = (char *)mdvi_malloc(n + 1);
280  memcpy(ptr, string, n);
281  return ptr;
282 }
char* mdvi_strrstr ( const char *  haystack,
const char *  needle 
)

Definition at line 292 of file util.c.

293 {
294  size_t i;
295  size_t needle_len;
296  size_t haystack_len;
297  const char *p;
298 
299  needle_len = strlen (needle);
300  haystack_len = strlen (haystack);
301 
302  if (needle_len == 0)
303  return NULL;
304 
305  if (haystack_len < needle_len)
306  return (char *)haystack;
307 
308  p = haystack + haystack_len - needle_len;
309  while (p >= haystack) {
310  for (i = 0; i < needle_len; i++)
311  if (p[i] != needle[i])
312  goto next;
313 
314  return (char *)p;
315 
316  next:
317  p--;
318  }
319 
320  return NULL;
321 }

+ Here is the caller graph for this function:

void mdvi_warning ( const char *  format,
  ... 
)

Definition at line 170 of file util.c.

171 {
172  va_list ap;
173 
174  va_start(ap, format);
175  fprintf(stderr, _("%s: Warning: "), program_name);
176  vfprintf(stderr, format, ap);
177 #ifndef __GNUC__
178  /* let's be portable */
179  va_end(ap);
180  va_start(ap, format);
181 #endif
182  vputlog(LOG_WARN, _("Warning"), format, ap);
183  va_end(ap);
184 }

+ Here is the caller graph for this function:

static long pow2 ( size_t  n)
static

Definition at line 456 of file util.c.

457 {
458  long x = 8; /* don't bother allocating less than this */
459 
460  while(x < n)
461  x <<= 1L;
462  return x;
463 }

+ Here is the caller graph for this function:

int unit2pix ( int  dpi,
const char *  spec 
)

Definition at line 389 of file util.c.

390 {
391  double factor = unit2pix_factor(spec);
392 
393  return (int)(factor * dpi + 0.5);
394 }

+ Here is the caller graph for this function:

double unit2pix_factor ( const char *  spec)

Definition at line 346 of file util.c.

347 {
348  double val;
349  double factor;
350  const char *p, *q;
351  static const char *units = "incmmmmtptpcddccspbpftydcs";
352 
353  val = 0.0;
354 
355  for(p = spec; *p >= '0' && *p <= '9'; p++)
356  val = 10.0 * val + (double)(*p - '0');
357  if(*p == '.') {
358  p++;
359  factor = 0.1;
360  while(*p && *p >= '0' && *p <= '9') {
361  val += (*p++ - '0') * factor;
362  factor = factor * 0.1;
363  }
364  }
365  factor = 1.0;
366  for(q = units; *q; q += 2) {
367  if(p[0] == q[0] && p[1] == q[1])
368  break;
369  }
370  switch((int)(q - units)) {
371  /*in*/ case 0: factor = 1.0; break;
372  /*cm*/ case 2: factor = 1.0 / 2.54; break;
373  /*mm*/ case 4: factor = 1.0 / 25.4; break;
374  /*mt*/ case 6: factor = 1.0 / 0.0254; break;
375  /*pt*/ case 8: factor = 1.0 / 72.27; break;
376  /*pc*/ case 10: factor = 12.0 / 72.27; break;
377  /*dd*/ case 12: factor = (1238.0 / 1157.0) / 72.27; break;
378  /*cc*/ case 14: factor = 12 * (1238.0 / 1157.0) / 72.27; break;
379  /*sp*/ case 16: factor = 1.0 / (72.27 * 65536); break;
380  /*bp*/ case 18: factor = 1.0 / 72.0; break;
381  /*ft*/ case 20: factor = 12.0; break;
382  /*yd*/ case 22: factor = 36.0; break;
383  /*cs*/ case 24: factor = 1.0 / 72000.0; break;
384  default: factor = 1.0;
385  }
386  return factor * val;
387 }

+ Here is the caller graph for this function:

static void vputlog ( int  level,
const char *  head,
const char *  format,
va_list  ap 
)
static

Definition at line 51 of file util.c.

52 {
53  if(logfile != NULL && _mdvi_log_level >= level) {
54  if(head != NULL)
55  fprintf(logfile, "%s: ", head);
56  vfprintf(logfile, format, ap);
57  }
58 }

+ Here is the caller graph for this function:

char* xstradd ( char *  dest,
size_t *  size,
size_t  n,
const char *  src,
size_t  m 
)

Definition at line 405 of file util.c.

406 {
407  if(m == 0)
408  m = strlen(src);
409  if(n + m >= *size) {
410  dest = mdvi_realloc(dest, n + m + 1);
411  *size = n + m + 1;
412  }
413  memcpy(dest + n, src, m);
414  dest[n + m] = 0;
415  return dest;
416 }

Variable Documentation

Uint32 _mdvi_debug_mask = 0

Definition at line 94 of file util.c.

int _mdvi_log_level
static

Definition at line 45 of file util.c.

FILE* logfile = NULL
static

Definition at line 44 of file util.c.

char* const messages[]
static
Initial value:
= {
_G("Ooops!"),
_G("Aieeeee!!"),
_G("Ouch!"),
_G("Houston, we have a problem"),
_G("3.. 2.. 1.. BOOM!"),
_G("I'm history"),
_G("I'm going down"),
}

Definition at line 32 of file util.c.