private static int next(PushbackTextReader r) { int c = nextConsumed(r); r.Unread(c); return(c); }
private static string readstring(PushbackTextReader r) { int first = r.Read(); if (first == -1) { throw new ArgumentException(BADEND + ", at line " + r.Line); } if (breakstring(first)) { throw new ArgumentException(BADCHAR + ((char)first) + "', at line " + r.Line); } StringBuilder sb = new StringBuilder(); if (first != '\'' && first != '"') { r.Unread(first); first = 0; } int c; while (true) { c = r.Read(); if (c == '\\') { c = r.Read(); if (c == -1) { throw new ArgumentException(BADEND + ", at line " + r.Line); } } else if (first != 0 && c == first) { break; } else if (first == 0) { if (isWhite(c) || c == -1) { break; } if (breakstring(c)) { r.Unread(c); break; } } else if (c == -1) { throw new ArgumentException(BADEND + ", at line " + r.Line); } sb.Append((char)c); } return(sb.ToString()); }
private static int swapComment(PushbackTextReader r) { int c; do { c = r.Read(); }while(c != '\n' && c != -1); return(c); }
private static int nextConsumed(PushbackTextReader r) { int c; do { c = r.Read(); if (c == '#') { c = swapComment(r); } }while(isWhite(c)); return(c); }
private static Hashtable readdict(PushbackTextReader r) { Hashtable h = new Hashtable(); int c = r.Read(); if (c != '{') { throw new ArgumentException(BADCHAR + (char)c + "', at line " + r.Line); } while (true) { c = next(r); if (c == '}') { r.Read(); break; } string key = readstring(r); c = next(r); if (c != '=') { throw new ArgumentException(BADCHAR + (char)c + "', at line " + r.Line); } r.Read(); next(r); object aValue = readobject(r); h[key] = aValue; c = next(r); if (c == '}') { r.Read(); break; } if (c == ';' || c == ',') { r.Read(); continue; } } return(h); }
private static byte[] readbytes(PushbackTextReader r) { MemoryStream bos = new MemoryStream(); int c = r.Read(); if (c != '<') { throw new ArgumentException(BADCHAR + (char)c + "', at line " + r.Line); } char[] tab = new char[2]; byte[] outTab = new byte[1]; int Read; while (true) { Read = nextConsumed(r); if (Read == -1) { throw new ArgumentException(BADEND + ", at line " + r.Line); } if (Read == '>') { break; } tab[0] = (char)Read; Read = nextConsumed(r); if (Read == -1) { throw new ArgumentException(BADEND + ", at line " + r.Line); } tab[1] = (char)Read; outTab[0] = hexaToInt(tab[0], r); outTab[0] <<= 4; outTab[0] |= hexaToInt(tab[1], r); bos.Write(outTab, 0, 1); } return(bos.ToArray()); }
private static byte hexaToInt(char c, PushbackTextReader r) { int n; if ('0' <= c && c <= '9') { n = c - '0'; } else if ('a' <= c && c <= 'f') { n = c - 'a' + 10; } else if ('A' <= c && c <= 'F') { n = c - 'A' + 10; } else { throw new ArgumentException(BADCHAR + (char)c + "', at line " + r.Line); } return((byte)n); }
private static object readobject(PushbackTextReader r) { int c = next(r); switch (c) { case -1: return(null); case '{': return(readdict(r)); case '(': return(readarray(r)); case '<': return(readbytes(r)); default: return(readstring(r)); } }
private static ArrayList readarray(PushbackTextReader r) { ArrayList v = new ArrayList(); int c = r.Read(); if (c != '(') { throw new ArgumentException(BADCHAR + (char)c + "', at line " + r.Line); } while (true) { c = next(r); if (c == ')') { r.Read(); break; } object item = readobject(r); v.Add(item); c = next(r); if (c == ')') { r.Read(); break; } if (c == ';' || c == ',') { r.Read(); continue; } // be tolerant // throw new ArgumentException(BADCHAR + (char)c + "', at line " + r.Line); } return(v); }