/// <summary> /// Extract the field from the delimited file, removing separators and quotes /// and any duplicate quotes within the record /// </summary> /// <param name="line">line containing record input</param> /// <returns>Extract information</returns> internal override ExtractedInfo ExtractFieldString(LineInfo line) { if (IsOptional && line.IsEOL()) { return(ExtractedInfo.Empty); } if (QuoteChar == '\0') { return(BasicExtractString(line)); } else { if (TrimMode == TrimMode.Both || TrimMode == TrimMode.Left) { line.TrimStart(TrimChars); } string quotedStr = QuoteChar.ToString(); if (line.StartsWith(quotedStr)) { var res = StringHelper.ExtractQuotedString(line, QuoteChar, QuoteMultiline == MultilineMode.AllowForBoth || QuoteMultiline == MultilineMode.AllowForRead); if (TrimMode == TrimMode.Both || TrimMode == TrimMode.Right) { line.TrimStart(TrimChars); } if (!IsLast && !line.StartsWith(Separator) && !line.IsEOL()) { throw new BadUsageException(line, "The field " + this.FieldInfo.Name + " is quoted but the quoted char: " + quotedStr + " not is just before the separator (You can use [FieldTrim] to avoid this error)"); } return(res); } else { if (QuoteMode == QuoteMode.OptionalForBoth || QuoteMode == QuoteMode.OptionalForRead) { return(BasicExtractString(line)); } else if (line.StartsWithTrim(quotedStr)) { throw new BadUsageException(string.Format("The field '{0}' has spaces before the QuotedChar at line {1}. Use the TrimAttribute to by pass this error. Field String: {2}", FieldInfo.Name, line.mReader.LineNumber, line.CurrentString)); } else { throw new BadUsageException(string.Format("The field '{0}' not begin with the QuotedChar at line {1}. You can use FieldQuoted(QuoteMode.OptionalForRead) to allow optional quoted field.. Field String: {2}", FieldInfo.Name, line.mReader.LineNumber, line.CurrentString)); } } } }
protected override ExtractedInfo ExtractFieldString(LineInfo line) { if (mIsOptional && line.IsEOL() ) return ExtractedInfo.Empty; if (mQuoteChar == '\0') return BasicExtractString(line); else { //TODO: UnComment and Fix if (mTrimMode == TrimMode.Both || mTrimMode == TrimMode.Left) { //int pos = line.mCurrentPos; line.TrimStart(mTrimChars); // from2 = from.TrimStart(mTrimChars); //res.CharsRemoved = line.mCurrentPos - pos; } string quotedStr = mQuoteChar.ToString(); if (line.StartsWith(quotedStr)) { // ExtractedInfo res = null; // res = new ExtractedInfo(line, line.mCurrentPos); return StringHelper.ExtractQuotedString(line, mQuoteChar, mQuoteMultiline == MultilineMode.AllowForBoth || mQuoteMultiline == MultilineMode.AllowForRead); // if (mQuoteMultiline == MultilineMode.AllowForBoth || mQuoteMultiline == MultilineMode.AllowForRead) // { // // //res.ExtractedString = ei.ExtractedString; // //res.CharsRemoved += ei.CharsRemoved; // //res.ExtraLines = ei.ExtraLines; // //res.NewRestOfLine = ei.NewRestOfLine; // } // else // { // return StringHelper.ExtractQuotedString(from2, mQuoteChar, out index); // //res.CharsRemoved += index; // } // return res; } else { if (mQuoteMode == QuoteMode.OptionalForBoth || mQuoteMode == QuoteMode.OptionalForRead) return BasicExtractString(line); else if (line.StartsWithTrim(quotedStr)) throw new BadUsageException("The field '" + this.mFieldInfo.Name + "' has spaces before the QuotedChar at line "+ line.mReader.LineNumber.ToString() + ". Use the TrimAttribute to by pass this error. Field String: " + line.CurrentString); else throw new BadUsageException("The field '" + this.mFieldInfo.Name + "' not begin with the QuotedChar at line "+ line.mReader.LineNumber.ToString() + ". You can use FieldQuoted(QuoteMode.OptionalForRead) to allow optional quoted field.. Field String: " + line.CurrentString); } } }
internal override ExtractedInfo ExtractFieldString(LineInfo line) { if (mIsOptional && line.IsEOL() ) return ExtractedInfo.Empty; if (mQuoteChar == '\0') return BasicExtractString(line); else { if (mTrimMode == TrimMode.Both || mTrimMode == TrimMode.Left) { line.TrimStart(mTrimChars); } string quotedStr = mQuoteChar.ToString(); if (line.StartsWith(quotedStr)) { return StringHelper.ExtractQuotedString(line, mQuoteChar, mQuoteMultiline == MultilineMode.AllowForBoth || mQuoteMultiline == MultilineMode.AllowForRead); } else { if (mQuoteMode == QuoteMode.OptionalForBoth || mQuoteMode == QuoteMode.OptionalForRead) return BasicExtractString(line); else if (line.StartsWithTrim(quotedStr)) throw new BadUsageException(string.Format("The field '{0}' has spaces before the QuotedChar at line {1}. Use the TrimAttribute to by pass this error. Field String: {2}", mFieldInfo.Name, line.mReader.LineNumber, line.CurrentString)); else throw new BadUsageException(string.Format("The field '{0}' not begin with the QuotedChar at line {1}. You can use FieldQuoted(QuoteMode.OptionalForRead) to allow optional quoted field.. Field String: {2}", mFieldInfo.Name, line.mReader.LineNumber, line.CurrentString)); } } }
/// <summary> /// Extract the field from the delimited file, removing separators and quotes /// and any duplicate quotes within the record /// </summary> /// <param name="line">line containing record input</param> /// <returns>Extract information</returns> internal override ExtractedInfo ExtractFieldString(LineInfo line) { if (IsOptional && line.IsEOL()) return ExtractedInfo.Empty; if (QuoteChar == '\0') return BasicExtractString(line); else { if (TrimMode == TrimMode.Both || TrimMode == TrimMode.Left) line.TrimStart(TrimChars); string quotedStr = QuoteChar.ToString(); if (line.StartsWith(quotedStr)) { var res = StringHelper.ExtractQuotedString(line, QuoteChar, QuoteMultiline == MultilineMode.AllowForBoth || QuoteMultiline == MultilineMode.AllowForRead); if (TrimMode == TrimMode.Both || TrimMode == TrimMode.Right) line.TrimStart(TrimChars); if (!IsLast && !line.StartsWith(Separator) && !line.IsEOL()) { throw new BadUsageException(line, "The field " + this.FieldInfo.Name + " is quoted but the quoted char: " + quotedStr + " not is just before the separator (You can use [FieldTrim] to avoid this error)"); } return res; } else { if (QuoteMode == QuoteMode.OptionalForBoth || QuoteMode == QuoteMode.OptionalForRead) return BasicExtractString(line); else if (line.StartsWithTrim(quotedStr)) { throw new BadUsageException( string.Format( "The field '{0}' has spaces before the QuotedChar at line {1}. Use the TrimAttribute to by pass this error. Field String: {2}", FieldInfo.Name, line.mReader.LineNumber, line.CurrentString)); } else { throw new BadUsageException( string.Format( "The field '{0}' does not begin with the QuotedChar at line {1}. You can use FieldQuoted(QuoteMode.OptionalForRead) to allow optional quoted field. Field String: {2}", FieldInfo.Name, line.mReader.LineNumber, line.CurrentString)); } } } }
internal override ExtractedInfo ExtractFieldString(LineInfo line) { if (mIsOptional && line.IsEOL()) { return(ExtractedInfo.Empty); } if (mQuoteChar == '\0') { return(BasicExtractString(line)); } else { if (mTrimMode == TrimMode.Both || mTrimMode == TrimMode.Left) { line.TrimStart(mTrimChars); } string quotedStr = mQuoteChar.ToString(); if (line.StartsWith(quotedStr)) { return(StringHelper.ExtractQuotedString(line, mQuoteChar, mQuoteMultiline == MultilineMode.AllowForBoth || mQuoteMultiline == MultilineMode.AllowForRead)); } else { if (mQuoteMode == QuoteMode.OptionalForBoth || mQuoteMode == QuoteMode.OptionalForRead) { return(BasicExtractString(line)); } else if (line.StartsWithTrim(quotedStr)) { throw new BadUsageException(string.Format("The field '{0}' has spaces before the QuotedChar at line {1}. Use the TrimAttribute to by pass this error. Field String: {2}", mFieldInfo.Name, line.mReader.LineNumber, line.CurrentString)); } else { throw new BadUsageException(string.Format("The field '{0}' not begin with the QuotedChar at line {1}. You can use FieldQuoted(QuoteMode.OptionalForRead) to allow optional quoted field.. Field String: {2}", mFieldInfo.Name, line.mReader.LineNumber, line.CurrentString)); } } } }
internal static ExtractedInfo ExtractQuotedString(LineInfo line, char quoteChar, bool allowMultiline) { // if (line.mReader == null) // throw new BadUsageException("The reader can´t be null"); if (line.IsEOL()) throw new BadUsageException("An empty String found and can be parsed like a QuotedString try to use SafeExtractQuotedString"); if (line.mLine[line.mCurrentPos] != quoteChar) throw new BadUsageException("The source string not begins with the quote char: " + quoteChar); StringBuilder res = new StringBuilder(32); //int lines = 0; bool firstFound = false; int i = line.mCurrentPos + 1; //bool mustContinue = true; while (line.mLineStr != null) { while (i < line.mLine.Length) { if (line.mLine[i] == quoteChar) { if (firstFound == true) { // Is an escaped quoted char res.Append(quoteChar); firstFound = false; } else { firstFound = true; } } else { if (firstFound) { // This was the end of the string line.mCurrentPos = i; return new ExtractedInfo(res.ToString()); // ExtractedInfo ei = ; // return ei; } else { res.Append(line.mLine[i]); } } i++; } if (firstFound) { line.mCurrentPos = i; return new ExtractedInfo(res.ToString()); } else { if (allowMultiline == false) throw new BadUsageException("The current field has an UnClosed quoted string. Complete line: " + res.ToString()); line.ReadNextLine(); res.Append(StringHelper.NewLine); //lines++; i = 0; } } throw new BadUsageException("The current field has an unclosed quoted string. Complete Filed String: " + res.ToString()); }
/// <summary> /// Get the data out of the records /// </summary> /// <param name="line">Line handler containing text</param> /// <returns></returns> internal object ExtractFieldValue(LineInfo line) { //-> extract only what I need if (InNewLine) { // Any trailing characters, terminate if (line.EmptyFromPos() == false) { throw new BadUsageException(line, "Text '" + line.CurrentString + "' found before the new line of the field: " + FieldInfo.Name + " (this is not allowed when you use [FieldInNewLine])"); } line.ReLoad(line.mReader.ReadNextLine()); if (line.mLineStr == null) { throw new BadUsageException(line, "End of stream found parsing the field " + FieldInfo.Name + ". Please check the class record."); } } if (IsArray == false) { ExtractedInfo info = ExtractFieldString(line); if (info.mCustomExtractedString == null) line.mCurrentPos = info.ExtractedTo + 1; line.mCurrentPos += CharsToDiscard; //total; if (Discarded) return GetDiscardedNullValue(); else return AssignFromString(info, line).Value; } else { if (ArrayMinLength <= 0) ArrayMinLength = 0; int i = 0; var res = new ArrayList(Math.Max(ArrayMinLength, 10)); while (line.mCurrentPos - CharsToDiscard < line.mLineStr.Length && i < ArrayMaxLength) { ExtractedInfo info = ExtractFieldString(line); if (info.mCustomExtractedString == null) line.mCurrentPos = info.ExtractedTo + 1; line.mCurrentPos += CharsToDiscard; try { var value = AssignFromString(info, line); if (value.NullValueUsed && i == 0 && line.IsEOL()) break; res.Add(value.Value); } catch (NullValueNotFoundException) { if (i == 0) break; else throw; } i++; } if (res.Count < ArrayMinLength) { throw new InvalidOperationException( string.Format( "Line: {0} Column: {1} Field: {2}. The array has only {3} values, less than the minimum length of {4}", line.mReader.LineNumber.ToString(), line.mCurrentPos.ToString(), FieldInfo.Name, res.Count, ArrayMinLength)); } else if (IsLast && line.IsEOL() == false) { throw new InvalidOperationException( string.Format( "Line: {0} Column: {1} Field: {2}. The array has more values than the maximum length of {3}", line.mReader.LineNumber, line.mCurrentPos, FieldInfo.Name, ArrayMaxLength)); } // TODO: is there a reason we go through all the array processing then discard it if (Discarded) return null; else return res.ToArray(ArrayType); } }
/// <summary> /// Get the data out of the records /// </summary> /// <param name="line">Line handler containing text</param> /// <returns></returns> internal object ExtractFieldValue(LineInfo line) { //-> extract only what I need if (InNewLine) { // Any trailing characters, terminate if (line.EmptyFromPos() == false) { throw new BadUsageException(line, "Text '" + line.CurrentString + "' found before the new line of the field: " + FieldInfo.Name + " (this is not allowed when you use [FieldInNewLine])"); } line.ReLoad(line.mReader.ReadNextLine()); if (line.mLineStr == null) { throw new BadUsageException(line, "End of stream found parsing the field " + FieldInfo.Name + ". Please check the class record."); } } if (IsArray == false) { ExtractedInfo info = ExtractFieldString(line); if (info.mCustomExtractedString == null) { line.mCurrentPos = info.ExtractedTo + 1; } line.mCurrentPos += CharsToDiscard; //total; if (Discarded) { return(GetDiscardedNullValue()); } else { return(AssignFromString(info, line).Value); } } else { if (ArrayMinLength <= 0) { ArrayMinLength = 0; } int i = 0; var res = new ArrayList(Math.Max(ArrayMinLength, 10)); while (line.mCurrentPos - CharsToDiscard < line.mLineStr.Length && i < ArrayMaxLength) { ExtractedInfo info = ExtractFieldString(line); if (info.mCustomExtractedString == null) { line.mCurrentPos = info.ExtractedTo + 1; } line.mCurrentPos += CharsToDiscard; try { var value = AssignFromString(info, line); if (value.NullValueUsed && i == 0 && line.IsEOL()) { break; } res.Add(value.Value); } catch (NullValueNotFoundException) { if (i == 0) { break; } else { throw; } } i++; } if (res.Count < ArrayMinLength) { throw new InvalidOperationException(string.Format("Line: {0} Column: {1} Field: {2}. The array has only {3} values, less than the minimum length of {4}", line.mReader.LineNumber.ToString(), line.mCurrentPos.ToString(), FieldInfo.Name, res.Count, ArrayMinLength)); } else if (IsLast && line.IsEOL() == false) { throw new InvalidOperationException(string.Format("Line: {0} Column: {1} Field: {2}. The array has more values than the maximum length of {3}", line.mReader.LineNumber, line.mCurrentPos, FieldInfo.Name, ArrayMaxLength)); } // TODO: is there a reason we go through all the array processing then discard it if (Discarded) { return(null); } else { return(res.ToArray(ArrayType)); } } }
/// <summary> /// Extract a string from a quoted string, allows for doubling the quotes /// for example 'o''clock' /// </summary> /// <param name="line">Line to extract from (with extra info)</param> /// <param name="quoteChar">Quote char to remove</param> /// <param name="allowMultiline">can we have a new line in middle of string</param> /// <returns>Extracted information</returns> internal static ExtractedInfo ExtractQuotedString(LineInfo line, char quoteChar, bool allowMultiline) { // if (line.mReader == null) // throw new BadUsageException("The reader can´t be null"); if (line.IsEOL()) { throw new BadUsageException( "An empty String found. This can not be parsed like a QuotedString try to use SafeExtractQuotedString"); } if (line.mLineStr[line.mCurrentPos] != quoteChar) { throw new BadUsageException("The source string does not begin with the quote char: " + quoteChar); } var res = new StringBuilder(32); bool firstFound = false; int i = line.mCurrentPos + 1; //bool mustContinue = true; while (line.mLineStr != null) { while (i < line.mLineStr.Length) { if (line.mLineStr[i] == quoteChar) { if (firstFound) { // Is an escaped quoted char res.Append(quoteChar); firstFound = false; } else { firstFound = true; } } else { if (firstFound) { // This was the end of the string line.mCurrentPos = i; return(new ExtractedInfo(res.ToString())); } else { res.Append(line.mLineStr[i]); } } i++; } if (firstFound) { line.mCurrentPos = i; return(new ExtractedInfo(res.ToString())); } else { if (allowMultiline == false) { throw new BadUsageException("The current field has an unclosed quoted string. Complete line: " + res.ToString()); } line.ReadNextLine(); res.Append(NewLine); //lines++; i = 0; } } throw new BadUsageException("The current field has an unclosed quoted string. Complete Filed String: " + res.ToString()); }
internal object ExtractFieldValue(LineInfo line) { //-> extract only what I need if (InNewLine) { if (line.EmptyFromPos() == false) throw new BadUsageException(line, "Text '" + line.CurrentString + "' found before the new line of the field: " + FieldInfo.Name + " (this is not allowed when you use [FieldInNewLine])"); line.ReLoad(line.mReader.ReadNextLine()); if (line.mLineStr == null) throw new BadUsageException(line, "End of stream found parsing the field " + FieldInfo.Name + ". Please check the class record."); } if (IsArray == false) { ExtractedInfo info = ExtractFieldString(line); if (info.mCustomExtractedString == null) line.mCurrentPos = info.ExtractedTo + 1; line.mCurrentPos += CharsToDiscard; //total; return AssignFromString(info, line); } else { if (ArrayMinLength <= 0) ArrayMinLength = 0; int i = 0; var res = new ArrayList(Math.Max(ArrayMinLength, 10)); while (line.mCurrentPos - CharsToDiscard < line.mLine.Length && i < ArrayMaxLength) { ExtractedInfo info = ExtractFieldString(line); if (info.mCustomExtractedString == null) line.mCurrentPos = info.ExtractedTo + 1; line.mCurrentPos += CharsToDiscard; res.Add(AssignFromString(info, line)); i++; } if (res.Count < ArrayMinLength) throw new InvalidOperationException(string.Format("Line: {0} Column: {1} Field: {2}. The array has only {3} values, less than the minimum length of {4}", line.mReader.LineNumber.ToString(), line.mCurrentPos.ToString(), FieldInfo.Name, res.Count, ArrayMinLength)); else if (IsLast && line.IsEOL() == false) throw new InvalidOperationException(string.Format("Line: {0} Column: {1} Field: {2}. The array has more values than the maximum length of {3}", line.mReader.LineNumber, line.mCurrentPos, FieldInfo.Name, ArrayMaxLength)); return res.ToArray(ArrayType); } }
internal object ExtractFieldValue(LineInfo line) { //-> extract only what I need if (mInNewLine) { if (line.EmptyFromPos() == false) { throw new BadUsageException(line, "Text '" + line.CurrentString + "' found before the new line of the field: " + mFieldInfo.Name + " (this is not allowed when you use [FieldInNewLine])"); } line.ReLoad(line.mReader.ReadNextLine()); if (line.mLineStr == null) { throw new BadUsageException(line, "End of stream found parsing the field " + mFieldInfo.Name + ". Please check the class record."); } } if (mIsArray == false) { ExtractedInfo info = ExtractFieldString(line); if (info.mCustomExtractedString == null) { line.mCurrentPos = info.ExtractedTo + 1; } line.mCurrentPos += mCharsToDiscard; //total; return(AssignFromString(info, line)); } else { if (mArrayMinLength <= 0) { mArrayMinLength = 0; } int i = 0; ArrayList res = new ArrayList(Math.Max(mArrayMinLength, 10)); while (line.mCurrentPos - mCharsToDiscard < line.mLine.Length && i < mArrayMaxLength) { ExtractedInfo info = ExtractFieldString(line); if (info.mCustomExtractedString == null) { line.mCurrentPos = info.ExtractedTo + 1; } line.mCurrentPos += mCharsToDiscard; res.Add(AssignFromString(info, line)); i++; } if (res.Count < mArrayMinLength) { throw new InvalidOperationException(string.Format("Line: {0} Column: {1} Field: {2}. The array has only {3} values, less than the minimum length of {4}", line.mReader.LineNumber.ToString(), line.mCurrentPos.ToString(), mFieldInfo.Name, res.Count, mArrayMinLength)); } else if (mIsLast && line.IsEOL() == false) { throw new InvalidOperationException(string.Format("Line: {0} Column: {1} Field: {2}. The array has more values than the maximum length of {3}", line.mReader.LineNumber, line.mCurrentPos, mFieldInfo.Name, mArrayMaxLength)); } return(res.ToArray(mArrayType)); } }
protected override ExtractedInfo ExtractFieldString(LineInfo line) { if (mIsOptional && line.IsEOL()) { return(ExtractedInfo.Empty); } if (mQuoteChar == '\0') { return(BasicExtractString(line)); } else { //TODO: UnComment and Fix if (mTrimMode == TrimMode.Both || mTrimMode == TrimMode.Left) { //int pos = line.mCurrentPos; line.TrimStart(mTrimChars); // from2 = from.TrimStart(mTrimChars); //res.CharsRemoved = line.mCurrentPos - pos; } string quotedStr = mQuoteChar.ToString(); if (line.StartsWith(quotedStr)) { // ExtractedInfo res = null; // res = new ExtractedInfo(line, line.mCurrentPos); return(StringHelper.ExtractQuotedString(line, mQuoteChar, mQuoteMultiline == MultilineMode.AllowForBoth || mQuoteMultiline == MultilineMode.AllowForRead)); // if (mQuoteMultiline == MultilineMode.AllowForBoth || mQuoteMultiline == MultilineMode.AllowForRead) // { // // //res.ExtractedString = ei.ExtractedString; // //res.CharsRemoved += ei.CharsRemoved; // //res.ExtraLines = ei.ExtraLines; // //res.NewRestOfLine = ei.NewRestOfLine; // } // else // { // return StringHelper.ExtractQuotedString(from2, mQuoteChar, out index); // //res.CharsRemoved += index; // } // return res; } else { if (mQuoteMode == QuoteMode.OptionalForBoth || mQuoteMode == QuoteMode.OptionalForRead) { return(BasicExtractString(line)); } else if (line.StartsWithTrim(quotedStr)) { throw new BadUsageException("The field '" + this.mFieldInfo.Name + "' has spaces before the QuotedChar at line " + line.mReader.LineNumber.ToString() + ". Use the TrimAttribute to by pass this error. Field String: " + line.CurrentString); } else { throw new BadUsageException("The field '" + this.mFieldInfo.Name + "' not begin with the QuotedChar at line " + line.mReader.LineNumber.ToString() + ". You can use FieldQuoted(QuoteMode.OptionalForRead) to allow optional quoted field.. Field String: " + line.CurrentString); } } } }