strlib.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. ** strlib.c
  3. ** String library to LUA
  4. **
  5. ** Waldemar Celes Filho
  6. ** TeCGraf - PUC-Rio
  7. ** 19 May 93
  8. */
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <ctype.h>
  12. #include "lua.h"
  13. /*
  14. ** Return the position of the first caracter of a substring into a string
  15. ** LUA interface:
  16. ** n = strfind (string, substring)
  17. */
  18. static void str_find (void)
  19. {
  20. int n;
  21. char *s1, *s2;
  22. lua_Object o1 = lua_getparam (1);
  23. lua_Object o2 = lua_getparam (2);
  24. if (!lua_isstring(o1) || !lua_isstring(o2))
  25. { lua_error ("incorrect arguments to function `strfind'"); return; }
  26. s1 = lua_getstring(o1);
  27. s2 = lua_getstring(o2);
  28. n = strstr(s1,s2) - s1 + 1;
  29. lua_pushnumber (n);
  30. }
  31. /*
  32. ** Return the string length
  33. ** LUA interface:
  34. ** n = strlen (string)
  35. */
  36. static void str_len (void)
  37. {
  38. lua_Object o = lua_getparam (1);
  39. if (!lua_isstring(o))
  40. { lua_error ("incorrect arguments to function `strlen'"); return; }
  41. lua_pushnumber(strlen(lua_getstring(o)));
  42. }
  43. /*
  44. ** Return the substring of a string, from start to end
  45. ** LUA interface:
  46. ** substring = strsub (string, start, end)
  47. */
  48. static void str_sub (void)
  49. {
  50. int start, end;
  51. char *s;
  52. lua_Object o1 = lua_getparam (1);
  53. lua_Object o2 = lua_getparam (2);
  54. lua_Object o3 = lua_getparam (3);
  55. if (!lua_isstring(o1) || !lua_isnumber(o2) || !lua_isnumber(o3))
  56. { lua_error ("incorrect arguments to function `strsub'"); return; }
  57. s = strdup (lua_getstring(o1));
  58. start = lua_getnumber (o2);
  59. end = lua_getnumber (o3);
  60. if (end < start || start < 1 || end > strlen(s))
  61. lua_pushstring ("");
  62. else
  63. {
  64. s[end] = 0;
  65. lua_pushstring (&s[start-1]);
  66. }
  67. free (s);
  68. }
  69. /*
  70. ** Convert a string to lower case.
  71. ** LUA interface:
  72. ** lowercase = strlower (string)
  73. */
  74. static void str_lower (void)
  75. {
  76. char *s, *c;
  77. lua_Object o = lua_getparam (1);
  78. if (!lua_isstring(o))
  79. { lua_error ("incorrect arguments to function `strlower'"); return; }
  80. c = s = strdup(lua_getstring(o));
  81. while (*c != 0)
  82. {
  83. *c = tolower(*c);
  84. c++;
  85. }
  86. lua_pushstring(s);
  87. free(s);
  88. }
  89. /*
  90. ** Convert a string to upper case.
  91. ** LUA interface:
  92. ** uppercase = strupper (string)
  93. */
  94. static void str_upper (void)
  95. {
  96. char *s, *c;
  97. lua_Object o = lua_getparam (1);
  98. if (!lua_isstring(o))
  99. { lua_error ("incorrect arguments to function `strlower'"); return; }
  100. c = s = strdup(lua_getstring(o));
  101. while (*c != 0)
  102. {
  103. *c = toupper(*c);
  104. c++;
  105. }
  106. lua_pushstring(s);
  107. free(s);
  108. }
  109. /*
  110. ** Open string library
  111. */
  112. void strlib_open (void)
  113. {
  114. lua_register ("strfind", str_find);
  115. lua_register ("strlen", str_len);
  116. lua_register ("strsub", str_sub);
  117. lua_register ("strlower", str_lower);
  118. lua_register ("strupper", str_upper);
  119. }