~james-page/ubuntu/saucy/openvswitch/1.12-snapshot

« back to all changes in this revision

Viewing changes to lib/dynamic-string.c

  • Committer: James Page
  • Date: 2013-08-21 10:16:57 UTC
  • mfrom: (1.1.20)
  • Revision ID: james.page@canonical.com-20130821101657-3o0z0qeiv5zkwlzi
New upstream snapshot

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
 
2
 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
3
3
 *
4
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
5
 * you may not use this file except in compliance with the License.
183
183
    }
184
184
}
185
185
 
186
 
/* Writes the current time to 'string' based on 'template'.
187
 
 * The current time is either localtime or UTC based on 'utc'. */
 
186
/* Writes time 'when' to 'string' based on 'template', in local time or UTC
 
187
 * based on 'utc'. */
188
188
void
189
 
ds_put_strftime(struct ds *ds, const char *template, bool utc)
 
189
ds_put_strftime(struct ds *ds, const char *template, time_t when, bool utc)
190
190
{
191
 
    const struct tm *tm;
192
 
    time_t now = time_wall();
 
191
    struct tm tm;
193
192
    if (utc) {
194
 
        tm = gmtime(&now);
 
193
        gmtime_r(&when, &tm);
195
194
    } else {
196
 
        tm = localtime(&now);
 
195
        localtime_r(&when, &tm);
197
196
    }
198
197
 
199
198
    for (;;) {
200
199
        size_t avail = ds->string ? ds->allocated - ds->length + 1 : 0;
201
 
        size_t used = strftime(&ds->string[ds->length], avail, template, tm);
 
200
        size_t used = strftime(&ds->string[ds->length], avail, template, &tm);
202
201
        if (used) {
203
202
            ds->length += used;
204
203
            return;
207
206
    }
208
207
}
209
208
 
 
209
/* Returns a malloc()'d string for time 'when' based on 'template', in local
 
210
 * time or UTC based on 'utc'. */
 
211
char *
 
212
xastrftime(const char *template, time_t when, bool utc)
 
213
{
 
214
    struct ds s;
 
215
 
 
216
    ds_init(&s);
 
217
    ds_put_strftime(&s, template, when, utc);
 
218
    return s.string;
 
219
}
 
220
 
210
221
int
211
222
ds_get_line(struct ds *ds, FILE *file)
212
223
{
227
238
 * Deletes comments introduced by "#" and skips lines that contains only white
228
239
 * space (after deleting comments).
229
240
 *
 
241
 * If 'line_numberp' is nonnull, increments '*line_numberp' by the number of
 
242
 * lines read from 'file'.
 
243
 *
230
244
 * Returns 0 if successful, EOF if no non-blank line was found. */
231
245
int
232
 
ds_get_preprocessed_line(struct ds *ds, FILE *file)
 
246
ds_get_preprocessed_line(struct ds *ds, FILE *file, int *line_numberp)
233
247
{
234
248
    while (!ds_get_line(ds, file)) {
235
249
        char *line = ds_cstr(ds);
236
250
        char *comment;
237
251
 
 
252
        if (line_numberp) {
 
253
            ++*line_numberp;
 
254
        }
 
255
 
238
256
        /* Delete comments. */
239
257
        comment = strchr(line, '#');
240
258
        if (comment) {
347
365
ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size,
348
366
                uintptr_t ofs, bool ascii)
349
367
{
350
 
  const uint8_t *buf = buf_;
351
 
  const size_t per_line = 16; /* Maximum bytes per line. */
352
 
 
353
 
  while (size > 0)
354
 
    {
355
 
      size_t start, end, n;
356
 
      size_t i;
357
 
 
358
 
      /* Number of bytes on this line. */
359
 
      start = ofs % per_line;
360
 
      end = per_line;
361
 
      if (end - start > size)
362
 
        end = start + size;
363
 
      n = end - start;
364
 
 
365
 
      /* Print line. */
366
 
      ds_put_format(ds, "%08jx  ", (uintmax_t) ROUND_DOWN(ofs, per_line));
367
 
      for (i = 0; i < start; i++)
368
 
        ds_put_format(ds, "   ");
369
 
      for (; i < end; i++)
370
 
        ds_put_format(ds, "%02hhx%c",
371
 
                buf[i - start], i == per_line / 2 - 1? '-' : ' ');
372
 
      if (ascii)
373
 
        {
374
 
          for (; i < per_line; i++)
 
368
    const uint8_t *buf = buf_;
 
369
    const size_t per_line = 16; /* Maximum bytes per line. */
 
370
 
 
371
    while (size > 0) {
 
372
        size_t start, end, n;
 
373
        size_t i;
 
374
 
 
375
        /* Number of bytes on this line. */
 
376
        start = ofs % per_line;
 
377
        end = per_line;
 
378
        if (end - start > size)
 
379
            end = start + size;
 
380
        n = end - start;
 
381
 
 
382
        /* Print line. */
 
383
        ds_put_format(ds, "%08jx  ", (uintmax_t) ROUND_DOWN(ofs, per_line));
 
384
        for (i = 0; i < start; i++) {
375
385
            ds_put_format(ds, "   ");
376
 
          ds_put_format(ds, "|");
377
 
          for (i = 0; i < start; i++)
378
 
            ds_put_format(ds, " ");
379
 
          for (; i < end; i++) {
380
 
              int c = buf[i - start];
381
 
              ds_put_char(ds, c >= 32 && c < 127 ? c : '.');
382
 
          }
383
 
          for (; i < per_line; i++)
384
 
            ds_put_format(ds, " ");
385
 
          ds_put_format(ds, "|");
386
 
        }
387
 
      ds_put_format(ds, "\n");
 
386
        }
 
387
        for (; i < end; i++) {
 
388
            ds_put_format(ds, "%02hhx%c",
 
389
                          buf[i - start], i == per_line / 2 - 1? '-' : ' ');
 
390
        }
 
391
        if (ascii) {
 
392
            for (; i < per_line; i++)
 
393
                ds_put_format(ds, "   ");
 
394
            ds_put_format(ds, "|");
 
395
            for (i = 0; i < start; i++)
 
396
                ds_put_format(ds, " ");
 
397
            for (; i < end; i++) {
 
398
                int c = buf[i - start];
 
399
                ds_put_char(ds, c >= 32 && c < 127 ? c : '.');
 
400
            }
 
401
            for (; i < per_line; i++)
 
402
                ds_put_format(ds, " ");
 
403
            ds_put_format(ds, "|");
 
404
        } else {
 
405
            ds_chomp(ds, ' ');
 
406
        }
 
407
        ds_put_format(ds, "\n");
388
408
 
389
 
      ofs += n;
390
 
      buf += n;
391
 
      size -= n;
 
409
        ofs += n;
 
410
        buf += n;
 
411
        size -= n;
392
412
    }
393
413
}
394
414