strlib.c 5.9 KB

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