]>
git.wh0rd.org - chrome-ext/crftp.git/blob - pnacl/util/util.h
1 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
2 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
3 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
6 * Creates new string PP_Var from C string. The resulting object will be a
7 * refcounted string object. It will be AddRef()ed for the caller. When the
8 * caller is done with it, it should be Release()d.
9 * @param[in] str C string to be converted to PP_Var
10 * @return PP_Var containing string.
12 static struct PP_Var
CStrToVar(const char *str
)
14 if (ppb_var_interface
)
15 return ppb_var_interface
->VarFromUtf8(str
, strlen(str
));
17 return PP_MakeUndefined();
21 * Convert a PP_Var to a C string, given a buffer.
22 * @param[in] var The PP_Var to convert.
23 * @param[out] buffer The buffer to write to.
24 * @param[in] length The length of |buffer|.
25 * @return The number of characters written.
27 static uint32_t VarToCStr(struct PP_Var var
, char *buffer
, uint32_t length
)
29 if (ppb_var_interface
== NULL
)
33 const char* str
= ppb_var_interface
->VarToUtf8(var
, &var_length
);
34 /* str is NOT NULL-terminated. Copy using memcpy. */
35 uint32_t min_length
= MIN(var_length
, length
- 1);
36 memcpy(buffer
, str
, min_length
);
37 buffer
[min_length
] = 0;
43 * Printf to a newly allocated C string.
44 * @param[in] format A printf format string.
45 * @param[in] args The printf arguments.
46 * @return The newly constructed string. Caller takes ownership. */
47 static char *VprintfToNewString(const char *format
, va_list args
)
54 va_copy(args_copy
, args
);
55 length
= vsnprintf(NULL
, 0, format
, args
);
56 buffer
= malloc(length
+ 1); /* +1 for NULL-terminator. */
57 result
= vsnprintf(&buffer
[0], length
+ 1, format
, args_copy
);
58 if (result
!= length
) {
66 * Send a message to the JavaScript Console
68 static void console_log(const char *str
, PP_LogLevel level
)
70 if (ppb_console_interface
) {
71 struct PP_Var var
= CStrToVar(str
);
72 ppb_console_interface
->Log(pp_instance
, level
, var
);
73 ppb_var_interface
->Release(var
);
76 #define _Console(lvl) \
77 static void _console_##lvl(const char *s) { console_log(s, PP_LOGLEVEL_##lvl); }
82 #define Console(lvl) _console_##lvl
85 * Create a javaascript like console object:
89 void (*tip
)(const char *);
90 void (*log
)(const char *);
91 void (*warn
)(const char *);
92 void (*error
)(const char *);
96 .warn
= Console(WARNING
),
97 .error
= Console(ERROR
),
100 static bool VarToCArr(struct PP_Var var_array
, char ***array
, uint32_t *length
)
105 if (ppb_var_array_interface
== NULL
) {
111 *length
= ppb_var_array_interface
->GetLength(var_array
);
112 if (*length
> (1 << 16))
114 *array
= malloc(*length
* sizeof(*array
));
118 for (i
= 0; i
< *length
; ++i
) {
119 struct PP_Var var_ele
= ppb_var_array_interface
->Get(var_array
, i
);;
120 uint32_t len
= VarToCStr(var_ele
, buf
, sizeof(buf
));
121 (*array
)[i
] = len
? strdup(buf
) : NULL
;
127 static void CArrFree(char **array
, uint32_t length
)
130 for (i
= 0; i
< length
; ++i
)