public CharPtr(CharPtr ptr) { this.chars = ptr.chars; this.index = ptr.index; }
public CharPtr(CharPtr ptr, int index) { this.chars = ptr.chars; this.index = index; }
public static CharPtr LinyeeOPushFString(LinyeeState L, CharPtr fmt, params object[] args) { return(LinyeeOPushVFString(L, fmt, args)); }
public TString(CharPtr str) { this.str = str; }
private static void PushStr(LinyeeState L, CharPtr str) { SetSValue2S(L, L.top, luaS_new(L, str)); IncrTop(L); }
/* this function handles only `%d', `%c', %f, %p, and `%s' formats */ public static CharPtr LinyeeOPushVFString(LinyeeState L, CharPtr fmt, params object[] argp) { int parm_index = 0; int n = 1; PushStr(L, ""); for (;;) { CharPtr e = strchr(fmt, '%'); if (e == null) { break; } SetSValue2S(L, L.top, luaS_newlstr(L, fmt, (uint)(e - fmt))); IncrTop(L); switch (e[1]) { case 's': { object o = argp[parm_index++]; CharPtr s = o as CharPtr; if (s == null) { s = (string)o; } if (s == null) { s = "(null)"; } PushStr(L, s); break; } case 'c': { CharPtr buff = new char[2]; buff[0] = (char)(int)argp[parm_index++]; buff[1] = '\0'; PushStr(L, buff); break; } case 'd': { SetNValue(L.top, (int)argp[parm_index++]); IncrTop(L); break; } case 'f': { SetNValue(L.top, (l_uacNumber)argp[parm_index++]); IncrTop(L); break; } case 'p': { //CharPtr buff = new char[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */ CharPtr buff = new char[32]; sprintf(buff, "0x%08x", argp[parm_index++].GetHashCode()); PushStr(L, buff); break; } case '%': { PushStr(L, "%"); break; } default: { CharPtr buff = new char[3]; buff[0] = '%'; buff[1] = e[1]; buff[2] = '\0'; PushStr(L, buff); break; } } n += 2; fmt = e + 2; } PushStr(L, fmt); luaV_concat(L, n + 1, CastInt(L.top - L.base_) - 1); L.top -= n; return(SValue(L.top - 1)); }
private static int OSDate(LinyeeState L) { CharPtr s = new CharPtr(LinyeeLOptString(L, 1, "%c")); DateTime stm; // Parses the second argument if there's one. If not, uses Now as time. if (LinyeeIsNoneOrNil(L, 2)) { stm = DateTime.Now; } else { LinyeeLCheckType(L, 2, LINYEE_TNUMBER); double seconds = LinyeeToNumber(L, 2); stm = new DateTime((long)seconds * TimeSpan.TicksPerSecond); } if (s[0] == '!') /* UTC? */ { stm = stm.ToUniversalTime(); s.inc(); /* skip `!' */ } if (strcmp(s, "*t") == 0) { LinyeeCreateTable(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]; LinyeeLBuffer b = new LinyeeLBuffer(); cc[0] = '%'; cc[2] = '\0'; LinyeeLBuffInit(L, b); for (; s[0] != 0; s.inc()) { if (s[0] != '%' || s[1] == '\0') /* no conversion specifier? */ { LinyeeLAddChar(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; LinyeeLAddLString(b, buff, reslen); } } LinyeeLPushResult(b); } return(1); }
private static CharPtr StrFTimeFmt(CharPtr baseFormat, DateTime t, CharPtr pt, CharPtr ptlim) { CharPtr format = new CharPtr(baseFormat); for (; format[0] != 0; format.inc()) { if (format == '%') { format.inc(); if (format == 'E') { format.inc(); // Alternate Era is ignored } else if (format == 'O') { format.inc(); // Alternate numeric symbols is ignored } switch (format[0]) { case '\0': format.dec(); break; case 'A': // Full day of week //pt = _add((t->tm_wday < 0 || t->tm_wday > 6) ? "?" : _days[t->tm_wday], pt, ptlim); pt = StrFTimeAdd(t.ToString("dddd"), pt, ptlim); continue; case 'a': // Abbreviated day of week //pt = _add((t->tm_wday < 0 || t->tm_wday > 6) ? "?" : _days_abbrev[t->tm_wday], pt, ptlim); pt = StrFTimeAdd(t.ToString("ddd"), pt, ptlim); continue; case 'B': // Full month name //pt = _add((t->tm_mon < 0 || t->tm_mon > 11) ? "?" : _months[t->tm_mon], pt, ptlim); pt = StrFTimeAdd(t.ToString("MMMM"), pt, ptlim); continue; case 'b': // Abbreviated month name case 'h': // Abbreviated month name //pt = _add((t->tm_mon < 0 || t->tm_mon > 11) ? "?" : _months_abbrev[t->tm_mon], pt, ptlim); pt = StrFTimeAdd(t.ToString("MMM"), pt, ptlim); continue; case 'C': // First two digits of year (a.k.a. Year divided by 100 and truncated to integer (00-99)) //pt = _conv((t->tm_year + TM_YEAR_BASE) / 100, "%02d", pt, ptlim); pt = StrFTimeAdd(t.ToString("yyyy").Substring(0, 2), pt, ptlim); continue; case 'c': // Abbreviated date/time representation (e.g. Thu Aug 23 14:55:02 2001) pt = StrFTimeFmt("%a %b %e %H:%M:%S %Y", t, pt, ptlim); continue; case 'D': // Short MM/DD/YY date pt = StrFTimeFmt("%m/%d/%y", t, pt, ptlim); continue; case 'd': // Day of the month, zero-padded (01-31) //pt = _conv(t->tm_mday, "%02d", pt, ptlim); pt = StrFTimeAdd(t.ToString("dd"), pt, ptlim); continue; case 'e': // Day of the month, space-padded ( 1-31) //pt = _conv(t->tm_mday, "%2d", pt, ptlim); pt = StrFTimeAdd(t.Day.ToString().PadLeft(2, ' '), pt, ptlim); continue; case 'F': // Short YYYY-MM-DD date pt = StrFTimeFmt("%Y-%m-%d", t, pt, ptlim); continue; case 'H': // Hour in 24h format (00-23) //pt = _conv(t->tm_hour, "%02d", pt, ptlim); pt = StrFTimeAdd(t.ToString("HH"), pt, ptlim); continue; case 'I': // Hour in 12h format (01-12) //pt = _conv((t->tm_hour % 12) ? (t->tm_hour % 12) : 12, "%02d", pt, ptlim); pt = StrFTimeAdd(t.ToString("hh"), pt, ptlim); continue; case 'j': // Day of the year (001-366) pt = StrFTimeAdd(t.DayOfYear.ToString().PadLeft(3, ' '), pt, ptlim); continue; case 'k': // (Non-standard) // Hours in 24h format, space-padded ( 1-23) //pt = _conv(t->tm_hour, "%2d", pt, ptlim); pt = StrFTimeAdd(t.ToString("%H").PadLeft(2, ' '), pt, ptlim); continue; case 'l': // (Non-standard) // Hours in 12h format, space-padded ( 1-12) //pt = _conv((t->tm_hour % 12) ? (t->tm_hour % 12) : 12, "%2d", pt, ptlim); pt = StrFTimeAdd(t.ToString("%h").PadLeft(2, ' '), pt, ptlim); continue; case 'M': // Minute (00-59) //pt = _conv(t->tm_min, "%02d", pt, ptlim); pt = StrFTimeAdd(t.ToString("mm"), pt, ptlim); continue; case 'm': // Month as a decimal number (01-12) //pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim); pt = StrFTimeAdd(t.ToString("MM"), pt, ptlim); continue; case 'n': // New-line character. pt = StrFTimeAdd(Environment.NewLine, pt, ptlim); continue; case 'p': // AM or PM designation (locale dependent). //pt = _add((t->tm_hour >= 12) ? "pm" : "am", pt, ptlim); pt = StrFTimeAdd(t.ToString("tt"), pt, ptlim); continue; case 'R': // 24-hour HH:MM time, equivalent to %H:%M pt = StrFTimeFmt("%H:%M", t, pt, ptlim); continue; case 'r': // 12-hour clock time (locale dependent). pt = StrFTimeFmt("%I:%M:%S %p", t, pt, ptlim); continue; case 'S': // Second ((00-59) //pt = _conv(t->tm_sec, "%02d", pt, ptlim); pt = StrFTimeAdd(t.ToString("ss"), pt, ptlim); continue; case 'T': // ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S pt = StrFTimeFmt("%H:%M:%S", t, pt, ptlim); continue; case 't': // Horizontal-tab character pt = StrFTimeAdd("\t", pt, ptlim); continue; case 'U': // Week number with the first Sunday as the first day of week one (00-53) //pt = _conv((t->tm_yday + 7 - t->tm_wday) / 7, "%02d", pt, ptlim); pt = StrFTimeAdd(System.Globalization.CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(t, System.Globalization.CalendarWeekRule.FirstFullWeek, DayOfWeek.Sunday).ToString(), pt, ptlim); continue; case 'u': // ISO 8601 weekday as number with Monday as 1 (1-7) (locale independant). //pt = _conv((t->tm_wday == 0) ? 7 : t->tm_wday, "%d", pt, ptlim); pt = StrFTimeAdd(t.DayOfWeek == DayOfWeek.Sunday ? "7" : ((int)t.DayOfWeek).ToString(), pt, ptlim); continue; case 'G': // ISO 8601 year (four digits) case 'g': // ISO 8601 year (two digits) case 'V': // ISO 8601 week number // See http://stackoverflow.com/questions/11154673/get-the-correct-week-number-of-a-given-date DateTime isoTime = t; DayOfWeek day = System.Globalization.CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(isoTime); if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday) { isoTime = isoTime.AddDays(3); } if (format[0] == 'V') // ISO 8601 week number { int isoWeek = System.Globalization.CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(isoTime, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday); pt = StrFTimeAdd(isoWeek.ToString(), pt, ptlim); } else { string isoYear = System.Globalization.CultureInfo.InvariantCulture.Calendar.GetYear(isoTime).ToString(); // ISO 8601 year (four digits) if (format[0] == 'g') // ISO 8601 year (two digits) { isoYear = isoYear.Substring(isoYear.Length - 2, 2); } pt = StrFTimeAdd(isoYear, pt, ptlim); } continue; case 'W': // Week number with the first Monday as the first day of week one (00-53) //pt = _conv((t->tm_yday + 7 - (t->tm_wday ? (t->tm_wday - 1) : 6)) / 7, "%02d", pt, ptlim); pt = StrFTimeAdd(System.Globalization.CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(t, System.Globalization.CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday).ToString(), pt, ptlim); continue; case 'w': // Weekday as a decimal number with Sunday as 0 (0-6) //pt = _conv(t->tm_wday, "%d", pt, ptlim); pt = StrFTimeAdd(((int)t.DayOfWeek).ToString(), pt, ptlim); continue; case 'X': // Long time representation (locale dependent) //pt = _fmt("%H:%M:%S", t, pt, ptlim); // fails to comply with spec! pt = StrFTimeAdd(t.ToString("%T"), pt, ptlim); continue; case 'x': // Short date representation (locale dependent) //pt = _fmt("%m/%d/%y", t, pt, ptlim); // fails to comply with spec! pt = StrFTimeAdd(t.ToString("%d"), pt, ptlim); continue; case 'y': // Last two digits of year (00-99) //pt = _conv((t->tm_year + TM_YEAR_BASE) % 100, "%02d", pt, ptlim); pt = StrFTimeAdd(t.ToString("yy"), pt, ptlim); continue; case 'Y': // Full year (all digits) //pt = _conv(t->tm_year + TM_YEAR_BASE, "%04d", pt, ptlim); pt = StrFTimeAdd(t.Year.ToString(), pt, ptlim); continue; case 'Z': // Timezone name or abbreviation (locale dependent) or nothing if unavailable (e.g. CDT) pt = StrFTimeAdd(TimeZoneInfo.Local.StandardName, pt, ptlim); continue; case 'z': // ISO 8601 offset from UTC in timezone (+/-hhmm), or nothing if unavailable TimeSpan ts = TimeZoneInfo.Local.GetUtcOffset(t); string offset = (ts.Ticks < 0 ? "-" : "+") + ts.TotalHours.ToString("#00") + ts.Minutes.ToString("00"); pt = StrFTimeAdd(offset, pt, ptlim); continue; case '%': // Add '%' pt = StrFTimeAdd("%", pt, ptlim); continue; default: break; } } if (pt == ptlim) { break; } pt[0] = format[0]; pt.inc(); } return(pt); }
/* ** {====================================================== ** Time/Date operations ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S, ** wday=%w+1, yday=%j, isdst=? } ** ======================================================= */ private static void SetField(LinyeeState L, CharPtr key, int value) { LinyeePushInteger(L, value); LinyeeSetField(L, -2, key); }
public static void LinyeeGetGlobal(LinyeeState L, CharPtr s) { LinyeeGetField(L, LINYEE_GLOBALSINDEX, s); }
public static void LinyeePushLiteral(LinyeeState L, CharPtr s) { //TODO: Implement use using ly_pushlstring instead of ly_pushstring //ly_pushlstring(L, "" s, (sizeof(s)/GetUnmanagedSize(typeof(char)))-1) LinyeePushString(L, s); }
public static void LinyeeRegister(LinyeeState L, CharPtr n, LinyeeNativeFunction f) { LinyeePushCFunction(L, f); LinyeeSetGlobal(L, n); }
private static int LinyeeBLoadFile(LinyeeState L) { CharPtr fname = LinyeeLOptString(L, 1, null); return(LoadAux(L, LinyeeLLoadFile(L, fname))); }