summaryrefslogtreecommitdiff
path: root/common-utils/common-utils.h
diff options
context:
space:
mode:
Diffstat (limited to 'common-utils/common-utils.h')
-rw-r--r--common-utils/common-utils.h78
1 files changed, 77 insertions, 1 deletions
diff --git a/common-utils/common-utils.h b/common-utils/common-utils.h
index 04d5f3b..e0d9314 100644
--- a/common-utils/common-utils.h
+++ b/common-utils/common-utils.h
@@ -1,5 +1,5 @@
1/* 1/*
2 * Copyright (C) 2010 NVIDIA Corporation 2 * Copyright (C) 2010-2012 NVIDIA Corporation
3 * 3 *
4 * This program is free software; you can redistribute it and/or modify it 4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License, 5 * under the terms and conditions of the GNU General Public License,
@@ -17,6 +17,9 @@
17#ifndef __COMMON_UTILS_H__ 17#ifndef __COMMON_UTILS_H__
18#define __COMMON_UTILS_H__ 18#define __COMMON_UTILS_H__
19 19
20#include <stdio.h>
21#include <stdarg.h>
22
20#if !defined(TRUE) 23#if !defined(TRUE)
21#define TRUE 1 24#define TRUE 1
22#endif 25#endif
@@ -25,13 +28,86 @@
25#define FALSE 0 28#define FALSE 0
26#endif 29#endif
27 30
31#define ARRAY_LEN(_arr) (sizeof(_arr) / sizeof(_arr[0]))
32
33#define TAB " "
34#define BIGTAB " "
35
36typedef struct {
37 char **t; /* the text rows */
38 int n; /* number of rows */
39 int m; /* maximum row length */
40} TextRows;
41
28void *nvalloc(size_t size); 42void *nvalloc(size_t size);
29char *nvstrcat(const char *str, ...); 43char *nvstrcat(const char *str, ...);
30void *nvrealloc(void *ptr, size_t size); 44void *nvrealloc(void *ptr, size_t size);
31char *nvstrdup(const char *s); 45char *nvstrdup(const char *s);
46char *nvstrndup(const char *s, size_t n);
32char *nvstrtolower(char *s); 47char *nvstrtolower(char *s);
33void nvfree(void *s); 48void nvfree(void *s);
34 49
35char *tilde_expansion(const char *str); 50char *tilde_expansion(const char *str);
36 51
52TextRows *nv_format_text_rows(const char *prefix,
53 const char *str,
54 int width, int word_boundary);
55void nv_text_rows_append(TextRows *t, const char *msg);
56void nv_concat_text_rows(TextRows *t0, TextRows *t1);
57void nv_free_text_rows(TextRows *t);
58
59void reset_current_terminal_width(unsigned short new_val);
60
61void silence_fmt(int val);
62void fmtout(const char *fmt, ...);
63void fmtoutp(const char *prefix, const char *fmt, ...);
64void fmterr(const char *fmt, ...);
65void fmtwarn(const char *fmt, ...);
66void fmt(FILE *stream, const char *prefix, const char *fmt, ...);
67
68/*
69 * NV_VSNPRINTF(): macro that assigns buf using vsnprintf(). This is
70 * correct for differing semantics of the vsnprintf() return value:
71 *
72 * -1 when the buffer is not long enough (glibc < 2.1)
73 *
74 * or
75 *
76 * the length the string would have been if the buffer had been large
77 * enough (glibc >= 2.1)
78 *
79 * This macro allocates memory for buf; the caller should free it when
80 * done.
81 */
82
83#define NV_FMT_BUF_LEN 256
84
85#define NV_VSNPRINTF(buf, fmt) \
86do { \
87 if (!fmt) { \
88 (buf) = NULL; \
89 } else { \
90 va_list ap; \
91 int len, current_len = NV_FMT_BUF_LEN; \
92 \
93 (buf) = malloc(current_len); \
94 \
95 while (1) { \
96 va_start(ap, fmt); \
97 len = vsnprintf((buf), current_len, (fmt), ap); \
98 va_end(ap); \
99 \
100 if ((len > -1) && (len < current_len)) { \
101 break; \
102 } else if (len > -1) { \
103 current_len = len + 1; \
104 } else { \
105 current_len += NV_FMT_BUF_LEN; \
106 } \
107 free(buf); \
108 (buf) = malloc(current_len); \
109 } \
110 } \
111} while (0)
112
37#endif /* __COMMON_UTILS_H__ */ 113#endif /* __COMMON_UTILS_H__ */