hidl_utils.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifndef __HIDL_UTILS_HPP__
  14. #define __HIDL_UTILS_HPP__
  15. //
  16. // To avoid recursive dependencies, hidl can NOT use jlib. Hence this utils.
  17. // Most of these are borrowed from jlib or a simplified version in jlib.
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdarg.h>
  22. // directory
  23. bool checkDirExists(const char * filename);
  24. void createDirectory(const char* dir);
  25. // create file
  26. int createFile(const char* src, const char* ext);
  27. int createFile(const char* src, const char* tail, const char* ext);
  28. // filenames
  29. char * changeext(const char *fn,const char *ext);
  30. char * changetail(const char *fn,const char *tail, const char *ext);
  31. bool hasext(const char *fn,const char *ext);
  32. char * gettail(const char *fn);
  33. typedef unsigned size32_t;
  34. // a simplified StringBuffer class
  35. class StrBuffer
  36. {
  37. public:
  38. StrBuffer();
  39. StrBuffer(const char *value);
  40. StrBuffer(unsigned len, const char *value);
  41. ~StrBuffer() { if (buffer) free(buffer); }
  42. inline size32_t length() const { return curLen; }
  43. void setLength(unsigned len);
  44. inline void ensureCapacity(unsigned max) { if (maxLen <= curLen + max) _realloc(curLen + max); }
  45. StrBuffer & append(int value);
  46. StrBuffer & append(unsigned int value);
  47. StrBuffer & append(char c);
  48. StrBuffer & append(double value);
  49. StrBuffer & append(const char * value);
  50. StrBuffer & append(unsigned len, const char * value);
  51. StrBuffer & appendf(const char *format, ...) __attribute__((format(printf, 2, 3)));
  52. StrBuffer & setf(const char* format, ...) __attribute__((format(printf, 2, 3)));
  53. StrBuffer & set(const char* val) { return clear().append(val); }
  54. StrBuffer & clear() { curLen = 0; return *this; }
  55. StrBuffer & va_append(const char *format, va_list args) __attribute__((format(printf,2,0)));
  56. const char * str() const;
  57. operator const char* () const { return str(); }
  58. char charAt(size32_t pos) { return buffer[pos]; }
  59. void setCharAt(size32_t pos, char value) { buffer[pos] = value; }
  60. char* detach()
  61. {
  62. if (buffer)
  63. {
  64. buffer[curLen] = 0;
  65. char* ret = buffer;
  66. init();
  67. return ret;
  68. }
  69. return strdup("");
  70. }
  71. StrBuffer& operator= (const char* s) { if (s) set(s); return *this; }
  72. protected:
  73. void init()
  74. {
  75. buffer = NULL;
  76. curLen = 0;
  77. maxLen = 0;
  78. }
  79. void _realloc(size32_t newLen);
  80. private:
  81. mutable char * buffer;
  82. size32_t curLen;
  83. size32_t maxLen;
  84. };
  85. class VStrBuffer : public StrBuffer
  86. {
  87. public:
  88. VStrBuffer(const char* format, ...) __attribute__((format(printf, 2, 3)));
  89. };
  90. StrBuffer& encodeXML(const char *x, StrBuffer &ret);
  91. StrBuffer& printfEncode(const char* src, StrBuffer& encoded);
  92. void initLeakCheck(bool fullMemoryCheck);
  93. #endif