public Map meta() { if (m_meta == null) { try { if (fpod.m_meta != null) { m_meta = (Map)fpod.m_meta; } else { InStream input = new SysInStream(fpod.m_store.read("meta.props")); m_meta = (Map)input.readProps().toImmutable(); input.close(); } } catch (Exception e) { Err.dumpStack(e); m_meta = Sys.m_emptyStrStrMap; } } return(m_meta); }
private static Map initVars() { Map vars = new Map(Sys.StrType, Sys.StrType); try { vars.caseInsensitive(true); // predefined vars.set("os.name", Environment.OSVersion.Platform.ToString()); vars.set("os.version", Environment.OSVersion.Version.ToString()); // environment variables IDictionary getenv = Environment.GetEnvironmentVariables(); foreach (DictionaryEntry de in getenv) { string key = (string)de.Key; string val = (string)de.Value; vars.set(key, val); } } catch (Exception e) { Err.dumpStack(e); } return((Map)vars.toImmutable()); }
public static Locale fromStr(string s, bool check) { int len = s.Length; try { if (len == 2) { if (FanStr.isLower(s)) { return(new Locale(s, s, null)); } } if (len == 5) { string lang = s.Substring(0, 2); string country = s.Substring(3, 2); if (FanStr.isLower(lang) && FanStr.isUpper(country) && s[2] == '-') { return(new Locale(s, lang, country)); } } } catch (Exception e) { Err.dumpStack(e); } if (!check) { return(null); } throw ParseErr.make("Locale", s).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); } }
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 Service start(Service self) { State state = null; try { lock (m_lock) { // start implies install install(self); // if already running, short circuit state = (State)byService[self]; if (state.running) { return(self); } // put into the running state state.running = true; } // onStart callback (outside of lock) self.onStart(); } catch (System.Exception e) { if (state != null) { state.running = false; } Err.dumpStack(e); } return(self); }
public static Service stop(Service self) { try { lock (m_lock) { // if not running, short circuit State state = (State)byService[self]; if (state == null || !state.running) { return(self); } // take out of the running state state.running = false; } // onStop (outside of lock) self.onStop(); } catch (System.Exception e) { Err.dumpStack(e); } return(self); }
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()); }
////////////////////////////////////////////////////////////////////////// // When Done ////////////////////////////////////////////////////////////////////////// internal void sendWhenDone(Actor a, Future f) { // if already done, then set immediate flag // otherwise add to our when done list bool immediate = false; lock (this) { if (isDone()) { immediate = true; } else { if (whenDone == null) { whenDone = new ArrayList(); } whenDone.Add(new WhenDone(a, f)); } } // if immediate we are already done so enqueue immediately if (immediate) { try { a._enqueue(f, false); } catch (System.Exception e) { Err.dumpStack(e); } } }
public virtual OutStream writeProps(Map props, bool cls) { Charset origCharset = charset(); charset(Charset.utf8()); try { List keys = props.keys().sort(); int size = keys.sz(); long eq = '='; long nl = '\n'; for (int i = 0; i < size; ++i) { string key = (string)keys.get(i); string val = (string)props.get(key); writePropStr(key); writeChar(eq); writePropStr(val); writeChar(nl); } return(this); } finally { try { if (cls) { close(); } } catch (System.Exception e) { Err.dumpStack(e); } charset(origCharset); } }
public override void finish() { if (m_finished) { return; } try { // ensure reflected and emitted reflect(); emit(); m_finished = true; // map .NET members to my slots for reflection; if // mixin then we do this for both the interface and // the static methods only of the implementation class finishSlots(m_type, false); if (isMixin()) { finishSlots(m_auxType, true); } } catch (Exception e) { Err.dumpStack(e); throw Err.make("Cannot emitFinish: " + m_qname + "." + m_finishing, e).val; } finally { m_finishing = null; } }
public virtual string readAllStr(bool normalizeNewlines) { try { char[] buf = new char[4096]; int n = 0; bool normalize = normalizeNewlines; // read characters int last = -1; while (true) { int c = rChar(); if (c < 0) { break; } // grow buffer if needed if (n >= buf.Length) { char[] temp = new char[buf.Length * 2]; System.Array.Copy(buf, 0, temp, 0, n); buf = temp; } // normalize newlines and add to buffer if (normalize) { if (c == '\r') { buf[n++] = '\n'; } else if (last == '\r' && c == '\n') { } else { buf[n++] = (char)c; } last = c; } else { buf[n++] = (char)c; } } return(new string(buf, 0, n)); } finally { try { close(); } catch (System.Exception e) { Err.dumpStack(e); } } }
////////////////////////////////////////////////////////////////////////// // Static Init ////////////////////////////////////////////////////////////////////////// static Log() { try { m_handlers[0] = Sys.LogRecType.method("print", true).func(); } catch (System.Exception e) { m_handlers = new Func[0]; Err.dumpStack(e); } }
public System.Type emit() { if (m_type == null) { if (Debug) { Console.WriteLine("-- emit: " + m_qname); } // make sure we have reflected to setup slots reflect(); // if sys class, just load it by name string podName = m_pod.m_name; if (podName == "sys") { try { m_dotnetRepr = FanUtil.isDotnetRepresentation(this); m_type = System.Type.GetType(FanUtil.toDotnetImplTypeName(podName, m_name)); } catch (Exception e) { Err.dumpStack(e); throw Err.make("Cannot load precompiled class: " + m_qname, e).val; } } // otherwise we need to emit it else { try { System.Type[] types = FTypeEmit.emitAndLoad(m_ftype); this.m_type = types[0]; if (types.Length > 1) { this.m_auxType = types[1]; } } catch (Exception e) { Err.dumpStack(e); throw Err.make("Cannot emit: " + m_qname, e).val; } } // we are done with our ftype now, gc it this.m_ftype = null; } return(m_type); }
internal static void sendWhenDone(ArrayList list) { if (list == null) { return; } for (int i = 0; i < list.Count; ++i) { WhenDone wd = (WhenDone)list[i]; try { wd.actor._enqueue(wd.future, false); } catch (System.Exception e) { Err.dumpStack(e); } } }
public static Pod doFind(string name, bool check, FPod fpod) { try { lock (m_podsByName) { // TODO - .NET does not have soft references, so how could // we implement this? See the Pod.java for the java impl. Pod pod = (Pod)m_podsByName[name]; if (pod == null) { // if fpod is non-null, then we are "creating" this pod in // memory direct from the compiler, otherwise we need to // find the pod zip file and load it's meta-data if (fpod == null) { fpod = readFPod(name); } // sanity check if (fpod.m_podName != name) { throw new Exception("Mismatched pod name b/w pod.def and pod zip filename: " + fpod.m_podName + " != " + name); } // create the pod and register it pod = new Pod(fpod); m_podsByName[name] = pod; } return(pod); } } catch (UnknownPodErr.Val e) { if (!check) { return(null); } throw e; } catch (Exception e) { Err.dumpStack(e); if (!check) { return(null); } throw UnknownPodErr.make(name, e).val; } }
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 virtual void eachLine(Func f) { try { string line; while ((line = readLine()) != null) { f.call(line); } } finally { try { close(); } catch (System.Exception e) { Err.dumpStack(e); } } }
public override void add(Future f) { try { object key = toKey(f.m_msg); if (key != null) { pending[key] = f; } } catch (System.Exception e) { Err.dumpStack(e); } base.add(f); }
////////////////////////////////////////////////////////////////////////// // Lifecycle ////////////////////////////////////////////////////////////////////////// public static Service install(Service self) { try { List types = FanObj.@typeof(self).inheritance(); lock (m_lock) { // if already installed, short circuit if (self.isInstalled()) { return(self); } // add to byService map byService[self] = new State(self); // add to map for each type service implements for (int i = 0; i < types.sz(); ++i) { Type t = (Type)types.get(i); if (!isServiceType(t)) { continue; } Node node = new Node(self); Node x = (Node)byType[t.qname()]; if (x == null) { byType[t.qname()] = node; } else { while (x.next != null) { x = x.next; } x.next = node; } } } } catch (System.Exception e) { Err.dumpStack(e); } return(self); }
public virtual List readAllLines() { try { List list = new List(Sys.StrType); string line; while ((line = readLine()) != null) { list.add(line); } return(list); } finally { try { close(); } catch (System.Exception e) { Err.dumpStack(e); } } }
public virtual Buf readAllBuf() { try { long size = FanInt.Chunk.longValue(); Buf buf = Buf.make(size); while (readBuf(buf, size) != null) { ; } buf.flip(); return(buf); } finally { try { close(); } catch (System.Exception e) { Err.dumpStack(e); } } }
// This is mostly for testing right. // java.util.TimeZone java() // { // return java.util.TimeZone.getTimeZone(name.val); // } ////////////////////////////////////////////////////////////////////////// // Aliases ////////////////////////////////////////////////////////////////////////// private static void loadAliases() { Hashtable map = new Hashtable(); try { // read as props file //String sep = java.io.File.separator; //Map props = Env.cur().props(Sys.sysPod, Uri.fromStr("timezone-aliases.props"), Duration.Zero); Map props = Sys.m_sysPod.props(Uri.fromStr("timezone-aliases.props"), Duration.Zero); System.Diagnostics.Debug.WriteLine(props.size()); System.Console.WriteLine(props.size()); // map both simple name and full names to aliases map IDictionaryEnumerator it = props.pairsIterator(); while (it.MoveNext()) { string key = (string)it.Key; string val = (string)it.Value; // map by fullName map[key] = val; // map by simple name int slash = key.LastIndexOf('/'); if (slash > 0) { map[key.Substring(slash + 1)] = val; } } } catch (Exception e) { Err.dumpStack(e); Console.WriteLine("ERROR: Cannot read timezone-aliases.props"); } // save to field and force memory barrier sync TimeZone.aliases = map; lock (TimeZone.aliases) {} //synchronized(TimeZone.aliases) {} }
////////////////////////////////////////////////////////////////////////// // Documentation ////////////////////////////////////////////////////////////////////////// public override string doc() { if (!m_docLoaded) { try { Stream input = m_pod.fpod.m_store.read("doc/" + m_name + ".apidoc"); if (input != null) { try { FDoc.read(input, this); } finally { input.Close(); } } } catch (Exception e) { Err.dumpStack(e); } m_docLoaded = true; } return(m_doc); }
public virtual void log(LogRec rec) { if (!enabled(rec.m_level)) { return; } Func[] handlers = Log.m_handlers; for (int i = 0; i < handlers.Length; ++i) { try { handlers[i].call(rec); } catch (System.Exception e) { Err.dumpStack(e); } } }
static Locale() { Locale x; try { string name = CultureInfo.CurrentCulture.Name; if (name.Length != 5) { name = CultureInfo.CurrentCulture.TwoLetterISOLanguageName; } x = fromStr(name); } catch (Exception e) { Err.dumpStack(e); x = fromStr("en"); } defaultLocale = x; }
////////////////////////////////////////////////////////////////////////// // Documentation ////////////////////////////////////////////////////////////////////////// public string doc() { if (!m_docLoaded) { try { Stream input = fpod.m_store.read("doc/pod.fandoc"); if (input != null) { m_doc = SysInStream.make(input, Long.valueOf(1024L)).readAllStr(); } } catch (Exception e) { Err.dumpStack(e); } m_docLoaded = true; } return(m_doc); }
public override Future get() { Future f = base.get(); if (f != null) { try { object key = toKey(f.m_msg); if (key != null) { pending.Remove(key); } } catch (System.Exception e) { Err.dumpStack(e); } } return(f); }
////////////////////////////////////////////////////////////////////////// // Extension ////////////////////////////////////////////////////////////////////////// public static MimeType forExt(string s) { if (s == null) { return(null); } try { string val = (string)Sys.m_sysPod.props(m_etcUri, Duration.m_oneMin).get(FanStr.lower(s)); if (val == null) { return(null); } return(MimeType.fromStr(val)); } catch (System.Exception e) { System.Console.WriteLine("MimeType.forExt: " + s); Err.dumpStack(e); return(null); } }
public Facet decode(Type type, string s) { try { // if no string use make/defVal if (s.Length == 0) { return((Facet)type.make()); } // decode using normal Fantom serialization return((Facet)ObjDecoder.decode(s)); } catch (System.Exception e) { string msg = "ERROR: Cannot decode facet " + type + ": " + s; System.Console.WriteLine(msg); Err.dumpStack(e); m_map.Remove(type); throw IOErr.make(msg).val; } }