public static String toLocale(long self, string pattern) { // if pattern is "B" format as bytes if (pattern != null && pattern.Length == 1 && pattern[0] == 'B') { return(toLocaleBytes(self)); } // get current locale Locale locale = Locale.cur(); NumberFormatInfo df = locale.dec(); // get default pattern if necessary if (pattern == null) { pattern = Env.cur().locale(Sys.m_sysPod, "int", "#,###"); } // parse pattern and get digits NumPattern p = NumPattern.parse(pattern); NumDigits d = new NumDigits(self); // route to common FanNum method return(FanNum.toLocale(p, d, df)); }
public static UriScheme find(string scheme, bool check) { // check cache lock (m_cache) { UriScheme cached = (UriScheme)m_cache[scheme]; if (cached != null) { return(cached); } } try { // lookup scheme type (avoid building index for common types) Type t = null; if (scheme == "fan") { t = Sys.FanSchemeType; } if (scheme == "file") { t = Sys.FileSchemeType; } if (t == null) { string qname = (string)Env.cur().index("sys.uriScheme." + scheme).first(); if (qname == null) { throw UnresolvedErr.make().val; } t = Type.find(qname); } // allocate instance UriScheme s = (UriScheme)t.make(); s.m_scheme = scheme; // add to cache lock (m_cache) { UriScheme cached = (UriScheme)m_cache[scheme]; if (cached != null) { return(cached); } m_cache[scheme] = s; } return(s); } catch (UnresolvedErr.Val) {} catch (System.Exception e) { Err.dumpStack(e); } if (!check) { return(null); } throw UnresolvedErr.make("Unknown scheme: " + scheme).val; }
public static List list() { lock (m_podsByName) { // TODO - eventually we need a faster way to load // pod meta-data into memory without actually loading // every pod into memory if (m_allPodsList == null) { List names = Env.cur().findAllPodNames(); List pods = new List(Sys.PodType); for (int i = 0; i < names.sz(); ++i) { string name = (string)names.get(i); try { pods.add(doFind(name, true, null)); } catch (Exception e) { System.Console.WriteLine("ERROR: Invalid pod file: " + name); Err.dumpStack(e); } } m_allPodsList = pods.ro(); } return(m_allPodsList); } }
private static List loadDatabase() { InStream input = null; List quantityNames = new List(Sys.StrType); try { // parse etc/sys/units.fog as big serialized list which contains // lists for each quantity (first item being the name) String path = "etc/sys/units.txt"; input = Env.cur().findFile(path).@in(); // parse each line string curQuantityName = null; List curQuantityList = null; string line; while ((line = input.readLine()) != null) { // skip comment and blank lines line = line.Trim(); if (line.StartsWith("//") || line.Length == 0) { continue; } // quanity sections delimited as "-- name (dim)" if (line.StartsWith("--")) { if (curQuantityName != null) { m_quantities[curQuantityName] = curQuantityList.toImmutable(); } curQuantityName = line.Substring(2, line.IndexOf('(') - 2).Trim(); curQuantityList = new List(Sys.UnitType); quantityNames.add(curQuantityName); continue; } // must be a unit try { Unit unit = Unit.define(line); curQuantityList.add(unit); } catch (Exception e) { System.Console.WriteLine("WARNING: Init unit in etc/sys/units.txt: " + line); System.Console.WriteLine(" " + e); } } m_quantities[curQuantityName] = curQuantityList.toImmutable(); } catch (Exception e) { try { input.close(); } catch (Exception) {} System.Console.WriteLine("WARNING: Cannot load lib/units.txt"); Err.dumpStack(e); } return((List)quantityNames.toImmutable()); }
////////////////////////////////////////////////////////////////////////// // Utils ////////////////////////////////////////////////////////////////////////// public File tempDir() { if (m_tempDir == null) { m_tempDir = Env.cur().tempDir().plus(Uri.fromStr("test/"), false); m_tempDir.delete(); m_tempDir.create(); } return(m_tempDir); }
public static string toLocale(double self, string pattern) { try { // get current locale Locale locale = Locale.cur(); NumberFormatInfo df = locale.dec(); // handle special values if (System.Double.IsNaN(self)) { return(df.NaNSymbol); } if (self == System.Double.PositiveInfinity) { return(df.PositiveInfinitySymbol); } if (self == System.Double.NegativeInfinity) { return(df.NegativeInfinitySymbol); } // get default pattern if necessary if (pattern == null) { pattern = Env.cur().locale(Sys.m_sysPod, "float", "#,###.0##"); } // TODO: if value is < 10^-3 or > 10^7 it will be // converted to exponent string, so just bail on that string str = Double.toString(self); if (str.IndexOf('E') > 0) { str = self.ToString("0.#########"); } // parse pattern and get digits NumPattern p = NumPattern.parse(pattern); NumDigits d = new NumDigits(str); // route to common FanNum method return(FanNum.toLocale(p, d, df)); } catch (Exception e) { Err.dumpStack(e); return(self.ToString()); } }
public string toLocale(string pattern) { // locale specific default Locale locale = null; if (pattern == null) { if (locale == null) { locale = Locale.cur(); } pattern = Env.cur().locale(Sys.m_sysPod, m_localeKey, "D-MMM-YYYY", locale); } return(new DateTimeStr(pattern, locale, this).format()); }
////////////////////////////////////////////////////////////////////////// // Init Env ////////////////////////////////////////////////////////////////////////// private static bool initEnv() { try { string var = (string)Env.cur().vars().get("FAN_ENV"); if (var == null) { return(true); } m_curEnv = (Env)Type.find(var).make(); } catch (Exception e) { initWarn("curEnv", e); } return(true); }
public static FPod readFPod(string name) { FStore store = null; // handle sys specially for bootstrapping the VM if (name == "sys") { store = new FStore(new ZipFile(FileUtil.combine(Sys.m_podsDir, name + ".pod"))); } // otherwise delegate to Env.cur to find the pod file else { FileSystemInfo file = null; Fan.Sys.File f = Env.cur().findPodFile(name); if (f != null) { file = ((LocalFile)f).m_file; } // if null or doesn't exist then its a no go if (file == null || !file.Exists) { throw UnknownPodErr.make(name).val; } // verify case since Windoze is case insensitive String actualName = file.Name; //getCanonicalFile().getName(); actualName = actualName.Substring(0, actualName.Length - 4); if (actualName != name) { throw UnknownPodErr.make("Mismatch case: " + name + " != " + actualName).val; } store = new FStore(new ZipFile(file.FullName)); } // read in the FPod tables FPod fpod = new FPod(name, store); fpod.read(); return(fpod); }
public static string toLocale(BigDecimal self, string pattern) { // get current locale Locale locale = Locale.cur(); NumberFormatInfo df = locale.dec(); // get default pattern if necessary if (pattern == null) { pattern = Env.cur().locale(Sys.m_sysPod, "decimal", "#,###.0##"); } // parse pattern and get digits NumPattern p = NumPattern.parse(pattern); NumDigits d = new NumDigits(self); // route to common FanNum method return(FanNum.toLocale(p, d, df)); }
////////////////////////////////////////////////////////////////////////// // Locale ////////////////////////////////////////////////////////////////////////// public string toLocale() { long ticks = this.m_ticks; Pod pod = Sys.m_sysPod; Env env = Env.cur(); Locale locale = Locale.cur(); StringBuilder s; // less than 1000ns Xns if (ticks < 1000L) { return(ticks + env.locale(pod, "nsAbbr", "ns", locale)); } // less than 2ms X.XXXms if (ticks < 2 * nsPerMilli) { s = new StringBuilder(); long ms = ticks / nsPerMilli; long us = (ticks - ms * nsPerMilli) / 1000L; s.Append(ms); s.Append('.'); if (us < 100) { s.Append('0'); } if (us < 10) { s.Append('0'); } s.Append(us); if (s[s.Length - 1] == '0') { s.Length = s.Length - 1; } if (s[s.Length - 1] == '0') { s.Length = s.Length - 1; } s.Append(env.locale(pod, "msAbbr", "ms", locale)); return(s.ToString()); } // less than 2sec Xms if (ticks < 2L * nsPerSec) { return((ticks / nsPerMilli) + env.locale(pod, "msAbbr", "ms", locale)); } // less than 2min Xsec if (ticks < 1L * nsPerMin) { return((ticks / nsPerSec) + env.locale(pod, "secAbbr", "sec", locale)); } // [Xdays] [Xhr] Xmin Xsec long days = ticks / nsPerDay; ticks -= days * nsPerDay; long hr = ticks / nsPerHr; ticks -= hr * nsPerHr; long min = ticks / nsPerMin; ticks -= min * nsPerMin; long sec = ticks / nsPerSec; s = new StringBuilder(); if (days > 0) { s.Append(days).Append(days == 1 ? env.locale(pod, "dayAbbr", "day", locale) : env.locale(pod, "daysAbbr", "days", locale)).Append(" "); } if (hr > 0) { s.Append(hr).Append(env.locale(pod, "hourAbbr", "hr", locale)).Append(" "); } if (min > 0) { s.Append(min).Append(env.locale(pod, "minAbbr", "min", locale)).Append(" "); } if (sec > 0) { s.Append(sec).Append(env.locale(pod, "secAbbr", "sec", locale)).Append(" "); } s.Length = s.Length - 1; return(s.ToString()); }
public string config(string key, string def) { return(Env.cur().config(this, key, def)); }
public Map props(Uri uri, Duration maxAge) { return(Env.cur().props(this, uri, maxAge)); }
static TimeZone() { try { loadIndex(); } catch (Exception e) { System.Console.WriteLine("ERROR: Cannot load timezone database"); Err.dumpStack(e); } try { m_utc = fromStr("Etc/UTC"); } catch (Exception e) { System.Console.WriteLine("ERROR: Cannot init UTC timezone"); Err.dumpStack(e); m_utc = loadFallback("Etc/UTC", "UTC"); } try { m_rel = fromStr("Etc/Rel"); } catch (Exception e) { System.Console.WriteLine("ERROR: Cannot init Rel timezone"); Err.dumpStack(e); m_rel = loadFallback("Etc/Rel", "Rel"); } try { // first check system property string sysProp = (string)Env.cur().vars().get("fan.timezone"); if (sysProp != null) { m_cur = fromStr(sysProp); } // we assume Java default uses Olson name else { // TODO - no clue how to auto map this yet //cur = fromStr(java.util.TimeZone.getDefault().getID()); m_cur = fromStr("America/New_York"); } } catch (Exception e) { System.Console.WriteLine("ERROR: Cannot init current timezone"); Err.dumpStack(e); m_cur = m_utc; } }
////////////////////////////////////////////////////////////////////////// // Identity ////////////////////////////////////////////////////////////////////////// public static long hash(Service self) { return(Env.cur().idHash(self)); }
public string config(string key) { return(Env.cur().config(this, key)); }
public Err trace() { return(trace(Env.cur().@err(), null, 0, true)); }
public string locale(string key, string def) { return(Env.cur().locale(this, key, def)); }
public static string toLocale(bool self) { return(Env.cur().locale(Sys.m_sysPod, self ? "boolTrue" : "boolFalse", toStr(self))); }
public void print() { print(Env.cur().@out()); }
public string locale(string key) { return(Env.cur().locale(this, key)); }