2
* Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
2
* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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.
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
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)
192
time_t now = time_wall();
193
gmtime_r(&when, &tm);
196
tm = localtime(&now);
195
localtime_r(&when, &tm);
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);
203
202
ds->length += used;
209
/* Returns a malloc()'d string for time 'when' based on 'template', in local
210
* time or UTC based on 'utc'. */
212
xastrftime(const char *template, time_t when, bool utc)
217
ds_put_strftime(&s, template, when, utc);
211
222
ds_get_line(struct ds *ds, FILE *file)
227
238
* Deletes comments introduced by "#" and skips lines that contains only white
228
239
* space (after deleting comments).
241
* If 'line_numberp' is nonnull, increments '*line_numberp' by the number of
242
* lines read from 'file'.
230
244
* Returns 0 if successful, EOF if no non-blank line was found. */
232
ds_get_preprocessed_line(struct ds *ds, FILE *file)
246
ds_get_preprocessed_line(struct ds *ds, FILE *file, int *line_numberp)
234
248
while (!ds_get_line(ds, file)) {
235
249
char *line = ds_cstr(ds);
238
256
/* Delete comments. */
239
257
comment = strchr(line, '#');
347
365
ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size,
348
366
uintptr_t ofs, bool ascii)
350
const uint8_t *buf = buf_;
351
const size_t per_line = 16; /* Maximum bytes per line. */
355
size_t start, end, n;
358
/* Number of bytes on this line. */
359
start = ofs % per_line;
361
if (end - start > size)
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, " ");
370
ds_put_format(ds, "%02hhx%c",
371
buf[i - start], i == per_line / 2 - 1? '-' : ' ');
374
for (; i < per_line; i++)
368
const uint8_t *buf = buf_;
369
const size_t per_line = 16; /* Maximum bytes per line. */
372
size_t start, end, n;
375
/* Number of bytes on this line. */
376
start = ofs % per_line;
378
if (end - start > size)
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 : '.');
383
for (; i < per_line; i++)
384
ds_put_format(ds, " ");
385
ds_put_format(ds, "|");
387
ds_put_format(ds, "\n");
387
for (; i < end; i++) {
388
ds_put_format(ds, "%02hhx%c",
389
buf[i - start], i == per_line / 2 - 1? '-' : ' ');
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 : '.');
401
for (; i < per_line; i++)
402
ds_put_format(ds, " ");
403
ds_put_format(ds, "|");
407
ds_put_format(ds, "\n");