private static int ReadLine(LuaState L, Stream f) { LuaLBuffer b = new LuaLBuffer(); LuaLBuffInit(L, b); for (;;) { uint l; CharPtr p = LuaLPrepBuffer(b); if (fgets(p, f) == null) /* eof? */ { LuaLPushResult(b); /* close buffer */ return((LuaObjectLen(L, -1) > 0) ? 1 : 0); /* check whether read something */ } l = (uint)strlen(p); if (l == 0 || p[l - 1] != '\n') { LuaLAddSize(b, (int)l); } else { LuaLAddSize(b, (int)(l - 1)); /* do not include `eol' */ LuaLPushResult(b); /* close buffer */ return(1); /* read at least an `eol' */ } } }
private static int str_gsub(LuaState L) { uint srcl; CharPtr src = LuaLCheckLString(L, 1, out srcl); CharPtr p = LuaLCheckString(L, 2); int tr = LuaType(L, 3); int max_s = LuaLOptInt(L, 4, (int)(srcl + 1)); int anchor = 0; if (p[0] == '^') { p = p.next(); anchor = 1; } int n = 0; MatchState ms = new MatchState(); LuaLBuffer b = new LuaLBuffer(); LuaLArgCheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || tr == LUA_TFUNCTION || tr == LUA_TTABLE || tr == LUA_TUSERDATA, 3, "string/function/table expected"); LuaLBuffInit(L, b); ms.L = L; ms.src_init = src; ms.src_end = src + srcl; while (n < max_s) { CharPtr e; ms.level = 0; e = match(ms, src, p); if (e != null) { n++; add_value(ms, b, src, e); } if ((e != null) && e > src) /* non empty match? */ { src = e; /* skip it */ } else if (src < ms.src_end) { char c = src[0]; src = src.next(); LuaLAddChar(b, c); } else { break; } if (anchor != 0) { break; } } LuaLAddLString(b, src, (uint)(ms.src_end - src)); LuaLPushResult(b); LuaPushInteger(L, n); /* number of substitutions */ return(2); }
public static void LuaLAddValue(LuaLBuffer B) { LuaState L = B.L; uint vl; CharPtr s = LuaToLString(L, -1, out vl); if (vl <= BufferFree(B)) /* fit into buffer? */ { CharPtr dst = new CharPtr(B.buffer.chars, B.buffer.index + B.p); CharPtr src = new CharPtr(s.chars, s.index); for (uint i = 0; i < vl; i++) { dst[i] = src[i]; } B.p += (int)vl; LuaPop(L, 1); /* remove from stack */ } else { if (EmptyBuffer(B) != 0) { LuaInsert(L, -2); /* put buffer before new value */ } B.lvl++; /* add new value into B stack */ AdjustStack(B); } }
private static void add_s(MatchState ms, LuaLBuffer b, CharPtr s, CharPtr e) { uint l, i; CharPtr news = LuaToLString(ms.L, 3, out l); for (i = 0; i < l; i++) { if (news[i] != L_ESC) { LuaLAddChar(b, news[i]); } else { i++; /* skip ESC */ if (!isdigit((char)(news[i]))) { if (news[i] != L_ESC) { LuaLError(ms.L, "invalid use of '%' in replacement string"); } LuaLAddChar(b, news[i]); } else if (news[i] == '0') { LuaLAddLString(b, s, (uint)(e - s)); } else { push_onecapture(ms, news[i] - '1', s, e); LuaLAddValue(b); /* add capture to accumulated result */ } } } }
private static void addquoted(LuaState L, LuaLBuffer b, int arg) { uint l; CharPtr s = LuaLCheckLString(L, arg, out l); LuaLAddChar(b, '"'); while ((l--) != 0) { switch (s[0]) { case '"': case '\\': case '\n': { LuaLAddChar(b, '\\'); LuaLAddChar(b, s[0]); break; } case '\r': { LuaLAddLString(b, "\\r", 2); break; } default: { if (s[0] < (char)16) { bool isfollowedbynum = false; if (l >= 1) { if (char.IsNumber(s[1])) { isfollowedbynum = true; } } if (isfollowedbynum) { LuaLAddString(b, string.Format("\\{0:000}", (int)s[0])); } else { LuaLAddString(b, string.Format("\\{0}", (int)s[0])); } } else { LuaLAddChar(b, s[0]); } break; } } s = s.next(); } LuaLAddChar(b, '"'); }
public static CharPtr LuaLPrepBuffer(LuaLBuffer B) { if (EmptyBuffer(B) != 0) { AdjustStack(B); } return(new CharPtr(B.buffer, B.p)); }
public static void LuaLAddChar(LuaLBuffer B, char c) { if (B.p >= LUAL_BUFFERSIZE) { LuaLPrepBuffer(B); } B.buffer[B.p++] = c; }
private static void addfield(LuaState L, LuaLBuffer b, int i) { LuaRawGetI(L, 1, i); if (LuaIsString(L, -1)==0) LuaLError(L, "invalid value (%s) at index %d in table for " + LUA_QL("concat"), LuaLTypeName(L, -1), i); LuaLAddValue(b); }
public static void LuaLAddLString(LuaLBuffer B, CharPtr s, uint l) { while (l-- != 0) { char c = s[0]; s = s.next(); LuaLAddChar(B, c); } }
private static int str_reverse (LuaState L) { uint l; LuaLBuffer b = new LuaLBuffer(); CharPtr s = LuaLCheckLString(L, 1, out l); LuaLBuffInit(L, b); while ((l--) != 0) LuaLAddChar(b, s[l]); LuaLPushResult(b); return 1; }
private static void addfield(LuaState L, LuaLBuffer b, int i) { LuaRawGetI(L, 1, i); if (LuaIsString(L, -1) == 0) { LuaLError(L, "invalid value (%s) at index %d in table for " + LUA_QL("concat"), LuaLTypeName(L, -1), i); } LuaLAddValue(b); }
private static int OSDate(LuaState L) { CharPtr s = LuaLOptString(L, 1, "%c"); DateTime stm; if (s[0] == '!') /* UTC? */ { stm = DateTime.UtcNow; s.inc(); /* skip `!' */ } else { stm = DateTime.Now; } if (strcmp(s, "*t") == 0) { LuaCreateTable(L, 0, 9); /* 9 = number of fields */ SetField(L, "sec", stm.Second); SetField(L, "min", stm.Minute); SetField(L, "hour", stm.Hour); SetField(L, "day", stm.Day); SetField(L, "month", stm.Month); SetField(L, "year", stm.Year); SetField(L, "wday", (int)stm.DayOfWeek); SetField(L, "yday", stm.DayOfYear); SetBoolField(L, "isdst", stm.IsDaylightSavingTime() ? 1 : 0); } else { CharPtr cc = new char[3]; LuaLBuffer b = new LuaLBuffer(); cc[0] = '%'; cc[2] = '\0'; LuaLBuffInit(L, b); for (; s[0] != 0; s.inc()) { if (s[0] != '%' || s[1] == '\0') /* no conversion specifier? */ { LuaLAddChar(b, s[0]); } else { uint reslen; CharPtr buff = new char[200]; /* should be big enough for any conversion result */ s.inc(); cc[1] = s[0]; reslen = strftime(buff, (uint)buff.chars.Length, cc, stm); buff.index = 0; LuaLAddLString(b, buff, reslen); } } LuaLPushResult(b); } return(1); }
private static int str_upper (LuaState L) { uint l; uint i; LuaLBuffer b = new LuaLBuffer(); CharPtr s = LuaLCheckLString(L, 1, out l); LuaLBuffInit(L, b); for (i=0; i<l; i++) LuaLAddChar(b, toupper(s[i])); LuaLPushResult(b); return 1; }
private static int str_dump(LuaState L) { LuaLBuffer b = new LuaLBuffer(); LuaLCheckType(L, 1, LUA_TFUNCTION); LuaSetTop(L, 1); LuaLBuffInit(L, b); if (LuaDump(L, writer, b) != 0) { LuaLError(L, "unable to dump given function"); } LuaLPushResult(b); return(1); }
private static int str_reverse(LuaState L) { uint l; LuaLBuffer b = new LuaLBuffer(); CharPtr s = LuaLCheckLString(L, 1, out l); LuaLBuffInit(L, b); while ((l--) != 0) { LuaLAddChar(b, s[l]); } LuaLPushResult(b); return(1); }
private static int str_upper(LuaState L) { uint l; uint i; LuaLBuffer b = new LuaLBuffer(); CharPtr s = LuaLCheckLString(L, 1, out l); LuaLBuffInit(L, b); for (i = 0; i < l; i++) { LuaLAddChar(b, toupper(s[i])); } LuaLPushResult(b); return(1); }
private static int str_rep(LuaState L) { uint l; LuaLBuffer b = new LuaLBuffer(); CharPtr s = LuaLCheckLString(L, 1, out l); int n = LuaLCheckInt(L, 2); LuaLBuffInit(L, b); while (n-- > 0) { LuaLAddLString(b, s, l); } LuaLPushResult(b); return(1); }
private static int str_char(LuaState L) { int n = LuaGetTop(L); /* number of arguments */ int i; LuaLBuffer b = new LuaLBuffer(); LuaLBuffInit(L, b); for (i = 1; i <= n; i++) { int c = LuaLCheckInt(L, i); LuaLArgCheck(L, (byte)(c) == c, i, "invalid value"); LuaLAddChar(b, (char)(byte)c); } LuaLPushResult(b); return(1); }
private static int EmptyBuffer(LuaLBuffer B) { uint l = (uint)BufferLen(B); if (l == 0) { return(0); /* put nothing on stack */ } else { LuaPushLString(B.L, B.buffer, l); B.p = 0; B.lvl++; return(1); } }
/* }====================================================== */ public static CharPtr LuaLGSub(LuaState L, CharPtr s, CharPtr p, CharPtr r) { CharPtr wild; uint l = (uint)strlen(p); LuaLBuffer b = new LuaLBuffer(); LuaLBuffInit(L, b); while ((wild = strstr(s, p)) != null) { LuaLAddLString(b, s, (uint)(wild - s)); /* push prefix */ LuaLAddString(b, r); /* push replacement in place of pattern */ s = wild + l; /* continue after `p' */ } LuaLAddString(b, s); /* push last suffix */ LuaLPushResult(b); return(LuaToString(L, -1)); }
private static void add_value(MatchState ms, LuaLBuffer b, CharPtr s, CharPtr e) { var L = ms.L; switch (LuaType(L, 3)) { case LUA_TNUMBER: case LUA_TSTRING: { add_s(ms, b, s, e); return; } // case LUA_TUSERDATA: /// +++ does this make sense ?? case LUA_TFUNCTION: { LuaPushValue(L, 3); int n = push_captures(ms, s, e); LuaCall(L, n, 1); break; } case LUA_TTABLE: { push_onecapture(ms, 0, s, e); LuaGetTable(L, 3); break; } } if (LuaToBoolean(L, -1) == 0) { /* nil or false? */ LuaPop(L, 1); LuaPushLString(L, s, (uint)(e - s)); /* keep original text */ } else if (LuaIsString(L, -1) == 0) { LuaLError(L, "invalid replacement value (a {0})", LuaLTypeName(L, -1)); } LuaLAddValue(b); /* add result to accumulator */ }
private static int ReadChars(LuaState L, Stream f, uint n) { uint rlen; /* how much to read */ uint nr; /* number of chars actually read */ LuaLBuffer b = new LuaLBuffer(); LuaLBuffInit(L, b); rlen = LUAL_BUFFERSIZE; /* try to read that much each time */ do { CharPtr p = LuaLPrepBuffer(b); if (rlen > n) { rlen = n; /* cannot read more than asked */ } nr = (uint)fread(p, GetUnmanagedSize(typeof(char)), (int)rlen, f); LuaLAddSize(b, (int)nr); n -= nr; /* still have to read `n' chars */ } while (n > 0 && nr == rlen); /* until end of count or eof */ LuaLPushResult(b); /* close buffer */ return((n == 0 || LuaObjectLen(L, -1) > 0) ? 1 : 0); }
private static int tconcat(LuaState L) { LuaLBuffer b = new LuaLBuffer(); uint lsep; int i, last; CharPtr sep = LuaLOptLString(L, 2, "", out lsep); LuaLCheckType(L, 1, LUA_TTABLE); i = LuaLOptInt(L, 3, 1); last = LuaLOptInteger(L, LuaLCheckInt, 4, LuaLGetN(L, 1)); LuaLBuffInit(L, b); for (; i < last; i++) { addfield(L, b, i); LuaLAddLString(b, sep, lsep); } if (i == last) /* add last value (if interval was not empty) */ { addfield(L, b, i); } LuaLPushResult(b); return(1); }
private static void addquoted(LuaState L, LuaLBuffer b, int arg) { uint l; CharPtr s = LuaLCheckLString(L, arg, out l); LuaLAddChar(b, '"'); while ((l--) != 0) { switch (s[0]) { case '"': case '\\': case '\n': { LuaLAddChar(b, '\\'); LuaLAddChar(b, s[0]); break; } case '\r': { LuaLAddLString(b, "\\r", 2); break; } case '\0': { LuaLAddLString(b, "\\000", 4); break; } default: { LuaLAddChar(b, s[0]); break; } } s = s.next(); } LuaLAddChar(b, '"'); }
private static void AdjustStack(LuaLBuffer B) { if (B.lvl > 1) { LuaState L = B.L; int toget = 1; /* number of levels to concat */ uint toplen = LuaStrLen(L, -1); do { uint l = LuaStrLen(L, -(toget + 1)); if (B.lvl - toget + 1 >= LIMIT || toplen > l) { toplen += l; toget++; } else { break; } } while (toget < B.lvl); LuaConcat(L, toget); B.lvl = B.lvl - toget + 1; } }
private static int str_dump(LuaState L) { LuaLBuffer b = new LuaLBuffer(); LuaLCheckType(L, 1, LUA_TFUNCTION); LuaSetTop(L, 1); LuaLBuffInit(L,b); if (LuaDump(L, writer, b) != 0) LuaLError(L, "unable to dump given function"); LuaLPushResult(b); return 1; }
private static void add_value(MatchState ms, LuaLBuffer b, CharPtr s, CharPtr e) { LuaState L = ms.L; switch (LuaType(L, 3)) { case LUA_TNUMBER: case LUA_TSTRING: { add_s(ms, b, s, e); return; } case LUA_TUSERDATA: case LUA_TFUNCTION: { int n; LuaPushValue(L, 3); n = push_captures(ms, s, e); LuaCall(L, n, 1); break; } case LUA_TTABLE: { push_onecapture(ms, 0, s, e); LuaGetTable(L, 3); break; } } if (LuaToBoolean(L, -1)==0) { /* nil or false? */ LuaPop(L, 1); LuaPushLString(L, s, (uint)(e - s)); /* keep original text */ } else if (LuaIsString(L, -1)==0) LuaLError(L, "invalid replacement value (a %s)", LuaLTypeName(L, -1)); LuaLAddValue(b); /* add result to accumulator */ }
///* compatibility only */ public static void LuaLPutChar(LuaLBuffer B, char c) {LuaLAddChar(B,c);}
public static void LuaLBuffInit(LuaState L, LuaLBuffer B) { B.L = L; B.p = /*B.buffer*/ 0; B.lvl = 0; }
public static void LuaLPushResult(LuaLBuffer B) { EmptyBuffer(B); LuaConcat(B.L, B.lvl); B.lvl = 1; }
private static int ReadChars(LuaState L, Stream f, uint n) { uint rlen; /* how much to read */ uint nr; /* number of chars actually read */ LuaLBuffer b = new LuaLBuffer(); LuaLBuffInit(L, b); rlen = LUAL_BUFFERSIZE; /* try to read that much each time */ do { CharPtr p = LuaLPrepBuffer(b); if (rlen > n) rlen = n; /* cannot read more than asked */ nr = (uint)fread(p, GetUnmanagedSize(typeof(char)), (int)rlen, f); LuaLAddSize(b, (int)nr); n -= nr; /* still have to read `n' chars */ } while (n > 0 && nr == rlen); /* until end of count or eof */ LuaLPushResult(b); /* close buffer */ return (n == 0 || LuaObjectLen(L, -1) > 0) ? 1 : 0; }
public static CharPtr LuaLPrepBuffer (LuaLBuffer B) { if (EmptyBuffer(B) != 0) AdjustStack(B); return new CharPtr(B.buffer, B.p); }
public static void LuaLAddLString (LuaLBuffer B, CharPtr s, uint l) { while (l-- != 0) { char c = s[0]; s = s.next(); LuaLAddChar(B, c); } }
public static void LuaLAddString (LuaLBuffer B, CharPtr s) { LuaLAddLString(B, s, (uint)strlen(s)); }
public static void LuaLPushResult (LuaLBuffer B) { EmptyBuffer(B); LuaConcat(B.L, B.lvl); B.lvl = 1; }
public static void LuaLAddValue (LuaLBuffer B) { LuaState L = B.L; uint vl; CharPtr s = LuaToLString(L, -1, out vl); if (vl <= BufferFree(B)) { /* fit into buffer? */ CharPtr dst = new CharPtr(B.buffer.chars, B.buffer.index + B.p); CharPtr src = new CharPtr(s.chars, s.index); for (uint i = 0; i < vl; i++) dst[i] = src[i]; B.p += (int)vl; LuaPop(L, 1); /* remove from stack */ } else { if (EmptyBuffer(B) != 0) LuaInsert(L, -2); /* put buffer before new value */ B.lvl++; /* add new value into B stack */ AdjustStack(B); } }
private static int str_gsub(LuaState L) { uint srcl; CharPtr src = LuaLCheckLString(L, 1, out srcl); CharPtr p = LuaLCheckString(L, 2); int tr = LuaType(L, 3); int max_s = LuaLOptInt(L, 4, (int)(srcl+1)); int anchor = 0; if (p[0] == '^') { p = p.next(); anchor = 1; } int n = 0; MatchState ms = new MatchState(); LuaLBuffer b = new LuaLBuffer(); LuaLArgCheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || tr == LUA_TFUNCTION || tr == LUA_TTABLE || tr == LUA_TUSERDATA, 3, "string/function/table expected"); LuaLBuffInit(L, b); ms.L = L; ms.matchdepth = MAXCCALLS; ms.src_init = src; ms.src_end = src+srcl; while (n < max_s) { CharPtr e; ms.level = 0; LuaAssert(ms.matchdepth == MAXCCALLS); e = match(ms, src, p); if (e != null) { n++; add_value(ms, b, src, e); } if ((e!=null) && e>src) /* non empty match? */ src = e; /* skip it */ else if (src < ms.src_end) { char c = src[0]; src = src.next(); LuaLAddChar(b, c); } else break; if (anchor != 0) break; } LuaLAddLString(b, src, (uint)(ms.src_end-src)); LuaLPushResult(b); LuaPushInteger(L, n); /* number of substitutions */ return 2; }
private static int tconcat (LuaState L) { LuaLBuffer b = new LuaLBuffer(); uint lsep; int i, last; CharPtr sep = LuaLOptLString(L, 2, "", out lsep); LuaLCheckType(L, 1, LUA_TTABLE); i = LuaLOptInt(L, 3, 1); last = LuaLOptInteger(L, LuaLCheckInt, 4, LuaLGetN(L, 1)); LuaLBuffInit(L, b); for (; i < last; i++) { addfield(L, b, i); LuaLAddLString(b, sep, lsep); } if (i == last) /* add last value (if interval was not empty) */ addfield(L, b, i); LuaLPushResult(b); return 1; }
private static void AdjustStack (LuaLBuffer B) { if (B.lvl > 1) { LuaState L = B.L; int toget = 1; /* number of levels to concat */ uint toplen = LuaStrLen(L, -1); do { uint l = LuaStrLen(L, -(toget+1)); if (B.lvl - toget + 1 >= LIMIT || toplen > l) { toplen += l; toget++; } else break; } while (toget < B.lvl); LuaConcat(L, toget); B.lvl = B.lvl - toget + 1; } }
public static void LuaLAddString(LuaLBuffer B, CharPtr s) { LuaLAddLString(B, s, (uint)strlen(s)); }
///* compatibility only */ public static void LuaLPutChar(LuaLBuffer B, char c) { LuaLAddChar(B, c); }
public static void LuaLAddSize(LuaLBuffer B, int n) {B.p += n;}
public static void LuaLAddSize(LuaLBuffer B, int n) { B.p += n; }
public static int str_format(LuaState L) { int top = LuaGetTop(L); int arg = 1; uint sfl; CharPtr strfrmt = LuaLCheckLString(L, arg, out sfl); CharPtr strfrmt_end = strfrmt + sfl; LuaLBuffer b = new LuaLBuffer(L); LuaLBuffInit(L, b); while (strfrmt < strfrmt_end) { if (strfrmt[0] != L_ESC) { LuaLAddChar(b, strfrmt[0]); strfrmt = strfrmt.next(); } else if (strfrmt[1] == L_ESC) { LuaLAddChar(b, strfrmt[0]); /* %% */ strfrmt = strfrmt + 2; } else { /* format item */ strfrmt = strfrmt.next(); CharPtr form = new char[MAX_FORMAT]; /* to store the format (`%...') */ CharPtr buff = new char[MAX_ITEM]; /* to store the formatted item */ if (++arg > top) { LuaLArgError(L, arg, "no value"); } strfrmt = scanformat(L, strfrmt, form); char ch = strfrmt[0]; strfrmt = strfrmt.next(); switch (ch) { case 'c': { sprintf(buff, form, (int)LuaLCheckNumber(L, arg)); break; } case 'd': case 'i': { addintlen(form); sprintf(buff, form, (LUA_INTFRM_T)LuaLCheckNumber(L, arg)); break; } case 'o': case 'u': case 'x': case 'X': { addintlen(form); sprintf(buff, form, (UNSIGNED_LUA_INTFRM_T)LuaLCheckNumber(L, arg)); break; } case 'e': case 'E': case 'f': case 'g': case 'G': { sprintf(buff, form, (double)LuaLCheckNumber(L, arg)); break; } case 'q': { addquoted(L, b, arg); continue; /* skip the 'addsize' at the end */ } case 's': { uint l; CharPtr s = LuaLCheckLString(L, arg, out l, true); if ((strchr(form, '.') == null) && l >= 100) { /* no precision and string is too long to be formatted; * keep original string */ LuaPushValue(L, arg); LuaLAddValue(b); continue; /* skip the `addsize' at the end */ } else { sprintf(buff, form, s); break; } } default: { /* also treat cases `pnLlh' */ return(LuaLError(L, "invalid option " + LUA_QL("%" + ch) + " to " + LUA_QL("format"), strfrmt[-1])); } } LuaLAddLString(b, buff, (uint)strlen(buff)); } } LuaLPushResult(b); return(1); }
private static int EmptyBuffer (LuaLBuffer B) { uint l = (uint)BufferLen(B); if (l == 0) return 0; /* put nothing on stack */ else { LuaPushLString(B.L, B.buffer, l); B.p = 0; B.lvl++; return 1; } }
public static void LuaLAddChar(LuaLBuffer B, char c) { if (B.p >= LUAL_BUFFERSIZE) LuaLPrepBuffer(B); B.buffer[B.p++] = c; }
/* ** {====================================================== ** Generic Buffer manipulation ** ======================================================= */ private static int BufferLen(LuaLBuffer B) { return(B.p); }
private static void add_s(MatchState ms, LuaLBuffer b, CharPtr s, CharPtr e) { uint l, i; CharPtr news = LuaToLString(ms.L, 3, out l); for (i = 0; i < l; i++) { if (news[i] != L_ESC) LuaLAddChar(b, news[i]); else { i++; /* skip ESC */ if (!isdigit((byte)(news[i]))) LuaLAddChar(b, news[i]); else if (news[i] == '0') LuaLAddLString(b, s, (uint)(e - s)); else { push_onecapture(ms, news[i] - '1', s, e); LuaLAddValue(b); /* add capture to accumulated result */ } } } }
private static int BufferFree(LuaLBuffer B) { return(LUAL_BUFFERSIZE - BufferLen(B)); }
private static int str_char(LuaState L) { int n = LuaGetTop(L); /* number of arguments */ int i; LuaLBuffer b = new LuaLBuffer(); LuaLBuffInit(L, b); for (i=1; i<=n; i++) { int c = LuaLCheckInt(L, i); LuaLArgCheck(L, (byte)(c) == c, i, "invalid value"); LuaLAddChar(b, (char)(byte)c); } LuaLPushResult(b); return 1; }
private static int BufferFree(LuaLBuffer B) {return LUAL_BUFFERSIZE - BufferLen(B);}
private static int str_format(LuaState L) { int top = LuaGetTop(L); int arg = 1; uint sfl; CharPtr strfrmt = LuaLCheckLString(L, arg, out sfl); CharPtr strfrmt_end = strfrmt+sfl; LuaLBuffer b = new LuaLBuffer(); LuaLBuffInit(L, b); while (strfrmt < strfrmt_end) { if (strfrmt[0] != L_ESC) { LuaLAddChar(b, strfrmt[0]); strfrmt = strfrmt.next(); } else if (strfrmt[1] == L_ESC) { LuaLAddChar(b, strfrmt[0]); /* %% */ strfrmt = strfrmt + 2; } else { /* format item */ strfrmt = strfrmt.next(); CharPtr form = new char[MAX_FORMAT]; /* to store the format (`%...') */ CharPtr buff = new char[MAX_ITEM]; /* to store the formatted item */ if (++arg > top) LuaLArgError(L, arg, "no value"); strfrmt = scanformat(L, strfrmt, form); char ch = strfrmt[0]; strfrmt = strfrmt.next(); switch (ch) { case 'c': { sprintf(buff, form, (int)LuaLCheckNumber(L, arg)); break; } case 'd': case 'i': { addintlen(form); sprintf(buff, form, (LUA_INTFRM_T)LuaLCheckNumber(L, arg)); break; } case 'o': case 'u': case 'x': case 'X': { addintlen(form); sprintf(buff, form, (UNSIGNED_LUA_INTFRM_T)LuaLCheckNumber(L, arg)); break; } case 'e': case 'E': case 'f': case 'g': case 'G': { sprintf(buff, form, (double)LuaLCheckNumber(L, arg)); break; } case 'q': { addquoted(L, b, arg); continue; /* skip the 'addsize' at the end */ } case 's': { uint l; CharPtr s = LuaLCheckLString(L, arg, out l); if ((strchr(form, '.') == null) && l >= 100) { /* no precision and string is too long to be formatted; keep original string */ LuaPushValue(L, arg); LuaLAddValue(b); continue; /* skip the `addsize' at the end */ } else { sprintf(buff, form, s); break; } } default: { /* also treat cases `pnLlh' */ return LuaLError(L, "invalid option " + LUA_QL("%%%c") + " to " + LUA_QL("format"), strfrmt[-1]); } } LuaLAddLString(b, buff, (uint)strlen(buff)); } } LuaLPushResult(b); return 1; }
/* ** {====================================================== ** Generic Buffer manipulation ** ======================================================= */ private static int BufferLen(LuaLBuffer B) {return B.p;}
private static int str_rep(LuaState L) { uint l; LuaLBuffer b = new LuaLBuffer(); CharPtr s = LuaLCheckLString(L, 1, out l); int n = LuaLCheckInt(L, 2); LuaLBuffInit(L, b); while (n-- > 0) LuaLAddLString(b, s, l); LuaLPushResult(b); return 1; }
/* }====================================================== */ public static CharPtr LuaLGSub (LuaState L, CharPtr s, CharPtr p, CharPtr r) { CharPtr wild; uint l = (uint)strlen(p); LuaLBuffer b = new LuaLBuffer(); LuaLBuffInit(L, b); while ((wild = strstr(s, p)) != null) { LuaLAddLString(b, s, (uint)(wild - s)); /* push prefix */ LuaLAddString(b, r); /* push replacement in place of pattern */ s = wild + l; /* continue after `p' */ } LuaLAddString(b, s); /* push last suffix */ LuaLPushResult(b); return LuaToString(L, -1); }
private static int OSDate (LuaState L) { CharPtr s = LuaLOptString(L, 1, "%c"); DateTime stm; if (s[0] == '!') { /* UTC? */ stm = DateTime.UtcNow; s.inc(); /* skip `!' */ } else stm = DateTime.Now; if (strcmp(s, "*t") == 0) { LuaCreateTable(L, 0, 9); /* 9 = number of fields */ SetField(L, "sec", stm.Second); SetField(L, "min", stm.Minute); SetField(L, "hour", stm.Hour); SetField(L, "day", stm.Day); SetField(L, "month", stm.Month); SetField(L, "year", stm.Year); SetField(L, "wday", (int)stm.DayOfWeek); SetField(L, "yday", stm.DayOfYear); SetBoolField(L, "isdst", stm.IsDaylightSavingTime() ? 1 : 0); } else { CharPtr cc = new char[3]; LuaLBuffer b = new LuaLBuffer(); cc[0] = '%'; cc[2] = '\0'; LuaLBuffInit(L, b); for (; s[0] != 0; s.inc()) { if (s[0] != '%' || s[1] == '\0') /* no conversion specifier? */ LuaLAddChar(b, s[0]); else { uint reslen; CharPtr buff = new char[200]; /* should be big enough for any conversion result */ s.inc(); cc[1] = s[0]; reslen = strftime(buff, (uint)buff.chars.Length, cc, stm); buff.index = 0; LuaLAddLString(b, buff, reslen); } } LuaLPushResult(b); } return 1; }
public static void LuaLBuffInit (LuaState L, LuaLBuffer B) { B.L = L; B.p = /*B.buffer*/ 0; B.lvl = 0; }
private static int ReadLine(LuaState L, Stream f) { LuaLBuffer b = new LuaLBuffer(); LuaLBuffInit(L, b); for (; ; ) { uint l; CharPtr p = LuaLPrepBuffer(b); if (fgets(p, f) == null) { /* eof? */ LuaLPushResult(b); /* close buffer */ return (LuaObjectLen(L, -1) > 0) ? 1 : 0; /* check whether read something */ } l = (uint)strlen(p); if (l == 0 || p[l - 1] != '\n') LuaLAddSize(b, (int)l); else { LuaLAddSize(b, (int)(l - 1)); /* do not include `eol' */ LuaLPushResult(b); /* close buffer */ return 1; /* read at least an `eol' */ } } }
private static int OSDate(LuaState L) { CharPtr s = LuaLOptString(L, 1, "%c"); DateTime stm; // Parses the second argument if there's one. If not, uses Now as time. if (LuaIsNoneOrNil(L, 2)) { stm = DateTime.Now; } else { LuaLCheckType(L, 2, LUA_TNUMBER); double seconds = LuaToNumber(L, 2); stm = new DateTime((long)seconds * TimeSpan.TicksPerSecond); } if (s[0] == '!') { /* UTC? */ stm = stm.ToUniversalTime(); s.inc(); /* skip `!' */ } if (strcmp(s, "*t") == 0) { LuaCreateTable(L, 0, 9); /* 9 = number of fields */ SetField(L, "sec", stm.Second); SetField(L, "min", stm.Minute); SetField(L, "hour", stm.Hour); SetField(L, "day", stm.Day); SetField(L, "month", stm.Month); SetField(L, "year", stm.Year); SetField(L, "wday", (int)stm.DayOfWeek + 1); SetField(L, "yday", stm.DayOfYear); SetBoolField(L, "isdst", stm.IsDaylightSavingTime() ? 1 : 0); } else { CharPtr cc = new char[3]; LuaLBuffer b = new LuaLBuffer(); cc[0] = '%'; cc[2] = '\0'; LuaLBuffInit(L, b); for (; s[0] != 0; s.inc()) { if (s[0] != '%' || s[1] == '\0') /* no conversion specifier? */ LuaLAddChar(b, s[0]); else { uint reslen; CharPtr buff = new char[200]; /* should be big enough for any conversion result */ s.inc(); cc[1] = s[0]; reslen = strftime(buff, (uint)buff.chars.Length, cc, stm); buff.index = 0; LuaLAddLString(b, buff, reslen); } } LuaLPushResult(b); } return 1; }