static int ReadKey(GTIF gt, GTIFReadMethod scan, object aux) { string message=scan(aux); if(message==null) return -1; if(message.Length>=12) if(message.ToLower().Substring(0, 12)==FMT_KEYEND.ToLower().Substring(0, 12)) return 0; try { int firstp=message.IndexOfAny(new char[] { '(', ' ', '\t' }); if(firstp<0) return StringError(message); string name=message.Substring(0, firstp).Trim(); string message1=message.Substring(firstp); int colon=message1.IndexOf(':'); if(colon<0) return StringError(message1); string head=message1.Substring(0, colon); head=head.Trim(' ', '\t', '(', ')'); string[] spl=head.Split(','); if(spl.Length!=2) return StringError(head); string type=spl[0]; int count=int.Parse(spl[1]); // skip white space string data=message1.Substring(colon+1).Trim(); if(data.Length==0) return StringError(message); if(GTIFKeyCode(name)<0) return StringError(name); geokey_t key=(geokey_t)GTIFKeyCode(name); if(GTIFTypeCode(type)<0) return StringError(type); tagtype_t ktype=(tagtype_t)GTIFTypeCode(type); switch(ktype) { case tagtype_t.TYPE_ASCII: { string cdata=""; int firstDoubleQuote=data.IndexOf('"'); if(firstDoubleQuote<0) return StringError(data); data=data.Substring(firstDoubleQuote+1); bool wasesc=false; char c='\0'; for(int i=0; i<data.Length; i++) { c=data[i]; if(wasesc) { if(c=='\\') cdata+='\\'; else if(c=='"') cdata+='"'; else if(c=='n') cdata+='\n'; else if(c=='t') cdata+='\t'; else if(c=='b') cdata+='\b'; else if(c=='r') cdata+='\r'; else if(c=='0') cdata+='\0'; wasesc=false; continue; } if(c=='\\') { wasesc=true; continue; } if(c=='\0') break; if(c=='"') break; cdata+=c; if(cdata.Length==count) { c=data[i+1]; break; } } if(cdata.Length<count) return StringError(message); if(c!='"') return StringError(message); GTIFKeySet(gt, key, cdata); } break; case tagtype_t.TYPE_DOUBLE: { double[] dptr=new double[count]; int i=0; for(; count>0; count-=3) { spl=data.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); if(spl.Length!=Math.Min(3, count)) return StringError(data); foreach(string part in spl) { message=part; dptr[i++]=GTIFAtof(part); } if(count>3) message=data=scan(aux); } GTIFKeySet(gt, key, dptr); } break; case tagtype_t.TYPE_SHORT: if(count==1) { int icode=GTIFValueCode(key, data); if(icode<0) return StringError(data); ushort code=(ushort)icode; GTIFKeySet(gt, key, code); } else // multi-valued short - no such thing yet { ushort[] sptr=new ushort[count]; int i=0; for(; count>0; count-=3) { spl=data.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); if(spl.Length!=Math.Min(3, count)) return StringError(data); foreach(string part in spl) { message=part; sptr[i++]=ushort.Parse(part); } if(count>3) message=data=scan(aux); } GTIFKeySet(gt, key, sptr); } break; default: return -1; } } catch { return StringError(message); } return 1; }
static int ReadTag(GTIF gt, GTIFReadMethod scan, object aux) { string message=scan(aux); if(message==null) return -1; if(message.Length>=12) if(message.ToLower().Substring(0, 12)==FMT_TAGEND.ToLower().Substring(0, 12)) return 0; try { int firstp=message.IndexOfAny(new char[] { '(', ' ', '\t' }); if(firstp<0) return StringError(message); string tagname=message.Substring(0, firstp).Trim(); message=message.Substring(firstp); int colon=message.IndexOf(':'); if(colon<0) return StringError(message); message=message.Substring(0, colon); message=message.Trim(' ', '\t', '(', ')'); string[] spl=message.Split(','); if(spl.Length!=2) return StringError(message); int nrows=int.Parse(spl[0]); int ncols=int.Parse(spl[1]); int tag=GTIFTagCode(tagname); if(tag<0) return StringError(tagname); int count=nrows*ncols; double[] dptr=new double[count]; for(int i=0; i<nrows; i++) { message=scan(aux); if(message==null) return -1; spl=message.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); if(spl.Length!=ncols) StringError(message); for(int j=0; j<ncols; j++) { message=spl[j]; dptr[i*ncols+j]=GTIFAtof(message); } } gt.gt_methods.set(gt.gt_tif, (ushort)tag, count, dptr); } catch { return StringError(message); } return 1; }
public static bool GTIFImport(GTIF gtif, GTIFReadMethod scan, object aux) { if(scan==null) scan=DefaultRead; if(aux==null) aux=Console.In; string message=scan(aux); if(message==null||message.Length<20) return false; if(message.ToLower().Substring(0, 20)!=FMT_GEOTIFF.ToLower().Substring(0, 20)) return false; message=scan(aux); if(message==null||message.Length<10) return false; if(message.ToLower().Substring(0, 8)!=FMT_VERSION.ToLower().Substring(0, 8)) return false; message=message.Substring(9); try { gtif.gt_version=ushort.Parse(message); } catch { return false; } message=scan(aux); if(message==null||message.Length<15) return false; if(message.ToLower().Substring(0, 13)!=FMT_REV.ToLower().Substring(0, 13)) return false; message=message.Substring(14); try { string[] spl=message.Split('.'); if(spl.Length!=2) return false; gtif.gt_rev_major=ushort.Parse(spl[0]); gtif.gt_rev_minor=ushort.Parse(spl[1]); } catch { return false; } message=scan(aux); if(message==null||message.Length<19) return false; if(message.ToLower().Substring(0, 19)!=FMT_TAGS.ToLower().Substring(0, 19)) return false; int status; while((status=ReadTag(gtif, scan, aux))>0) ; if(status<0) return false; message=scan(aux); if(message==null||message.Length<18) return false; if(message.ToLower().Substring(0, 18)!=FMT_KEYS.ToLower().Substring(0, 18)) return false; while((status=ReadKey(gtif, scan, aux))>0) ; return (status==0); // success }
static int ReadKey(GTIF gt, GTIFReadMethod scan, object aux) { string message = scan(aux); if (message == null) { return(-1); } if (message.Length >= 12) { if (message.ToLower().Substring(0, 12) == FMT_KEYEND.ToLower().Substring(0, 12)) { return(0); } } try { int firstp = message.IndexOfAny(new char[] { '(', ' ', '\t' }); if (firstp < 0) { return(StringError(message)); } string name = message.Substring(0, firstp).Trim(); string message1 = message.Substring(firstp); int colon = message1.IndexOf(':'); if (colon < 0) { return(StringError(message1)); } string head = message1.Substring(0, colon); head = head.Trim(' ', '\t', '(', ')'); string[] spl = head.Split(','); if (spl.Length != 2) { return(StringError(head)); } string type = spl[0]; int count = int.Parse(spl[1]); // skip white space string data = message1.Substring(colon + 1).Trim(); if (data.Length == 0) { return(StringError(message)); } if (GTIFKeyCode(name) < 0) { return(StringError(name)); } geokey_t key = (geokey_t)GTIFKeyCode(name); if (GTIFTypeCode(type) < 0) { return(StringError(type)); } tagtype_t ktype = (tagtype_t)GTIFTypeCode(type); switch (ktype) { case tagtype_t.TYPE_ASCII: { string cdata = ""; int firstDoubleQuote = data.IndexOf('"'); if (firstDoubleQuote < 0) { return(StringError(data)); } data = data.Substring(firstDoubleQuote + 1); bool wasesc = false; char c = '\0'; for (int i = 0; i < data.Length; i++) { c = data[i]; if (wasesc) { if (c == '\\') { cdata += '\\'; } else if (c == '"') { cdata += '"'; } else if (c == 'n') { cdata += '\n'; } else if (c == 't') { cdata += '\t'; } else if (c == 'b') { cdata += '\b'; } else if (c == 'r') { cdata += '\r'; } else if (c == '0') { cdata += '\0'; } wasesc = false; continue; } if (c == '\\') { wasesc = true; continue; } if (c == '\0') { break; } if (c == '"') { break; } cdata += c; if (cdata.Length == count) { c = data[i + 1]; break; } } if (cdata.Length < count) { return(StringError(message)); } if (c != '"') { return(StringError(message)); } GTIFKeySet(gt, key, cdata); } break; case tagtype_t.TYPE_DOUBLE: { double[] dptr = new double[count]; int i = 0; for (; count > 0; count -= 3) { spl = data.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); if (spl.Length != Math.Min(3, count)) { return(StringError(data)); } foreach (string part in spl) { message = part; dptr[i++] = GTIFAtof(part); } if (count > 3) { message = data = scan(aux); } } GTIFKeySet(gt, key, dptr); } break; case tagtype_t.TYPE_SHORT: if (count == 1) { int icode = GTIFValueCode(key, data); if (icode < 0) { return(StringError(data)); } ushort code = (ushort)icode; GTIFKeySet(gt, key, code); } else // multi-valued short - no such thing yet { ushort[] sptr = new ushort[count]; int i = 0; for (; count > 0; count -= 3) { spl = data.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); if (spl.Length != Math.Min(3, count)) { return(StringError(data)); } foreach (string part in spl) { message = part; sptr[i++] = ushort.Parse(part); } if (count > 3) { message = data = scan(aux); } } GTIFKeySet(gt, key, sptr); } break; default: return(-1); } } catch { return(StringError(message)); } return(1); }
static int ReadTag(GTIF gt, GTIFReadMethod scan, object aux) { string message = scan(aux); if (message == null) { return(-1); } if (message.Length >= 12) { if (message.ToLower().Substring(0, 12) == FMT_TAGEND.ToLower().Substring(0, 12)) { return(0); } } try { int firstp = message.IndexOfAny(new char[] { '(', ' ', '\t' }); if (firstp < 0) { return(StringError(message)); } string tagname = message.Substring(0, firstp).Trim(); message = message.Substring(firstp); int colon = message.IndexOf(':'); if (colon < 0) { return(StringError(message)); } message = message.Substring(0, colon); message = message.Trim(' ', '\t', '(', ')'); string[] spl = message.Split(','); if (spl.Length != 2) { return(StringError(message)); } int nrows = int.Parse(spl[0]); int ncols = int.Parse(spl[1]); int tag = GTIFTagCode(tagname); if (tag < 0) { return(StringError(tagname)); } int count = nrows * ncols; double[] dptr = new double[count]; for (int i = 0; i < nrows; i++) { message = scan(aux); if (message == null) { return(-1); } spl = message.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); if (spl.Length != ncols) { StringError(message); } for (int j = 0; j < ncols; j++) { message = spl[j]; dptr[i * ncols + j] = GTIFAtof(message); } } gt.gt_methods.set(gt.gt_tif, (ushort)tag, count, dptr); } catch { return(StringError(message)); } return(1); }
public static bool GTIFImport(GTIF gtif, GTIFReadMethod scan, object aux) { if (scan == null) { scan = DefaultRead; } if (aux == null) { aux = Console.In; } string message = scan(aux); if (message == null || message.Length < 20) { return(false); } if (message.ToLower().Substring(0, 20) != FMT_GEOTIFF.ToLower().Substring(0, 20)) { return(false); } message = scan(aux); if (message == null || message.Length < 10) { return(false); } if (message.ToLower().Substring(0, 8) != FMT_VERSION.ToLower().Substring(0, 8)) { return(false); } message = message.Substring(9); try { gtif.gt_version = ushort.Parse(message); } catch { return(false); } message = scan(aux); if (message == null || message.Length < 15) { return(false); } if (message.ToLower().Substring(0, 13) != FMT_REV.ToLower().Substring(0, 13)) { return(false); } message = message.Substring(14); try { string[] spl = message.Split('.'); if (spl.Length != 2) { return(false); } gtif.gt_rev_major = ushort.Parse(spl[0]); gtif.gt_rev_minor = ushort.Parse(spl[1]); } catch { return(false); } message = scan(aux); if (message == null || message.Length < 19) { return(false); } if (message.ToLower().Substring(0, 19) != FMT_TAGS.ToLower().Substring(0, 19)) { return(false); } int status; while ((status = ReadTag(gtif, scan, aux)) > 0) { ; } if (status < 0) { return(false); } message = scan(aux); if (message == null || message.Length < 18) { return(false); } if (message.ToLower().Substring(0, 18) != FMT_KEYS.ToLower().Substring(0, 18)) { return(false); } while ((status = ReadKey(gtif, scan, aux)) > 0) { ; } return(status == 0); // success }