strlib.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. ** strlib.c
  3. ** String library to LUA
  4. */
  5. char *rcs_strlib="$Id: strlib.c,v 1.19 1996/03/14 15:52:35 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))
  22. lua_arg_error(funcname);
  23. return lua_getstring(o);
  24. }
  25. double 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. luaI_addchar(0);
  108. while (start <= end)
  109. luaI_addchar(s[start++ - 1]);
  110. lua_pushstring (luaI_addchar(0));
  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. 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. }