hidl_utils.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*##############################################################################
  2. Copyright (C) 2011 HPCC Systems.
  3. All rights reserved. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ############################################################################## */
  14. #ifndef __HIDL_UTILS_HPP__
  15. #define __HIDL_UTILS_HPP__
  16. //
  17. // To avoid recursive dependencies, hidl can NOT use jlib. Hence this utils.
  18. // Most of these are borrowed from jlib or a simplified version in jlib.
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <stdarg.h>
  23. // directory
  24. bool checkDirExists(const char * filename);
  25. void createDirectory(const char* dir);
  26. // create file
  27. int createFile(const char* src, const char* ext);
  28. int createFile(const char* src, const char* tail, const char* ext);
  29. // filenames
  30. char * changeext(const char *fn,const char *ext);
  31. char * changetail(const char *fn,const char *tail, const char *ext);
  32. bool hasext(const char *fn,const char *ext);
  33. char * gettail(const char *fn);
  34. typedef unsigned size32_t;
  35. // a simplified StringBuffer class
  36. class StrBuffer
  37. {
  38. public:
  39. StrBuffer();
  40. StrBuffer(const char *value);
  41. StrBuffer(unsigned len, const char *value);
  42. ~StrBuffer() { if (buffer) free(buffer); }
  43. inline size32_t length() const { return curLen; }
  44. void setLength(unsigned len);
  45. inline void ensureCapacity(unsigned max) { if (maxLen <= curLen + max) _realloc(curLen + max); }
  46. StrBuffer & append(int value);
  47. StrBuffer & append(unsigned int value);
  48. StrBuffer & append(char c);
  49. StrBuffer & append(double value);
  50. StrBuffer & append(const char * value);
  51. StrBuffer & append(unsigned len, const char * value);
  52. StrBuffer & appendf(const char *format, ...) __attribute__((format(printf, 2, 3)));
  53. StrBuffer & setf(const char* format, ...) __attribute__((format(printf, 2, 3)));
  54. StrBuffer & set(const char* val) { return clear().append(val); }
  55. StrBuffer & clear() { curLen = 0; return *this; }
  56. StrBuffer & va_append(const char *format, va_list args);
  57. const char * str() const;
  58. operator const char* () const { return str(); }
  59. char charAt(size32_t pos) { return buffer[pos]; }
  60. void setCharAt(size32_t pos, char value) { buffer[pos] = value; }
  61. char* detach()
  62. {
  63. if (buffer)
  64. {
  65. buffer[curLen] = 0;
  66. char* ret = buffer;
  67. init();
  68. return ret;
  69. }
  70. return strdup("");
  71. }
  72. StrBuffer& operator= (const char* s) { if (s) set(s); return *this; }
  73. protected:
  74. void init()
  75. {
  76. buffer = NULL;
  77. curLen = 0;
  78. maxLen = 0;
  79. }
  80. void _realloc(size32_t newLen);
  81. private:
  82. mutable char * buffer;
  83. size32_t curLen;
  84. size32_t maxLen;
  85. };
  86. class VStrBuffer : public StrBuffer
  87. {
  88. public:
  89. VStrBuffer(const char* format, ...) __attribute__((format(printf, 2, 3)));
  90. };
  91. StrBuffer& encodeXML(const char *x, StrBuffer &ret);
  92. StrBuffer& printfEncode(const char* src, StrBuffer& encoded);
  93. void initLeakCheck(bool fullMemoryCheck);
  94. #endif