lobject.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. ** $Id: lobject.c,v 1.69 2001/03/07 12:27:06 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. #define LUA_PRIVATE
  12. #include "lua.h"
  13. #include "ldo.h"
  14. #include "lmem.h"
  15. #include "lobject.h"
  16. #include "lstate.h"
  17. const TObject luaO_nilobject = {LUA_TNIL, {NULL}};
  18. int luaO_equalObj (const TObject *t1, const TObject *t2) {
  19. if (ttype(t1) != ttype(t2)) return 0;
  20. switch (ttype(t1)) {
  21. case LUA_TNUMBER:
  22. return nvalue(t1) == nvalue(t2);
  23. case LUA_TNIL:
  24. return 1;
  25. default: /* all other types are equal if pointers are equal */
  26. return tsvalue(t1) == tsvalue(t2);
  27. }
  28. }
  29. void *luaO_openspaceaux (lua_State *L, size_t n) {
  30. if (n > G(L)->Mbuffsize) {
  31. G(L)->Mbuffer = luaM_realloc(L, G(L)->Mbuffer, G(L)->Mbuffsize, n);
  32. G(L)->Mbuffsize = n;
  33. }
  34. return G(L)->Mbuffer;
  35. }
  36. int luaO_str2d (const l_char *s, lua_Number *result) {
  37. l_char *endptr;
  38. lua_Number res = lua_str2number(s, &endptr);
  39. if (endptr == s) return 0; /* no conversion */
  40. while (isspace(uchar(*endptr))) endptr++;
  41. if (*endptr != l_c('\0')) return 0; /* invalid trailing characters? */
  42. *result = res;
  43. return 1;
  44. }
  45. /* maximum length of a string format for `luaO_verror' */
  46. #define MAX_VERROR 280
  47. /* this function needs to handle only '%d' and '%.XXs' formats */
  48. void luaO_verror (lua_State *L, const l_char *fmt, ...) {
  49. va_list argp;
  50. l_char buff[MAX_VERROR]; /* to hold formatted message */
  51. va_start(argp, fmt);
  52. vsprintf(buff, fmt, argp);
  53. va_end(argp);
  54. luaD_error(L, buff);
  55. }
  56. void luaO_chunkid (l_char *out, const l_char *source, int bufflen) {
  57. if (*source == l_c('=')) {
  58. strncpy(out, source+1, bufflen); /* remove first char */
  59. out[bufflen-1] = l_c('\0'); /* ensures null termination */
  60. }
  61. else {
  62. if (*source == l_c('@')) {
  63. int l;
  64. source++; /* skip the `@' */
  65. bufflen -= sizeof(l_s("file `...%s'"));
  66. l = strlen(source);
  67. if (l>bufflen) {
  68. source += (l-bufflen); /* get last part of file name */
  69. sprintf(out, l_s("file `...%.99s'"), source);
  70. }
  71. else
  72. sprintf(out, l_s("file `%.99s'"), source);
  73. }
  74. else {
  75. int len = strcspn(source, l_s("\n")); /* stop at first newline */
  76. bufflen -= sizeof(l_s("string \"%.*s...\""));
  77. if (len > bufflen) len = bufflen;
  78. if (source[len] != l_c('\0')) { /* must truncate? */
  79. strcpy(out, l_s("string \""));
  80. out += strlen(out);
  81. strncpy(out, source, len);
  82. strcpy(out+len, l_s("...\""));
  83. }
  84. else
  85. sprintf(out, l_s("string \"%.99s\""), source);
  86. }
  87. }
  88. }