~ubuntu-branches/ubuntu/edgy/enigma/edgy

« back to all changes in this revision

Viewing changes to lib-src/lua/lobject.c

  • Committer: Bazaar Package Importer
  • Author(s): Erich Schubert
  • Date: 2005-08-28 15:30:09 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050828153009-sky64kb6tcq37xt5
Tags: 0.92.1-1
* New upstream subversion checkout
* Remove menu.s3m, which we are allowed to distributed but not to modify
  also copyright notice is confusing... (Closes: #321669)
* Rebuild with new libzipios (Closes: #325405)
  I hope this works without a versioned build-dependency
* Added "enigma replaces enigma-data" for upgrades (Closes: #308558)
* Added notes about the fonts copyright.
* updated to policy 3.6.2.1 (no changes)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
** $Id: lobject.c,v 1.1 2003/02/09 21:30:32 dheck Exp $
3
 
** Some generic functions over Lua objects
4
 
** See Copyright Notice in lua.h
5
 
*/
6
 
 
7
 
#include <ctype.h>
8
 
#include <stdarg.h>
9
 
#include <stdio.h>
10
 
#include <stdlib.h>
11
 
#include <string.h>
12
 
 
13
 
#include "lua.h"
14
 
 
15
 
#include "lmem.h"
16
 
#include "lobject.h"
17
 
#include "lstate.h"
18
 
 
19
 
 
20
 
 
21
 
const TObject luaO_nilobject = {LUA_TNIL, {NULL}};
22
 
 
23
 
 
24
 
const char *const luaO_typenames[] = {
25
 
  "userdata", "nil", "number", "string", "table", "function"
26
 
};
27
 
 
28
 
 
29
 
 
30
 
/*
31
 
** returns smaller power of 2 larger than `n' (minimum is MINPOWER2) 
32
 
*/
33
 
lint32 luaO_power2 (lint32 n) {
34
 
  lint32 p = MINPOWER2;
35
 
  while (p<=n) p<<=1;
36
 
  return p;
37
 
}
38
 
 
39
 
 
40
 
int luaO_equalObj (const TObject *t1, const TObject *t2) {
41
 
  if (ttype(t1) != ttype(t2)) return 0;
42
 
  switch (ttype(t1)) {
43
 
    case LUA_TNUMBER:
44
 
      return nvalue(t1) == nvalue(t2);
45
 
    case LUA_TSTRING: case LUA_TUSERDATA:
46
 
      return tsvalue(t1) == tsvalue(t2);
47
 
    case LUA_TTABLE: 
48
 
      return hvalue(t1) == hvalue(t2);
49
 
    case LUA_TFUNCTION:
50
 
      return clvalue(t1) == clvalue(t2);
51
 
    default:
52
 
      LUA_ASSERT(ttype(t1) == LUA_TNIL, "invalid type");
53
 
      return 1; /* LUA_TNIL */
54
 
  }
55
 
}
56
 
 
57
 
 
58
 
char *luaO_openspace (lua_State *L, size_t n) {
59
 
  if (n > L->Mbuffsize) {
60
 
    luaM_reallocvector(L, L->Mbuffer, n, char);
61
 
    L->nblocks += (n - L->Mbuffsize)*sizeof(char);
62
 
    L->Mbuffsize = n;
63
 
  }
64
 
  return L->Mbuffer;
65
 
}
66
 
 
67
 
 
68
 
int luaO_str2d (const char *s, Number *result) {  /* LUA_NUMBER */
69
 
  char *endptr;
70
 
  Number res = lua_str2number(s, &endptr);
71
 
  if (endptr == s) return 0;  /* no conversion */
72
 
  while (isspace((unsigned char)*endptr)) endptr++;
73
 
  if (*endptr != '\0') return 0;  /* invalid trailing characters? */
74
 
  *result = res;
75
 
  return 1;
76
 
}
77
 
 
78
 
 
79
 
/* maximum length of a string format for `luaO_verror' */
80
 
#define MAX_VERROR      280
81
 
 
82
 
/* this function needs to handle only '%d' and '%.XXs' formats */
83
 
void luaO_verror (lua_State *L, const char *fmt, ...) {
84
 
  va_list argp;
85
 
  char buff[MAX_VERROR];  /* to hold formatted message */
86
 
  va_start(argp, fmt);
87
 
  vsprintf(buff, fmt, argp);
88
 
  va_end(argp);
89
 
  lua_error(L, buff);
90
 
}
91
 
 
92
 
 
93
 
void luaO_chunkid (char *out, const char *source, int bufflen) {
94
 
  if (*source == '=') {
95
 
    strncpy(out, source+1, bufflen);  /* remove first char */
96
 
    out[bufflen-1] = '\0';  /* ensures null termination */
97
 
  }
98
 
  else {
99
 
    if (*source == '@') {
100
 
      int l;
101
 
      source++;  /* skip the `@' */
102
 
      bufflen -= sizeof("file `...%s'");
103
 
      l = strlen(source);
104
 
      if (l>bufflen) {
105
 
        source += (l-bufflen);  /* get last part of file name */
106
 
        sprintf(out, "file `...%.99s'", source);
107
 
      }
108
 
      else
109
 
        sprintf(out, "file `%.99s'", source);
110
 
    }
111
 
    else {
112
 
      int len = strcspn(source, "\n");  /* stop at first newline */
113
 
      bufflen -= sizeof("string \"%.*s...\"");
114
 
      if (len > bufflen) len = bufflen;
115
 
      if (source[len] != '\0') {  /* must truncate? */
116
 
        strcpy(out, "string \"");
117
 
        out += strlen(out);
118
 
        strncpy(out, source, len);
119
 
        strcpy(out+len, "...\"");
120
 
      }
121
 
      else
122
 
        sprintf(out, "string \"%.99s\"", source);
123
 
    }
124
 
  }
125
 
}