Browse Source

avoid the use of "exotic" libc functions

pull/9/head
Roberto Ierusalimschy 17 years ago
parent
commit
ad60b3ead7
  1. 66
      lobject.c

66
lobject.c

@ -1,5 +1,5 @@
/* /*
** $Id: lobject.c,v 2.24 2006/11/22 11:02:03 roberto Exp roberto $ ** $Id: lobject.c,v 2.25 2007/04/10 12:18:17 roberto Exp roberto $
** Some generic functions over Lua objects ** Some generic functions over Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -161,36 +161,46 @@ const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
} }
#define LL(x) (sizeof(x) - 1)
#define RETS "..."
#define PRE "[string \""
#define POS "\"]"
#define addstr(a,b,l) ( memcpy(a,b,l), a += (l) )
void luaO_chunkid (char *out, const char *source, size_t bufflen) { void luaO_chunkid (char *out, const char *source, size_t bufflen) {
if (*source == '=') { size_t l = strlen(source);
strncpy(out, source+1, bufflen); /* remove first char */ if (*source == '=') { /* 'literal' source */
out[bufflen-1] = '\0'; /* ensures null termination */ if (l <= bufflen) /* small enough? */
memcpy(out, source + 1, l);
else { /* truncate it */
addstr(out, source + 1, bufflen - 1);
*out = '\0';
}
} }
else { /* out = "source", or "...source" */ else if (*source == '@') { /* file name */
if (*source == '@') { if (l <= bufflen) /* small enough? */
size_t l; memcpy(out, source + 1, l);
source++; /* skip the `@' */ else { /* add '...' before rest of name */
bufflen -= sizeof(" '...' "); addstr(out, RETS, LL(RETS));
l = strlen(source); bufflen -= LL(RETS);
strcpy(out, ""); memcpy(out, source + 1 + l - bufflen, bufflen);
if (l > bufflen) {
source += (l-bufflen); /* get last part of file name */
strcat(out, "...");
}
strcat(out, source);
} }
else { /* out = [string "string"] */ }
size_t len = strcspn(source, "\n\r"); /* stop at first newline */ else { /* string; format as [string "source"] */
bufflen -= sizeof(" [string \"...\"] "); const char *nl = strchr(source, '\n'); /* find first new line (if any) */
if (len > bufflen) len = bufflen; addstr(out, PRE, LL(PRE)); /* add prefix */
strcpy(out, "[string \""); bufflen -= LL(PRE RETS POS); /* save space for prefix+sufix */
if (source[len] != '\0') { /* must truncate? */ if (l < bufflen && nl == NULL) { /* small one-line source? */
strncat(out, source, len); addstr(out, source, l); /* keep it */
strcat(out, "..."); }
} else {
else if (nl != NULL) l = nl - source; /* stop at first newline */
strcat(out, source); if (l > bufflen) l = bufflen;
strcat(out, "\"]"); addstr(out, source, l);
addstr(out, RETS, LL(RETS));
} }
memcpy(out, POS, LL(POS) + 1);
} }
} }

Loading…
Cancel
Save