private void BuildOldDate(String dStr, int first, int len) { //int middle = dStr.IndexOf((System.Char) '/', first + 1); int middle = SupportClass.StringIndexOf(dStr, '/', first + 1); if (middle > first + 2 && middle < len) { try { year = Int32.Parse(dStr.Substring(middle + 1)) + 1900; month = Int32.Parse(dStr.Substring(first + 1, (middle) - (first + 1))); mday = Int32.Parse(dStr.Substring(0, (first) - (0))); } catch (FormatException) { year = month = mday = -1; } } }
private void BuildNewDate(String dStr, int first, int len) { // find the middle separator //int middle = dStr.IndexOf((System.Char) '-', first + 1); int middle = SupportClass.StringIndexOf(dStr, '-', first + 1); if (middle > first + 2 && middle < len) { try { // if this date string includes a time... if (middle + 3 < len && dStr[middle + 3] == 'T') { // ... try to parse the time try { ParseTime(dStr.Substring(middle + 4)); } catch (FitsException) { throw new FitsException("Bad time in FITS date string \"" + dStr + "\""); } // we got the time; mark the end of the date string len = middle + 3; } // parse date string year = Int32.Parse(dStr.Substring(0, (first) - (0))); month = Int32.Parse(dStr.Substring(first + 1, (middle) - (first + 1))); mday = Int32.Parse(dStr.Substring(middle + 1, (len) - (middle + 1))); } catch (FormatException) { // yikes, something failed; reset everything year = month = mday = hour = minute = second = millisecond = -1; } } }
private void ParseTime(String tStr) { int first = tStr.IndexOf(':'); if (first < 0) { throw new FitsException("Bad time"); } int len = tStr.Length; //int middle = tStr.IndexOf((System.Char) ':', first + 1); int middle = SupportClass.StringIndexOf(tStr, ':', first + 1); if (middle > first + 2 && middle < len) { if (middle + 3 < len && tStr[middle + 3] == '.') { double d = Double.Parse(tStr.Substring(middle + 3)); millisecond = (int)(d * 1000); len = middle + 3; } try { hour = Int32.Parse(tStr.Substring(0, (first) - (0))); minute = Int32.Parse(tStr.Substring(first + 1, (middle) - (first + 1))); second = Int32.Parse(tStr.Substring(middle + 1, (len) - (middle + 1))); } catch (FormatException) { hour = minute = second = millisecond = -1; } } }
private String HierarchToString() { System.Text.StringBuilder b = new System.Text.StringBuilder(80); int p = 0; String space = ""; while (p < key.Length) { int q = SupportClass.StringIndexOf(key, '.', p); if (q < 0) { b.Append(space + key.Substring(p)); break; } else { b.Append(space + key.Substring(p, (q) - (p))); } space = " "; p = q + 1; } if (val != null || nullable) { b.Append("= "); if (val != null) { // Try to align values int avail = 80 - (b.Length + val.Length); if (isString) { avail -= 2; } if (comment != null) { avail -= 3 + comment.Length; } if (avail > 0 && b.Length < 29) { b.Append(space80.Substring(0, (Math.Min(avail, 29 - b.Length)) - (0))); } if (isString) { b.Append('\''); } else if (avail > 0 && val.Length < 10) { b.Append(space80.Substring(0, (Math.Min(avail, 10 - val.Length)) - (0))); } b.Append(val); if (isString) { b.Append('\''); } } else if (b.Length < 30) { // Pad out a null value b.Append(space80.Substring(0, (30 - b.Length) - (0))); } } if (comment != null) { b.Append(" / " + comment); } if (b.Length < 80) { b.Append(space80.Substring(0, (80 - b.Length) - (0))); } String card = new String(b.ToString().ToCharArray()); if (card.Length > 80) { card = card.Substring(0, (80) - (0)); } return(card); }
/// <summary>Create a HeaderCard from a FITS card image</summary> /// <param name="card">the 80 character card image</param> public HeaderCard(String card) { key = null; val = null; comment = null; isString = false; // suggested in .99.2 version // Truncated String in one argument constructor to // a maximum of 80 characters. if (card.Length > 80) { card = card.Substring(0, 80); } if (FitsFactory.UseHierarch && card.Length > 9 && card.Substring(0, (9) - (0)).Equals("HIERARCH ")) { HierarchCard(card); return; } // We are going to assume that the value has no blanks in // it unless it is enclosed in quotes. Also, we assume that // a / terminates the string (except inside quotes) // treat short lines as special keywords if (card.Length < 9) { key = card; return; } // extract the key key = card.Substring(0, (8) - (0)).Trim(); // if it is an empty key, assume the remainder of the card is a comment if (key.Length == 0) { key = ""; comment = card.Substring(8); return; } // change suggested in .97 version: // Non-key/value pair lines are treated as keyed comments if (key.Equals("COMMENT") || key.Equals("HISTORY") || !card.Substring(8, (10) - (8)).Equals("= ")) { comment = card.Substring(8).Trim(); return; } // extract the value/comment part of the string String valueAndComment = card.Substring(10).Trim(); // If there is no value/comment part, we are done. if (valueAndComment.Length == 0) { val = ""; return; } int vend = -1; //bool quote = false; // If we have a ' then find the matching '. if (valueAndComment[0] == '\'') { int offset = 1; while (offset < valueAndComment.Length) { // look for next single-quote character vend = SupportClass.StringIndexOf(valueAndComment, '\'', offset); // if the quote character is the last character on the line... if (vend == valueAndComment.Length - 1) { break; } // if we did not find a matching single-quote... if (vend == -1) { // pretend this is a comment card key = null; comment = card; return; } // if this is not an escaped single-quote, we are done if (valueAndComment[vend + 1] != '\'') { break; } // skip past escaped single-quote offset = vend + 2; } // break apart character string val = valueAndComment.Substring(1, (vend) - (1)).Trim(); if (vend + 1 >= valueAndComment.Length) { comment = null; } else { comment = valueAndComment.Substring(vend + 1).Trim(); if (comment[0] == '/') { if (comment.Length > 1) { comment = comment.Substring(1); } else { comment = ""; } } if (comment.Length == 0) { comment = null; } } isString = true; } else { // look for a / to terminate the field. int slashLoc = valueAndComment.IndexOf((System.Char) '/'); if (slashLoc != -1) { comment = valueAndComment.Substring(slashLoc + 1).Trim(); val = valueAndComment.Substring(0, (slashLoc) - (0)).Trim(); } else { val = valueAndComment; } } }