dumpvalue.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /* dumpvalue() service, which prints to Standard Output the value of
  2. an xmlrpc_value.
  3. We've put this in a separate module in hopes that it eventually can be
  4. used for debugging purposes in other places.
  5. */
  6. #define _XOPEN_SOURCE 600 /* Make sure strdup() is in <string.h> */
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "xmlrpc_config.h" /* information about this build environment */
  11. #include "int.h"
  12. #include "casprintf.h"
  13. #include "mallocvar.h"
  14. #include "xmlrpc-c/base.h"
  15. #include "xmlrpc-c/string_int.h"
  16. #include "dumpvalue.h"
  17. static void
  18. dumpInt(const char * const prefix,
  19. xmlrpc_value * const valueP) {
  20. xmlrpc_env env;
  21. xmlrpc_int value;
  22. xmlrpc_env_init(&env);
  23. xmlrpc_read_int(&env, valueP, &value);
  24. if (env.fault_occurred)
  25. printf("Internal error: unable to extract value of "
  26. "integer xmlrpc_value %lx. %s\n",
  27. (unsigned long)valueP, env.fault_string);
  28. else
  29. printf("%sInteger: %d\n", prefix, value);
  30. xmlrpc_env_clean(&env);
  31. }
  32. static void
  33. dumpBool(const char * const prefix,
  34. xmlrpc_value * const valueP) {
  35. xmlrpc_env env;
  36. xmlrpc_bool value;
  37. xmlrpc_env_init(&env);
  38. xmlrpc_read_bool(&env, valueP, &value);
  39. if (env.fault_occurred)
  40. printf("Internal error: Unable to extract value of "
  41. "boolean xmlrpc_value %lx. %s\n",
  42. (unsigned long)valueP, env.fault_string);
  43. else
  44. printf("%sBoolean: %s\n", prefix, value ? "TRUE" : "FALSE");
  45. xmlrpc_env_clean(&env);
  46. }
  47. static void
  48. dumpDouble(const char * const prefix,
  49. xmlrpc_value * const valueP) {
  50. xmlrpc_env env;
  51. xmlrpc_double value;
  52. xmlrpc_env_init(&env);
  53. xmlrpc_read_double(&env, valueP, &value);
  54. if (env.fault_occurred)
  55. printf("Internal error: Unable to extract value from "
  56. "floating point number xmlrpc_value %lx. %s\n",
  57. (unsigned long)valueP, env.fault_string);
  58. else
  59. printf("%sFloating Point: %f\n", prefix, value);
  60. xmlrpc_env_clean(&env);
  61. }
  62. static void
  63. dumpDatetime(const char * const prefix,
  64. xmlrpc_value * const valueP) {
  65. printf("%sDon't know how to print datetime value %lx.\n",
  66. prefix, (unsigned long) valueP);
  67. }
  68. static size_t
  69. nextLineSize(const char * const string,
  70. size_t const startPos,
  71. size_t const stringSize) {
  72. /*----------------------------------------------------------------------------
  73. Return the length of the line that starts at offset 'startPos' in the
  74. string 'string', which is 'stringSize' characters long.
  75. 'string' in not NUL-terminated.
  76. A line begins at beginning of string or after a newline character and
  77. runs through the next newline character or end of string. The line
  78. includes the newline character at the end, if any.
  79. -----------------------------------------------------------------------------*/
  80. size_t i;
  81. for (i = startPos; i < stringSize && string[i] != '\n'; ++i);
  82. if (i < stringSize)
  83. ++i; /* Include the newline */
  84. return i - startPos;
  85. }
  86. static void
  87. dumpMultilineString(const char * const prefix,
  88. const char * const value,
  89. size_t const length) {
  90. size_t cursor; /* Index into value[] */
  91. for (cursor = 0; cursor < length; ) {
  92. /* Print one line of buffer */
  93. size_t const lineSize = nextLineSize(value, cursor, length);
  94. const char * const printableLine =
  95. xmlrpc_makePrintable_lp(&value[cursor], lineSize);
  96. printf("%s%s\n", prefix, printableLine);
  97. cursor += lineSize;
  98. strfree(printableLine);
  99. }
  100. }
  101. static void
  102. dumpSimpleString(const char * const value,
  103. size_t const length) {
  104. const char * const printable = xmlrpc_makePrintable_lp(value, length);
  105. printf("'%s'\n", printable);
  106. strfree(printable);
  107. }
  108. static void
  109. dumpString(const char * const prefix,
  110. xmlrpc_value * const valueP) {
  111. xmlrpc_env env;
  112. size_t length;
  113. const char * value;
  114. xmlrpc_env_init(&env);
  115. xmlrpc_read_string_lp(&env, valueP, &length, &value);
  116. if (env.fault_occurred)
  117. printf("Internal error: Unable to extract value from "
  118. "string xmlrpc_value %lx. %s\n",
  119. (unsigned long)valueP, env.fault_string);
  120. else {
  121. printf("%sString: ", prefix);
  122. if (strlen(value) == length && strchr(value, '\n')) {
  123. const char * prefix2;
  124. casprintf(&prefix2, "%s ", prefix);
  125. printf("\n");
  126. dumpMultilineString(prefix2, value, length);
  127. strfree(prefix2);
  128. } else
  129. dumpSimpleString(value, length);
  130. strfree(value);
  131. }
  132. xmlrpc_env_clean(&env);
  133. }
  134. static void
  135. dumpBase64(const char * const prefix,
  136. xmlrpc_value * const valueP) {
  137. xmlrpc_env env;
  138. const unsigned char * value;
  139. size_t length;
  140. xmlrpc_env_init(&env);
  141. xmlrpc_read_base64(&env, valueP, &length, &value);
  142. if (env.fault_occurred)
  143. printf("Unable to parse base64 bit string xmlrpc_value %lx. %s\n",
  144. (unsigned long)valueP, env.fault_string);
  145. else {
  146. unsigned int i;
  147. printf("%sBit string: ", prefix);
  148. for (i = 0; i < length; ++i)
  149. printf("%02x", value[i]);
  150. free((void*)value);
  151. }
  152. xmlrpc_env_clean(&env);
  153. }
  154. static void
  155. dumpArray(const char * const prefix,
  156. xmlrpc_value * const arrayP) {
  157. xmlrpc_env env;
  158. unsigned int arraySize;
  159. xmlrpc_env_init(&env);
  160. XMLRPC_ASSERT_ARRAY_OK(arrayP);
  161. arraySize = xmlrpc_array_size(&env, arrayP);
  162. if (env.fault_occurred)
  163. printf("Unable to get array size. %s\n", env.fault_string);
  164. else {
  165. int const spaceCount = strlen(prefix);
  166. unsigned int i;
  167. const char * blankPrefix;
  168. printf("%sArray of %u items:\n", prefix, arraySize);
  169. casprintf(&blankPrefix, "%*s", spaceCount, "");
  170. for (i = 0; i < arraySize; ++i) {
  171. xmlrpc_value * valueP;
  172. xmlrpc_array_read_item(&env, arrayP, i, &valueP);
  173. if (env.fault_occurred)
  174. printf("Unable to get array item %u\n", i);
  175. else {
  176. const char * prefix2;
  177. casprintf(&prefix2, "%s Index %2u ", blankPrefix, i);
  178. dumpValue(prefix2, valueP);
  179. strfree(prefix2);
  180. xmlrpc_DECREF(valueP);
  181. }
  182. }
  183. strfree(blankPrefix);
  184. }
  185. xmlrpc_env_clean(&env);
  186. }
  187. static void
  188. dumpStructMember(const char * const prefix,
  189. xmlrpc_value * const structP,
  190. unsigned int const index) {
  191. xmlrpc_env env;
  192. xmlrpc_value * keyP;
  193. xmlrpc_value * valueP;
  194. xmlrpc_env_init(&env);
  195. xmlrpc_struct_read_member(&env, structP, index, &keyP, &valueP);
  196. if (env.fault_occurred)
  197. printf("Unable to get struct member %u\n", index);
  198. else {
  199. int const blankCount = strlen(prefix);
  200. const char * prefix2;
  201. const char * blankPrefix;
  202. casprintf(&blankPrefix, "%*s", blankCount, "");
  203. casprintf(&prefix2, "%s Key: ", blankPrefix);
  204. dumpValue(prefix2, keyP);
  205. strfree(prefix2);
  206. casprintf(&prefix2, "%s Value: ", blankPrefix);
  207. dumpValue(prefix2, valueP);
  208. strfree(prefix2);
  209. strfree(blankPrefix);
  210. xmlrpc_DECREF(keyP);
  211. xmlrpc_DECREF(valueP);
  212. }
  213. xmlrpc_env_clean(&env);
  214. }
  215. static void
  216. dumpStruct(const char * const prefix,
  217. xmlrpc_value * const structP) {
  218. xmlrpc_env env;
  219. unsigned int structSize;
  220. xmlrpc_env_init(&env);
  221. structSize = xmlrpc_struct_size(&env, structP);
  222. if (env.fault_occurred)
  223. printf("Unable to get struct size. %s\n", env.fault_string);
  224. else {
  225. unsigned int i;
  226. printf("%sStruct of %u members:\n", prefix, structSize);
  227. for (i = 0; i < structSize; ++i) {
  228. const char * prefix1;
  229. if (i == 0)
  230. prefix1 = strdup(prefix);
  231. else {
  232. int const blankCount = strlen(prefix);
  233. casprintf(&prefix1, "%*s", blankCount, "");
  234. }
  235. dumpStructMember(prefix1, structP, i);
  236. strfree(prefix1);
  237. }
  238. }
  239. xmlrpc_env_clean(&env);
  240. }
  241. static void
  242. dumpCPtr(const char * const prefix,
  243. xmlrpc_value * const valueP) {
  244. xmlrpc_env env;
  245. void * value;
  246. xmlrpc_env_init(&env);
  247. xmlrpc_read_cptr(&env, valueP, &value);
  248. if (env.fault_occurred)
  249. printf("Unable to parse C pointer xmlrpc_value %lx. %s\n",
  250. (unsigned long)valueP, env.fault_string);
  251. else
  252. printf("%sC pointer: '%lux'\n", prefix, (unsigned long)value);
  253. xmlrpc_env_clean(&env);
  254. }
  255. static void
  256. dumpNil(const char * const prefix,
  257. xmlrpc_value * const valueP) {
  258. xmlrpc_env env;
  259. xmlrpc_env_init(&env);
  260. xmlrpc_read_nil(&env, valueP);
  261. if (env.fault_occurred)
  262. printf("Internal error: nil value xmlrpc_value %lx "
  263. "is not valid. %s\n",
  264. (unsigned long)valueP, env.fault_string);
  265. else
  266. printf("%sNil\n", prefix);
  267. xmlrpc_env_clean(&env);
  268. }
  269. static void
  270. dumpI8(const char * const prefix,
  271. xmlrpc_value * const valueP) {
  272. xmlrpc_env env;
  273. xmlrpc_int64 value;
  274. xmlrpc_env_init(&env);
  275. xmlrpc_read_i8(&env, valueP, &value);
  276. if (env.fault_occurred)
  277. printf("Internal error: unable to extract value of "
  278. "64-bit integer xmlrpc_value %lx. %s\n",
  279. (unsigned long)valueP, env.fault_string);
  280. else
  281. printf("%s64-bit integer: %" XMLRPC_PRId64 "\n", prefix, value);
  282. xmlrpc_env_clean(&env);
  283. }
  284. static void
  285. dumpUnknown(const char * const prefix,
  286. xmlrpc_value * const valueP) {
  287. printf("%sDon't recognize value type %u of xmlrpc_value %lx.\n",
  288. prefix, xmlrpc_value_type(valueP), (unsigned long)valueP);
  289. printf("%sCan't print it.\n", prefix);
  290. }
  291. void
  292. dumpValue(const char * const prefix,
  293. xmlrpc_value * const valueP) {
  294. switch (xmlrpc_value_type(valueP)) {
  295. case XMLRPC_TYPE_INT:
  296. dumpInt(prefix, valueP);
  297. break;
  298. case XMLRPC_TYPE_BOOL:
  299. dumpBool(prefix, valueP);
  300. break;
  301. case XMLRPC_TYPE_DOUBLE:
  302. dumpDouble(prefix, valueP);
  303. break;
  304. case XMLRPC_TYPE_DATETIME:
  305. dumpDatetime(prefix, valueP);
  306. break;
  307. case XMLRPC_TYPE_STRING:
  308. dumpString(prefix, valueP);
  309. break;
  310. case XMLRPC_TYPE_BASE64:
  311. dumpBase64(prefix, valueP);
  312. break;
  313. case XMLRPC_TYPE_ARRAY:
  314. dumpArray(prefix, valueP);
  315. break;
  316. case XMLRPC_TYPE_STRUCT:
  317. dumpStruct(prefix, valueP);
  318. break;
  319. case XMLRPC_TYPE_C_PTR:
  320. dumpCPtr(prefix, valueP);
  321. break;
  322. case XMLRPC_TYPE_NIL:
  323. dumpNil(prefix, valueP);
  324. break;
  325. case XMLRPC_TYPE_I8:
  326. dumpI8(prefix, valueP);
  327. break;
  328. default:
  329. dumpUnknown(prefix, valueP);
  330. }
  331. }