lobject.c 2.7 KB

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