private bool ParseNode(BinaryReader br, ParsedData parsed) { int bytes = 0; Int64 len = ParseNumber(br, false); Int64 id = parsed.ApplyDelta(ParseNumberAndTrackBytes(br, true, out bytes), "nodeid"); var node = ParseNodeContents(br, parsed, bytes, (int)len); node.id = id; parsed.Nodes.Add(id, node); return(true); }
private bool ParseBoundary(BinaryReader br, ParsedData parsed) { // len, minlon, minlat, maxlon, maxlat Int64 len = ParseNumber(br, false); var details = new FileInfo(); details.bounds.minLon = ParseNumber(br, true) / 10000000d; details.bounds.minLat = ParseNumber(br, true) / 10000000d; details.bounds.maxLon = ParseNumber(br, true) / 10000000d; details.bounds.maxLat = ParseNumber(br, true) / 10000000d; parsed.Details = details; return(true); }
private string ParseUIDStringAndTrackBytes(BinaryReader br, ParsedData parsed, out int bytes) { var byteStr = new List <byte>(); bytes = 0; int bytesRead = 0; if (br.PeekChar() != 0x00) // table entry { var entry = ParseNumberAndTrackBytes(br, false, out bytes); try { return(parsed.StringTable[parsed.StringTable.Count - Convert.ToInt32(entry)]); } catch (Exception ex) { return(string.Empty); } } br.ReadByte(); // 0x00 bytes++; byte curr; var uid = ParseNumberAndTrackBytes(br, false, out bytesRead).ToString(); bytes += bytesRead; br.ReadByte(); // 0x00 bytes++; while ((curr = br.ReadByte()) != 0x00) { byteStr.Add(curr); bytes++; } bytes++; // to catch the 0x00 var uidString = uid + '\0' + Encoding.UTF8.GetString(byteStr.ToArray(), 0, byteStr.Count); if (!parsed.StringTable.Contains(uidString)) { parsed.StringTable.Add(uidString); } return(uidString); }
private Way ParseWayContents(BinaryReader br, ParsedData parsed, int bytesSoFar, int bytesTotal) { int bytes = 0; var way = new Way(); way.header = ParseHeader(br, parsed, out bytes); bytesSoFar += bytes; // reference parsing way.refs = ParseWayRefs(br, parsed, out bytes); bytesSoFar += bytes; way.tags = ParseTags(br, parsed, bytesSoFar, bytesTotal); return(way); }
private bool ParseWay(BinaryReader br, ParsedData parsed) { int bytes = 0; int bytesSoFar = 0; Int64 len = ParseNumber(br, false); Int64 id = parsed.ApplyDelta(ParseNumberAndTrackBytes(br, true, out bytes), "wayid"); bytesSoFar += bytes; // bytes... var way = ParseWayContents(br, parsed, bytesSoFar, (int)len); way.id = id; parsed.Ways.Add(way); return(true); }
private string ParseStringPair(BinaryReader br, ParsedData parsed, out int bytes) { var byteStr = new List <byte>(); bytes = 0; if (br.PeekChar() != 0x00) // table entry { var entry = ParseNumberAndTrackBytes(br, false, out bytes); return(parsed.StringTable[parsed.StringTable.Count - Convert.ToInt32(entry)]); } br.ReadByte(); bytes++; byte curr; while ((curr = br.ReadByte()) != 0x00) { byteStr.Add(curr); bytes++; } bytes++; // to catch the 0x00 byteStr.Add(0x00); //bytes++; while ((curr = br.ReadByte()) != 0x00) { byteStr.Add(curr); bytes++; } bytes++; // to catch the 0x00 var retString = Encoding.UTF8.GetString(byteStr.ToArray(), 0, byteStr.Count); var splitString = retString.Split('\0'); if (!parsed.StringTable.Contains(retString)) { parsed.StringTable.Add(retString); } return(retString); }
/// <returns>null if no tags</returns> private Dictionary <string, string> ParseTags(BinaryReader br, ParsedData parsed, int bytesSoFar, int bytesTotal) { int bytes = 0; Dictionary <string, string> tags = null; if (bytesSoFar < bytesTotal) { tags = new Dictionary <string, string>(); } while (bytesSoFar < bytesTotal) { if (br.PeekChar() == 0x00) { var tag = ParseStringPair(br, parsed, out bytes); bytesSoFar += bytes; var splitTag = tag.Split('\0'); tags[splitTag[0]] = splitTag[1]; if (!parsed.StringTable.Contains(tag)) { parsed.StringTable.Add(tag); } } else { var entry = ParseNumberAndTrackBytes(br, false, out bytes); bytesSoFar += bytes; try { var tag = parsed.StringTable[parsed.StringTable.Count - Convert.ToInt32(entry)]; var splitTag = tag.Split('\0'); tags[splitTag[0]] = splitTag[1]; } catch (Exception ex) { ; } } } return(tags); }
private bool ParseFile(BinaryReader br, ParsedData parsed) { bool reading = true; bool isBoundsReaded = false; var streamLength = br.BaseStream.Length; while (reading && br.BaseStream.Position < streamLength) { byte b = br.ReadByte(); switch (b) { case 0xFF: // reset parsed.ClearTables(); break; case 0xDB: if (!isBoundsReaded && !ParseBoundary(br, parsed)) { return(false); } isBoundsReaded = true; break; case 0x10: if (!ParseNode(br, parsed)) { return(false); } break; case 0x11: if (!ParseWay(br, parsed)) { return(false); } break; } } return(true); }
private Node ParseNodeContents(BinaryReader br, ParsedData parsed, int bytesSoFar, int bytesTotal) { int bytes = 0; var node = new Node(); node.header = ParseHeader(br, parsed, out bytes); bytesSoFar += bytes; var l = parsed.ApplyDelta(ParseNumberAndTrackBytes(br, true, out bytes), "nodelon"); node.lon = l / 10000000d; bytesSoFar += bytes; node.lat = parsed.ApplyDelta(ParseNumberAndTrackBytes(br, true, out bytes), "nodelat") / 10000000d; bytesSoFar += bytes; node.tags = ParseTags(br, parsed, bytesSoFar, bytesTotal); return(node); }
private Header ParseHeader(BinaryReader br, ParsedData parsed, out int bytes) { bytes = 0; int bytesRead = 0; var header = new Header(); header.version = ParseNumberAndTrackBytes(br, false, out bytesRead); bytes += bytesRead; if (header.version != 0) { header.timestamp = parsed.ApplyDelta(ParseNumberAndTrackBytes(br, true, out bytesRead), "nodetimestamp"); // seconds from 1970 bytes += bytesRead; // specs unclear if you filter author with timestamp or version if (header.timestamp != 0) { header.changeset = parsed.ApplyDelta(ParseNumberAndTrackBytes(br, true, out bytesRead), "nodechangeset"); bytes += bytesRead; var authorInfo = ParseUIDStringAndTrackBytes(br, parsed, out bytesRead).Split('\0'); // [0] uid, [1] user name bytes += bytesRead; if (authorInfo.Length > 0) { header.author.id = authorInfo[0]; } if (authorInfo.Length > 1) { header.author.name = authorInfo[1]; } } } return(header); }
private string ParseUIDString(BinaryReader br, ParsedData parsed) { int bytes = 0; return(ParseUIDStringAndTrackBytes(br, parsed, out bytes)); }