xdrstring.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*!
  2. \file lib/db/dbmi_base/xdrstring.c
  3. \brief DBMI Library (base) - external data representation (string)
  4. (C) 1999-2009, 2011 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Joel Jones (CERL/UIUC), Radim Blazek, Brad Douglas, Markus Neteler
  8. \author Doxygenized by Martin Landa <landa.martin gmail.com> (2011)
  9. */
  10. #include <string.h>
  11. #include "xdr.h"
  12. /*!
  13. \brief Send string array
  14. \param a
  15. \param count
  16. \return
  17. */
  18. int db__send_string_array(dbString * a, int count)
  19. {
  20. int i;
  21. int stat;
  22. stat = db__send_int(count);
  23. for (i = 0; stat == DB_OK && i < count; i++)
  24. stat = db__send_string(&a[i]);
  25. return stat;
  26. }
  27. /*!
  28. \brief Receive string array
  29. \param a
  30. \param n
  31. \return
  32. */
  33. int db__recv_string_array(dbString ** a, int *n)
  34. {
  35. int i, count;
  36. int stat;
  37. dbString *b;
  38. *n = 0;
  39. *a = NULL;
  40. stat = db__recv_int(&count);
  41. if (stat != DB_OK)
  42. return stat;
  43. if (count < 0) {
  44. db_protocol_error();
  45. return DB_PROTOCOL_ERR;
  46. }
  47. b = db_alloc_string_array(count);
  48. if (b == NULL)
  49. return DB_MEMORY_ERR;
  50. for (i = 0; i < count; i++) {
  51. stat = db__recv_string(&b[i]);
  52. if (stat != DB_OK) {
  53. db_free_string_array(b, count);
  54. return stat;
  55. }
  56. }
  57. *n = count;
  58. *a = b;
  59. return DB_OK;
  60. }
  61. /*!
  62. \brief Send string
  63. \param x
  64. \return
  65. */
  66. int db__send_string(dbString * x)
  67. {
  68. int stat = DB_OK;
  69. const char *s = db_get_string(x);
  70. int len = s ? strlen(s) + 1 : 1;
  71. if (!s)
  72. s = ""; /* don't send a NULL string */
  73. if (!db__send(&len, sizeof(len)))
  74. stat = DB_PROTOCOL_ERR;
  75. if (!db__send(s, len))
  76. stat = DB_PROTOCOL_ERR;
  77. if (stat == DB_PROTOCOL_ERR)
  78. db_protocol_error();
  79. return stat;
  80. }
  81. /*!
  82. \brief Reads a string from transport
  83. Note: caller MUST initialize x by calling db_init_string()
  84. \param x
  85. \return DB_OK, DB_MEMORY_ERR, or DB_PROTOCOL_ERR
  86. \return NULL if error
  87. */
  88. int db__recv_string(dbString * x)
  89. {
  90. int stat = DB_OK;
  91. int len;
  92. char *s;
  93. if (!db__recv(&len, sizeof(len)))
  94. stat = DB_PROTOCOL_ERR;
  95. if (len <= 0) /* len will include the null byte */
  96. stat = DB_PROTOCOL_ERR;
  97. if (db_enlarge_string(x, len) != DB_OK)
  98. stat = DB_PROTOCOL_ERR;
  99. s = db_get_string(x);
  100. if (!db__recv(s, len))
  101. stat = DB_PROTOCOL_ERR;
  102. if (stat == DB_PROTOCOL_ERR)
  103. db_protocol_error();
  104. return stat;
  105. }
  106. /*!
  107. \brief Send C string
  108. \param s
  109. \return
  110. */
  111. int db__send_Cstring(const char *s)
  112. {
  113. dbString x;
  114. db_init_string(&x);
  115. db_set_string_no_copy(&x, (char *)s);
  116. return db__send_string(&x);
  117. }