strlib.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. ** strlib.c
  3. ** String library to LUA
  4. */
  5. char *rcs_strlib="$Id: strlib.c,v 1.13 1995/10/09 12:49:21 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. static char *newstring (char *s)
  33. {
  34. char *ns = (char *)malloc(strlen(s)+1);
  35. if (ns == 0)
  36. lua_error("not enough memory for new string");
  37. strcpy(ns, s);
  38. return ns;
  39. }
  40. /*
  41. ** Return the position of the first caracter of a substring into a string
  42. ** LUA interface:
  43. ** n = strfind (string, substring, init, end)
  44. */
  45. static void str_find (void)
  46. {
  47. char *s1 = lua_check_string(1, "strfind");
  48. char *s2 = lua_check_string(2, "strfind");
  49. int init = (lua_getparam(3) == LUA_NOOBJECT) ? 0 :
  50. (int)lua_check_number(3, "strfind")-1;
  51. char *f = strstr(s1+init,s2);
  52. if (f != NULL)
  53. {
  54. int pos = f-s1+1;
  55. if (lua_getparam (4) == LUA_NOOBJECT)
  56. lua_pushnumber (pos);
  57. else if ((int)lua_check_number(4, "strfind") >= pos+strlen(s2)-1)
  58. lua_pushnumber (pos);
  59. else
  60. lua_pushnil();
  61. }
  62. else
  63. lua_pushnil();
  64. }
  65. /*
  66. ** Return the string length
  67. ** LUA interface:
  68. ** n = strlen (string)
  69. */
  70. static void str_len (void)
  71. {
  72. char *s = lua_check_string(1, "strlen");
  73. lua_pushnumber(strlen(s));
  74. }
  75. /*
  76. ** Return the substring of a string, from start to end
  77. ** LUA interface:
  78. ** substring = strsub (string, start, end)
  79. */
  80. static void str_sub (void)
  81. {
  82. char *s = lua_check_string(1, "strsub");
  83. int start = (int)lua_check_number(2, "strsub");
  84. int end = (lua_getparam(3) == LUA_NOOBJECT) ? strlen(s) :
  85. (int)lua_check_number(3, "strsub");
  86. if (end < start || start < 1 || end > strlen(s))
  87. lua_pushliteral("");
  88. else
  89. {
  90. char temp = s[end];
  91. s[end] = 0;
  92. lua_pushstring (&s[start-1]);
  93. s[end] = temp;
  94. }
  95. }
  96. /*
  97. ** Convert a string according to given function.
  98. */
  99. typedef int (*strfunc)(int s);
  100. static void str_apply (strfunc f, char *funcname)
  101. {
  102. char *s, *c;
  103. c = s = newstring(lua_check_string(1, funcname));
  104. while (*c != 0)
  105. {
  106. *c = f(*c);
  107. c++;
  108. }
  109. lua_pushstring(s);
  110. free(s);
  111. }
  112. /*
  113. ** Convert a string to lower case.
  114. ** LUA interface:
  115. ** lowercase = strlower (string)
  116. */
  117. static void str_lower (void)
  118. {
  119. str_apply(tolower, "strlower");
  120. }
  121. /*
  122. ** Convert a string to upper case.
  123. ** LUA interface:
  124. ** uppercase = strupper (string)
  125. */
  126. static void str_upper (void)
  127. {
  128. str_apply(toupper, "strupper");
  129. }
  130. /*
  131. ** get ascii value of a character in a string
  132. */
  133. static void str_ascii (void)
  134. {
  135. char *s = lua_check_string(1, "ascii");
  136. lua_Object o2 = lua_getparam(2);
  137. int pos;
  138. pos = (o2 == LUA_NOOBJECT) ? 0 : (int)lua_check_number(2, "ascii")-1;
  139. if (pos<0 || pos>=strlen(s))
  140. lua_arg_error("ascii");
  141. lua_pushnumber(s[pos]);
  142. }
  143. /*
  144. ** converts one or more integers to chars in a string
  145. */
  146. #define maxparams 50
  147. static void str_int2str (void)
  148. {
  149. char s[maxparams+1];
  150. int i = 0;
  151. while (lua_getparam(++i) != LUA_NOOBJECT)
  152. {
  153. if (i > maxparams)
  154. lua_error("too many parameters to function `int2str'");
  155. s[i-1] = (int)lua_check_number(i, "int2str");
  156. }
  157. s[i-1] = 0;
  158. lua_pushstring(s);
  159. }
  160. /*
  161. ** Open string library
  162. */
  163. void strlib_open (void)
  164. {
  165. lua_register ("strfind", str_find);
  166. lua_register ("strlen", str_len);
  167. lua_register ("strsub", str_sub);
  168. lua_register ("strlower", str_lower);
  169. lua_register ("strupper", str_upper);
  170. lua_register ("ascii", str_ascii);
  171. lua_register ("int2str", str_int2str);
  172. }