jutil.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 JUTIL_HPP
  15. #define JUTIL_HPP
  16. #include "jexpdef.hpp"
  17. #include "jstring.hpp"
  18. #include "jarray.hpp"
  19. #include "jbuff.hpp"
  20. //#define NAMEDCOUNTS
  21. interface IPropertyTree;
  22. void jlib_decl MilliSleep(unsigned milli);
  23. long jlib_decl atolong_l(const char * s,int l);
  24. int jlib_decl atoi_l(const char * s,int l);
  25. __int64 jlib_decl atoi64_l(const char * s,int l);
  26. inline __int64 atoi64(const char* s) { return atoi64_l(s, (int)strlen(s)); }
  27. #ifndef _WIN32
  28. extern jlib_decl char * itoa(int n, char *str, int b);
  29. extern jlib_decl char * ltoa(long n, char *str, int b);
  30. extern jlib_decl char * ultoa(unsigned long n, char *str, int b);
  31. #define Sleep(milli) MilliSleep(milli)
  32. #endif
  33. void jlib_decl packNumber(char * target, const char * source, unsigned slen);
  34. void jlib_decl unpackNumber(char * target, const char * source, unsigned tlen);
  35. int jlib_decl numtostr(char *dst, char _value);
  36. int jlib_decl numtostr(char *dst, short _value);
  37. int jlib_decl numtostr(char *dst, int _value);
  38. int jlib_decl numtostr(char *dst, long _value);
  39. int jlib_decl numtostr(char *dst, __int64 _value);
  40. int jlib_decl numtostr(char *dst, unsigned char value);
  41. int jlib_decl numtostr(char *dst, unsigned short value);
  42. int jlib_decl numtostr(char *dst, unsigned int value);
  43. int jlib_decl numtostr(char *dst, unsigned long value);
  44. int jlib_decl numtostr(char *dst, unsigned __int64 _value);
  45. extern jlib_decl HINSTANCE LoadSharedObject(const char *name, bool isGlobal, bool raiseOnError);
  46. extern jlib_decl void FreeSharedObject(HINSTANCE h);
  47. class jlib_decl SharedObject
  48. {
  49. public:
  50. SharedObject() { h = 0; bRefCounted = false; }
  51. ~SharedObject() { unload(); }
  52. bool load(const char * dllName, bool isGlobal, bool raiseOnError=false);
  53. bool loadCurrentExecutable();
  54. bool loaded() { return h != 0; }
  55. void unload();
  56. HINSTANCE getInstanceHandle() const { return h; }
  57. public:
  58. HINSTANCE h;
  59. bool bRefCounted;
  60. };
  61. //---------------------------------------------------------------------------
  62. //functions for generating unique identifiers consisting of 0..9,A..V
  63. typedef unsigned __int64 unique_id_t;
  64. extern jlib_decl StringBuffer & appendUniqueId(StringBuffer & target, unique_id_t value);
  65. extern jlib_decl unique_id_t getUniqueId();
  66. extern jlib_decl StringBuffer & getUniqueId(StringBuffer & target);
  67. extern jlib_decl void resetUniqueId();
  68. extern jlib_decl unsigned getRandom(); // global
  69. extern jlib_decl void seedRandom(unsigned seed);
  70. interface IRandomNumberGenerator: public IInterface
  71. {
  72. virtual void seed(unsigned seedval)=0;
  73. virtual unsigned next()=0;
  74. };
  75. extern jlib_decl IRandomNumberGenerator *createRandomNumberGenerator();
  76. #ifdef WIN32
  77. // Reentrant version of the rand() function for use with multithreaded applications.
  78. // rand_r return value between 0 and RAND_R_MAX (exclusive). Not that RAND_MAX is
  79. // implementation dependent: SHORT_MAX on Windows, INT_MAX on Linux.
  80. jlib_decl int rand_r(unsigned int *seed);
  81. #define RAND_R_MAX INT_MAX
  82. #else
  83. #define RAND_R_MAX RAND_MAX
  84. #endif
  85. interface IShuffledIterator: extends IInterface
  86. {
  87. virtual void seed(unsigned seedval)=0; // ony required for repeatability
  88. virtual bool first()=0;
  89. virtual bool isValid() = 0;
  90. virtual bool next() = 0;
  91. virtual unsigned get() = 0;
  92. virtual unsigned lookup(unsigned idx) = 0; // looks up idx'th entry
  93. };
  94. extern jlib_decl IShuffledIterator *createShuffledIterator(unsigned n); // returns iterator that returns 0..n-1 in shuffled order
  95. /* misc */
  96. extern jlib_decl bool isCIdentifier(const char* id);
  97. /* base64 encoder/decoder */
  98. extern jlib_decl void JBASE64_Encode(const void *data, long length, StringBuffer &out, bool addLineBreaks=true);
  99. extern jlib_decl void JBASE64_Encode(const void *data, long length, IIOStream &out, bool addLineBreaks=true);
  100. extern jlib_decl StringBuffer &JBASE64_Decode(const char *in, StringBuffer &out);
  101. extern jlib_decl MemoryBuffer &JBASE64_Decode(const char *in, MemoryBuffer &out);
  102. extern jlib_decl StringBuffer &JBASE64_Decode(ISimpleReadStream &in, StringBuffer &out);
  103. extern jlib_decl MemoryBuffer &JBASE64_Decode(ISimpleReadStream &in, MemoryBuffer &out);
  104. extern jlib_decl void JBASE32_Encode(const char *in,StringBuffer &out); // result all lower
  105. extern jlib_decl void JBASE32_Decode(const char *in,StringBuffer &out);
  106. /* URL: http://user:passwd@host:port/path */
  107. extern jlib_decl StringBuffer& encodeUrlUseridPassword(StringBuffer& out, const char* in);
  108. extern jlib_decl StringBuffer& decodeUrlUseridPassword(StringBuffer& out, const char* in);
  109. class StringArray : public ArrayOf<const char *, const char *>
  110. {
  111. };
  112. class CIStringArray : public StringArray, public CInterface
  113. {
  114. };
  115. // separated list to array
  116. extern jlib_decl void DelimToStringArray(const char *csl, StringArray &dst, const char * delim, bool deldup=false);
  117. extern jlib_decl void CslToStringArray(const char *csl, StringArray &dst, bool deldup=false);
  118. extern jlib_decl unsigned msTick();
  119. extern jlib_decl unsigned usTick();
  120. extern jlib_decl int make_daemon(bool printpid=false); // outputs pid to stdout if printpid true
  121. extern jlib_decl void doStackProbe();
  122. #ifndef arraysize
  123. #define arraysize(T) (sizeof(T)/sizeof(*T))
  124. #endif
  125. extern jlib_decl bool callExternalProgram(const char *progname, const StringBuffer &input, StringBuffer &output, StringArray *env=NULL);
  126. extern jlib_decl unsigned __int64 greatestCommonDivisor(unsigned __int64 left, unsigned __int64 right);
  127. inline unsigned hex2num(char next)
  128. {
  129. if ((next >= '0') && (next <= '9'))
  130. return next - '0';
  131. if ((next >= 'a') && (next <= 'f'))
  132. return next - 'a' + 10;
  133. if ((next >= 'A') && (next <= 'F'))
  134. return next - 'A' + 10;
  135. return 0;
  136. }
  137. extern jlib_decl void initThreadLocal(int len, void* val);
  138. extern jlib_decl void* getThreadLocalVal();
  139. extern jlib_decl void clearThreadLocal();
  140. extern jlib_decl bool matchesMask(const char *fn, const char *mask, unsigned p, unsigned n);
  141. extern jlib_decl StringBuffer &expandMask(StringBuffer &buf, const char *mask, unsigned p, unsigned n);
  142. extern jlib_decl bool constructMask(StringAttr &attr, const char *fn, unsigned p, unsigned n);
  143. extern jlib_decl bool deduceMask(const char *fn, bool expandN, StringAttr &mask, unsigned &p, unsigned &n); // p is 0 based in these routines
  144. class HashKeyElement;
  145. class jlib_decl NamedCount
  146. {
  147. HashKeyElement *ht;
  148. public:
  149. NamedCount();
  150. ~NamedCount();
  151. void set(const char *name);
  152. };
  153. #ifdef NAMEDCOUNTS
  154. #define DECL_NAMEDCOUNT NamedCount namedCount
  155. #define INIT_NAMEDCOUNT namedCount.set(typeid(*this).name())
  156. #else
  157. #define DECL_NAMEDCOUNT
  158. #define INIT_NAMEDCOUNT {}
  159. #endif
  160. extern jlib_decl StringBuffer &dumpNamedCounts(StringBuffer &str);
  161. interface IAuthenticatedUser: extends IInterface
  162. {
  163. virtual bool login(const char *user, const char *passwd) = 0;
  164. virtual void impersonate()=0;
  165. virtual void revert()=0;
  166. virtual const char *username()=0;
  167. };
  168. interface IAtom;
  169. extern jlib_decl IAuthenticatedUser *createAuthenticatedUser();
  170. extern jlib_decl void serializeAtom(MemoryBuffer & target, IAtom * name);
  171. extern jlib_decl IAtom * deserializeAtom(MemoryBuffer & source);
  172. template <class KEY, class VALUE, class COMPARE>
  173. VALUE * binsearch(KEY key, VALUE * * values, unsigned num, COMPARE * cmp)
  174. {
  175. unsigned l = 0;
  176. unsigned u = num;
  177. while(l<u)
  178. {
  179. unsigned i = (l+u)/2;
  180. int c = cmp->compare(key, values[i]);
  181. if(c == 0)
  182. {
  183. return values[i];
  184. }
  185. else if(c < 0)
  186. {
  187. u = i;
  188. }
  189. else
  190. {
  191. l = i+1;
  192. }
  193. }
  194. return NULL;
  195. }
  196. extern jlib_decl StringBuffer &genUUID(StringBuffer &in, bool nocase=false);
  197. // Convert offset_t to string
  198. // Note that offset_t can be 32, 64 bit integers or a structure
  199. class jlib_decl OffsetToString
  200. {
  201. StringBuffer m_buffer;
  202. public:
  203. OffsetToString(offset_t offset);
  204. const char* str() { return m_buffer.str(); }
  205. };
  206. extern jlib_decl StringBuffer passwordInput(const char* prompt, StringBuffer& passwd);
  207. extern jlib_decl IPropertyTree *getHPCCenvironment(const char *confloc=NULL);
  208. extern jlib_decl bool getConfigurationDirectory(const IPropertyTree *dirtree, // NULL to use HPCC config
  209. const char *category,
  210. const char *component,
  211. const char *instance,
  212. StringBuffer &dirout);
  213. extern jlib_decl const char * matchConfigurationDirectoryEntry(const char *path,const char *mask,StringBuffer &name, StringBuffer &component, StringBuffer &instance);
  214. extern jlib_decl bool replaceConfigurationDirectoryEntry(const char *path,const char *frommask,const char *tomask,StringBuffer &out);
  215. extern jlib_decl const char *queryCurrentProcessName();
  216. extern jlib_decl int parseCommandLine(const char * cmdline, MemoryBuffer &mb, const char** &argvout); // parses cmdline into argvout returning arg count (mb used as buffer)
  217. extern jlib_decl bool safe_ecvt(size_t len, char * buffer, double value, int numDigits, int * decimal, int * sign);
  218. extern jlib_decl bool safe_fcvt(size_t len, char * buffer, double value, int numPlaces, int * decimal, int * sign);
  219. extern jlib_decl StringBuffer &getTempFilePath(StringBuffer & target, const char * component, IPropertyTree * pTree);
  220. #endif