utils.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include <string.h>
  2. #include "utils.h"
  3. #include <stdio.h>
  4. #include <stdarg.h>
  5. #include <stdlib.h>
  6. #include "malloc.h"
  7. #include "mmo.h"
  8. void dump(unsigned char *buffer, int num)
  9. {
  10. int icnt,jcnt;
  11. printf(" Hex ASCII\n");
  12. printf(" ----------------------------------------------- ----------------");
  13. for (icnt=0;icnt<num;icnt+=16) {
  14. printf("\n%p ",&buffer[icnt]);
  15. for (jcnt=icnt;jcnt<icnt+16;++jcnt) {
  16. if (jcnt < num) {
  17. printf("%02hX ",buffer[jcnt]);
  18. } else
  19. printf(" ");
  20. }
  21. printf(" | ");
  22. for (jcnt=icnt;jcnt<icnt+16;++jcnt) {
  23. if (jcnt < num) {
  24. if (buffer[jcnt] > 31 && buffer[jcnt] < 127)
  25. printf("%c",buffer[jcnt]);
  26. else
  27. printf(".");
  28. } else
  29. printf(" ");
  30. }
  31. }
  32. printf("\n");
  33. }
  34. #ifdef _WIN32
  35. char *rindex(char *str, char c)
  36. {
  37. char *sptr;
  38. sptr = str;
  39. while(*sptr)
  40. ++sptr;
  41. if (c == '\0')
  42. return(sptr);
  43. while(str != sptr)
  44. if (*sptr-- == c)
  45. return(++sptr);
  46. return(NULL);
  47. }
  48. int strcasecmp(const char *arg1, const char *arg2)
  49. {
  50. int chk, i;
  51. if (arg1 == NULL || arg2 == NULL) {
  52. printf("SYSERR: str_cmp() passed a NULL pointer, %p or %p.\n", arg1, arg2);
  53. return (0);
  54. }
  55. for (i = 0; arg1[i] || arg2[i]; i++)
  56. if ((chk = LOWER(arg1[i]) - LOWER(arg2[i])) != 0)
  57. return (chk); /* not equal */
  58. return (0);
  59. }
  60. int strncasecmp(const char *arg1, const char *arg2, int n)
  61. {
  62. int chk, i;
  63. if (arg1 == NULL || arg2 == NULL) {
  64. printf("SYSERR: strn_cmp() passed a NULL pointer, %p or %p.\n", arg1, arg2);
  65. return (0);
  66. }
  67. for (i = 0; (arg1[i] || arg2[i]) && (n > 0); i++, n--)
  68. if ((chk = LOWER(arg1[i]) - LOWER(arg2[i])) != 0)
  69. return (chk); /* not equal */
  70. return (0);
  71. }
  72. void str_upper(char *name)
  73. {
  74. int len = strlen(name);
  75. while (len--) {
  76. if (*name >= 'a' && *name <= 'z')
  77. *name -= ('a' - 'A');
  78. name++;
  79. }
  80. }
  81. void str_lower(char *name)
  82. {
  83. int len = strlen(name);
  84. while (len--) {
  85. if (*name >= 'A' && *name <= 'Z')
  86. *name += ('a' - 'A');
  87. name++;
  88. }
  89. }
  90. #endif
  91. // Allocate a StringBuf [MouseJstr]
  92. struct StringBuf * StringBuf_Malloc()
  93. {
  94. struct StringBuf * ret = (struct StringBuf *) aMallocA(sizeof(struct StringBuf));
  95. StringBuf_Init(ret);
  96. return ret;
  97. }
  98. // Initialize a previously allocated StringBuf [MouseJstr]
  99. void StringBuf_Init(struct StringBuf * sbuf) {
  100. sbuf->max_ = 1024;
  101. sbuf->ptr_ = sbuf->buf_ = (char *) aMallocA(sbuf->max_ + 1);
  102. }
  103. // printf into a StringBuf, moving the pointer [MouseJstr]
  104. int StringBuf_Printf(struct StringBuf *sbuf,const char *fmt,...)
  105. {
  106. va_list ap;
  107. int n, size, off;
  108. while (1) {
  109. /* Try to print in the allocated space. */
  110. va_start(ap, fmt);
  111. size = sbuf->max_ - (sbuf->ptr_ - sbuf->buf_);
  112. n = vsnprintf (sbuf->ptr_, size, fmt, ap);
  113. va_end(ap);
  114. /* If that worked, return the length. */
  115. if (n > -1 && n < size) {
  116. sbuf->ptr_ += n;
  117. return sbuf->ptr_ - sbuf->buf_;
  118. }
  119. /* Else try again with more space. */
  120. sbuf->max_ *= 2; // twice the old size
  121. off = sbuf->ptr_ - sbuf->buf_;
  122. sbuf->buf_ = (char *) aRealloc(sbuf->buf_, sbuf->max_ + 1);
  123. sbuf->ptr_ = sbuf->buf_ + off;
  124. }
  125. }
  126. // Append buf2 onto the end of buf1 [MouseJstr]
  127. int StringBuf_Append(struct StringBuf *buf1,const struct StringBuf *buf2)
  128. {
  129. int buf1_avail = buf1->max_ - (buf1->ptr_ - buf1->buf_);
  130. int size2 = buf2->ptr_ - buf2->buf_;
  131. if (size2 >= buf1_avail) {
  132. int off = buf1->ptr_ - buf1->buf_;
  133. buf1->max_ += size2;
  134. buf1->buf_ = (char *) aRealloc(buf1->buf_, buf1->max_ + 1);
  135. buf1->ptr_ = buf1->buf_ + off;
  136. }
  137. memcpy(buf1->ptr_, buf2->buf_, size2);
  138. buf1->ptr_ += size2;
  139. return buf1->ptr_ - buf1->buf_;
  140. }
  141. // Destroy a StringBuf [MouseJstr]
  142. void StringBuf_Destroy(struct StringBuf *sbuf)
  143. {
  144. aFree(sbuf->buf_);
  145. sbuf->ptr_ = sbuf->buf_ = 0;
  146. }
  147. // Free a StringBuf returned by StringBuf_Malloc [MouseJstr]
  148. void StringBuf_Free(struct StringBuf *sbuf)
  149. {
  150. StringBuf_Destroy(sbuf);
  151. aFree(sbuf);
  152. }
  153. // Return the built string from the StringBuf [MouseJstr]
  154. char * StringBuf_Value(struct StringBuf *sbuf)
  155. {
  156. *sbuf->ptr_ = '\0';
  157. return sbuf->buf_;
  158. }