lobject.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. ** $Id: lobject.c,v 1.62 2001/01/26 14:16:35 roberto Exp roberto $
  3. ** Some generic functions over Lua objects
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <stdarg.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "lua.h"
  12. #include "ldo.h"
  13. #include "lmem.h"
  14. #include "lobject.h"
  15. #include "lstate.h"
  16. const char luaO_ttnil = LUA_TNIL;
  17. const TObject luaO_nilobject = {LUA_TNIL, {(void *)&luaO_ttnil}};
  18. /*
  19. ** returns smaller power of 2 larger than `n' (minimum is MINPOWER2)
  20. */
  21. luint32 luaO_power2 (luint32 n) {
  22. luint32 p = MINPOWER2;
  23. while (p<=n) p<<=1;
  24. return p;
  25. }
  26. int luaO_equalObj (const TObject *t1, const TObject *t2) {
  27. if (ttype(t1) != ttype(t2)) return 0;
  28. switch (ttype(t1)) {
  29. case LUA_TNUMBER:
  30. return nvalue(t1) == nvalue(t2);
  31. case LUA_TNIL:
  32. return 1;
  33. default: /* all other types are equal if pointers are equal */
  34. return tsvalue(t1) == tsvalue(t2);
  35. }
  36. }
  37. char *luaO_openspace (lua_State *L, size_t n) {
  38. if (n > G(L)->Mbuffsize) {
  39. luaM_reallocvector(L, G(L)->Mbuffer, G(L)->Mbuffsize, n, char);
  40. G(L)->Mbuffsize = n;
  41. }
  42. return G(L)->Mbuffer;
  43. }
  44. int luaO_str2d (const char *s, lua_Number *result) { /* LUA_NUMBER */
  45. char *endptr;
  46. lua_Number res = lua_str2number(s, &endptr);
  47. if (endptr == s) return 0; /* no conversion */
  48. while (isspace((unsigned char)*endptr)) endptr++;
  49. if (*endptr != '\0') return 0; /* invalid trailing characters? */
  50. *result = res;
  51. return 1;
  52. }
  53. /* maximum length of a string format for `luaO_verror' */
  54. #define MAX_VERROR 280
  55. /* this function needs to handle only '%d' and '%.XXs' formats */
  56. void luaO_verror (lua_State *L, const char *fmt, ...) {
  57. va_list argp;
  58. char buff[MAX_VERROR]; /* to hold formatted message */
  59. va_start(argp, fmt);
  60. vsprintf(buff, fmt, argp);
  61. va_end(argp);
  62. luaD_error(L, buff);
  63. }
  64. void luaO_chunkid (char *out, const char *source, int bufflen) {
  65. if (*source == '=') {
  66. strncpy(out, source+1, bufflen); /* remove first char */
  67. out[bufflen-1] = '\0'; /* ensures null termination */
  68. }
  69. else {
  70. if (*source == '@') {
  71. int l;
  72. source++; /* skip the `@' */
  73. bufflen -= sizeof("file `...%s'");
  74. l = strlen(source);
  75. if (l>bufflen) {
  76. source += (l-bufflen); /* get last part of file name */
  77. sprintf(out, "file `...%.99s'", source);
  78. }
  79. else
  80. sprintf(out, "file `%.99s'", source);
  81. }
  82. else {
  83. int len = strcspn(source, "\n"); /* stop at first newline */
  84. bufflen -= sizeof("string \"%.*s...\"");
  85. if (len > bufflen) len = bufflen;
  86. if (source[len] != '\0') { /* must truncate? */
  87. strcpy(out, "string \"");
  88. out += strlen(out);
  89. strncpy(out, source, len);
  90. strcpy(out+len, "...\"");
  91. }
  92. else
  93. sprintf(out, "string \"%.99s\"", source);
  94. }
  95. }
  96. }