strlib.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. ** strlib.c
  3. ** String library to LUA
  4. */
  5. char *rcs_strlib="$Id: strlib.c,v 1.15 1996/01/22 17:38:57 roberto Exp roberto $";
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <ctype.h>
  10. #include "lua.h"
  11. #include "lualib.h"
  12. void lua_arg_error(char *funcname)
  13. {
  14. char buff[100];
  15. sprintf(buff, "incorrect arguments to function `%s'", funcname);
  16. lua_error(buff);
  17. }
  18. char *lua_check_string (int numArg, char *funcname)
  19. {
  20. lua_Object o = lua_getparam(numArg);
  21. if (!(lua_isstring(o) || lua_isnumber(o)))
  22. lua_arg_error(funcname);
  23. return lua_getstring(o);
  24. }
  25. float lua_check_number (int numArg, char *funcname)
  26. {
  27. lua_Object o = lua_getparam(numArg);
  28. if (!lua_isnumber(o))
  29. lua_arg_error(funcname);
  30. return lua_getnumber(o);
  31. }
  32. char *luaI_addchar (int c)
  33. {
  34. static char *buff = NULL;
  35. static int max = 0;
  36. static int n = 0;
  37. if (n >= max)
  38. {
  39. if (max == 0)
  40. {
  41. max = 100;
  42. buff = (char *)malloc(max);
  43. }
  44. else
  45. {
  46. max *= 2;
  47. buff = (char *)realloc(buff, max);
  48. }
  49. if (buff == NULL)
  50. lua_error("memory overflow");
  51. }
  52. buff[n++] = c;
  53. if (c == 0)
  54. n = 0; /* prepare for next string */
  55. return buff;
  56. }
  57. /*
  58. ** Return the position of the first caracter of a substring into a string
  59. ** LUA interface:
  60. ** n = strfind (string, substring, init, end)
  61. */
  62. static void str_find (void)
  63. {
  64. char *s1 = lua_check_string(1, "strfind");
  65. char *s2 = lua_check_string(2, "strfind");
  66. int init = (lua_getparam(3) == LUA_NOOBJECT) ? 0 :
  67. (int)lua_check_number(3, "strfind")-1;
  68. char *f = strstr(s1+init,s2);
  69. if (f != NULL)
  70. {
  71. int pos = f-s1+1;
  72. if (lua_getparam (4) == LUA_NOOBJECT)
  73. lua_pushnumber (pos);
  74. else if ((int)lua_check_number(4, "strfind") >= pos+strlen(s2)-1)
  75. lua_pushnumber (pos);
  76. else
  77. lua_pushnil();
  78. }
  79. else
  80. lua_pushnil();
  81. }
  82. /*
  83. ** Return the string length
  84. ** LUA interface:
  85. ** n = strlen (string)
  86. */
  87. static void str_len (void)
  88. {
  89. char *s = lua_check_string(1, "strlen");
  90. lua_pushnumber(strlen(s));
  91. }
  92. /*
  93. ** Return the substring of a string, from start to end
  94. ** LUA interface:
  95. ** substring = strsub (string, start, end)
  96. */
  97. static void str_sub (void)
  98. {
  99. char *s = lua_check_string(1, "strsub");
  100. int start = (int)lua_check_number(2, "strsub");
  101. int end = (lua_getparam(3) == LUA_NOOBJECT) ? strlen(s) :
  102. (int)lua_check_number(3, "strsub");
  103. if (end < start || start < 1 || end > strlen(s))
  104. lua_pushliteral("");
  105. else
  106. {
  107. char temp = s[end];
  108. s[end] = 0;
  109. lua_pushstring (&s[start-1]);
  110. s[end] = temp;
  111. }
  112. }
  113. /*
  114. ** Convert a string to lower case.
  115. ** LUA interface:
  116. ** lowercase = strlower (string)
  117. */
  118. static void str_lower (void)
  119. {
  120. char *s = lua_check_string(1, "strlower");
  121. luaI_addchar(0);
  122. while (*s)
  123. luaI_addchar(tolower(*s++));
  124. lua_pushstring(luaI_addchar(0));
  125. }
  126. /*
  127. ** Convert a string to upper case.
  128. ** LUA interface:
  129. ** uppercase = strupper (string)
  130. */
  131. static void str_upper (void)
  132. {
  133. char *s = lua_check_string(1, "strupper");
  134. luaI_addchar(0);
  135. while (*s)
  136. luaI_addchar(toupper(*s++));
  137. lua_pushstring(luaI_addchar(0));
  138. }
  139. /*
  140. ** get ascii value of a character in a string
  141. */
  142. static void str_ascii (void)
  143. {
  144. char *s = lua_check_string(1, "ascii");
  145. lua_Object o2 = lua_getparam(2);
  146. int pos;
  147. pos = (o2 == LUA_NOOBJECT) ? 0 : (int)lua_check_number(2, "ascii")-1;
  148. if (pos<0 || pos>=strlen(s))
  149. lua_arg_error("ascii");
  150. lua_pushnumber(s[pos]);
  151. }
  152. #define MAX_CONVERTION 2000
  153. #define MAX_FORMAT 50
  154. static void io_format (void)
  155. {
  156. int arg = 1;
  157. char *strfrmt = lua_check_string(arg++, "format");
  158. luaI_addchar(0); /* initialize */
  159. while (*strfrmt)
  160. {
  161. if (*strfrmt != '%')
  162. luaI_addchar(*strfrmt++);
  163. else if (*++strfrmt == '%')
  164. luaI_addchar(*strfrmt++); /* %% */
  165. else
  166. { /* format item */
  167. char form[MAX_FORMAT]; /* store the format ('%...') */
  168. char buff[MAX_CONVERTION]; /* store the formated value */
  169. int size = 0;
  170. int i = 0;
  171. form[i++] = '%';
  172. form[i] = *strfrmt++;
  173. while (!isalpha(form[i]))
  174. {
  175. if (isdigit(form[i]))
  176. {
  177. size = size*10 + form[i]-'0';
  178. if (size >= MAX_CONVERTION)
  179. lua_error("format size/precision too long in function `format'");
  180. }
  181. else if (form[i] == '.')
  182. size = 0; /* re-start */
  183. if (++i >= MAX_FORMAT)
  184. lua_error("bad format in function `format'");
  185. form[i] = *strfrmt++;
  186. }
  187. form[i+1] = 0; /* ends string */
  188. switch (form[i])
  189. {
  190. case 's':
  191. {
  192. char *s = lua_check_string(arg++, "format");
  193. if (strlen(s) >= MAX_CONVERTION)
  194. lua_error("string argument too long in function `format'");
  195. sprintf(buff, form, s);
  196. break;
  197. }
  198. case 'c': case 'd': case 'i': case 'o':
  199. case 'u': case 'x': case 'X':
  200. sprintf(buff, form, (int)lua_check_number(arg++, "format"));
  201. break;
  202. case 'e': case 'E': case 'f': case 'g':
  203. sprintf(buff, form, lua_check_number(arg++, "format"));
  204. break;
  205. default: /* also treat cases 'pnLlh' */
  206. lua_error("invalid format option in function `format'");
  207. }
  208. for (i=0; buff[i]; i++) /* move formated value to result */
  209. luaI_addchar(buff[i]);
  210. }
  211. }
  212. lua_pushstring(luaI_addchar(0)); /* push the result */
  213. }
  214. /*
  215. ** Open string library
  216. */
  217. void strlib_open (void)
  218. {
  219. lua_register ("strfind", str_find);
  220. lua_register ("strlen", str_len);
  221. lua_register ("strsub", str_sub);
  222. lua_register ("strlower", str_lower);
  223. lua_register ("strupper", str_upper);
  224. lua_register ("ascii", str_ascii);
  225. lua_register ("format", io_format);
  226. }