utils.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #ifndef COMMON_UTILS_H
  2. #define COMMON_UTILS_H
  3. #ifndef NULL
  4. #define NULL (void *)0
  5. #endif
  6. #define LOWER(c) (((c)>='A' && (c) <= 'Z') ? ((c)+('a'-'A')) : (c))
  7. #define UPPER(c) (((c)>='a' && (c) <= 'z') ? ((c)+('A'-'a')) : (c) )
  8. /* strcasecmp -> stricmp -> str_cmp */
  9. #ifdef _WIN32
  10. int strcasecmp(const char *arg1, const char *arg2);
  11. int strncasecmp(const char *arg1, const char *arg2, int n);
  12. void str_upper(char *name);
  13. void str_lower(char *name);
  14. char *rindex(char *str, char c);
  15. #endif
  16. void dump(unsigned char *buffer, int num);
  17. #define CREATE(result, type, number) do {\
  18. if ((number) * sizeof(type) <= 0) \
  19. printf("SYSERR: Zero bytes or less requested at %s:%d.\n", __FILE__, __LINE__); \
  20. if (!((result) = (type *) calloc ((number), sizeof(type)))) \
  21. { perror("SYSERR: malloc failure"); abort(); } } while(0)
  22. #define RECREATE(result,type,number) do {\
  23. if (!((result) = (type *) realloc ((result), sizeof(type) * (number))))\
  24. { printf("SYSERR: realloc failure"); abort(); } } while(0)
  25. struct StringBuf {
  26. char *buf_;
  27. char *ptr_;
  28. unsigned int max_;
  29. };
  30. extern struct StringBuf * StringBuf_Malloc();
  31. extern void StringBuf_Init(struct StringBuf *);
  32. extern int StringBuf_Printf(struct StringBuf *,const char *,...);
  33. extern int StringBuf_Append(struct StringBuf *,const struct StringBuf *);
  34. extern char * StringBuf_Value(struct StringBuf *);
  35. extern void StringBuf_Destroy(struct StringBuf *);
  36. extern void StringBuf_Free(struct StringBuf *);
  37. #endif