[Csmail-patches] CVS: csmail/src/CSMail.Utils ChangeLog,1.15,1.16 HeaderTokenType.cs,1.1,1.2 HeaderT
Status: Pre-Alpha
Brought to you by:
mastergaurav
From: Gaurav V. <mas...@us...> - 2002-10-07 12:56:04
|
Update of /cvsroot/csmail/csmail/src/CSMail.Utils In directory usw-pr-cvs1:/tmp/cvs-serv8242 Modified Files: ChangeLog HeaderTokenType.cs HeaderTokenizer.cs Log Message: 2002-10-07 * HeaderTokenType.cs : Added member "Undefined". * HeaderTokenizer.cs : SkipWhiteSpace(), : ParseToken(string, int, int) - Implemented. : GetNextToken() - Partial implementation. Index: ChangeLog =================================================================== RCS file: /cvsroot/csmail/csmail/src/CSMail.Utils/ChangeLog,v retrieving revision 1.15 retrieving revision 1.16 diff -u -r1.15 -r1.16 --- ChangeLog 4 Oct 2002 11:13:20 -0000 1.15 +++ ChangeLog 7 Oct 2002 12:56:01 -0000 1.16 @@ -1,4 +1,12 @@ +2002-10-07 Gaurav Vaish <mastergaurav AT users DOT sf DOT net> + + * HeaderTokenType.cs : Added member "Undefined". + * HeaderTokenizer.cs : SkipWhiteSpace(), + : ParseToken(string, int, int) + - Implemented. + : GetNextToken() - Partial implementation. + 2002-10-04 Gaurav Vaish <mastergaurav AT users DOT sf DOT net> * HeaderTokenizer.cs : Finally, I had to create this class. Index: HeaderTokenType.cs =================================================================== RCS file: /cvsroot/csmail/csmail/src/CSMail.Utils/HeaderTokenType.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- HeaderTokenType.cs 4 Oct 2002 11:13:20 -0000 1.1 +++ HeaderTokenType.cs 7 Oct 2002 12:56:01 -0000 1.2 @@ -16,6 +16,7 @@ Atom, Comment, EOF, - QuotedString + QuotedString, + Undefined } } Index: HeaderTokenizer.cs =================================================================== RCS file: /cvsroot/csmail/csmail/src/CSMail.Utils/HeaderTokenizer.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- HeaderTokenizer.cs 4 Oct 2002 11:13:20 -0000 1.1 +++ HeaderTokenizer.cs 7 Oct 2002 12:56:01 -0000 1.2 @@ -9,6 +9,7 @@ */ using System; +using System.Text; using CSMail; namespace CSMail.Utils @@ -22,6 +23,11 @@ private int currentPosition = 0; private int nextPosition = 0; private int peekPosition = 0; + private int maximumPosition = 0; + + private static readonly HeaderToken EOFToken = new HeaderToken( + HeaderTokenType.EOF, + null); public HeaderTokenizer(string content, string delimiters, bool skipComments) @@ -30,10 +36,11 @@ { throw new ArgumentNullException("[HeaderTokenizer] Null value to header"); } - this.content = content; + this.content = (content == null ? String.Empty : content); this.skipComments = skipComments; this.delimiters = (delimiters == null ? Constants.RFC822Delimiters : delimiters); + this.maximumPosition = content.Length; } public HeaderTokenizer(string content, string delimiters) @@ -62,13 +69,165 @@ { get { - return GetNextToken(); + currentPosition = nextPosition; + HeaderToken retVal = GetNextToken(); + nextPosition = currentPosition; + peekPosition = currentPosition; + return retVal; } } private HeaderToken GetNextToken() { + if(currentPosition >= maximumPosition) + { + return EOFToken; + } + if(SkipWhiteSpace() < 0) + { + return EOFToken; + } + + bool waitNext = false; + char c; + for( c = content[currentPosition]; c == '('; c = content[currentPosition]) + { + currentPosition++; + int start = currentPosition; + int braceBalance; + for(braceBalance = 1; + braceBalance > 0 && currentPosition < maximumPosition; + currentPosition++) + { + c = content[currentPosition]; + if(c == '\\') + { + currentPosition++; + waitNext = true; + } else if(c == '\r') + { + waitNext = true; + } else if(c == '(') + { + braceBalance++; + } else if(c == ')') + { + braceBalance--; + } + } + + if(braceBalance != 0) + { + throw new ParseException("[HeaderTokenizer] Unbalanced comments"); + } + if(!skipComments) + { + string data = null; + if(waitNext) + { + data = ParseToken(content, start, + currentPosition - 1); + } else + { + data = content.Substring(start, + currentPosition - start); + } + return new HeaderToken(HeaderTokenType.Comment, data); + } + } + + if(c == '"') + { + int start = ++currentPosition; + while(currentPosition < maximumPosition) + { + c = content[currentPosition]; + if(c == '\\') + { + currentPosition++; + waitNext = true; + } else if(c == '\r') + { + currentPosition++; + waitNext = true; + } else if(c == '"') + { + currentPosition++; + string data = null; + if(waitNext) + { + data = ParseToken(content, start, + currentPosition - 1); + } else + { + data = content.Substring(start, + currentPosition - start); + } + return new HeaderToken(HeaderTokenType.QuotedString, + data); + } + currentPosition++; + } + throw new ParseException("[HeaderTokenizer] Unbalanced quoted string"); + } + /* + if(c < ' ' || c >= '\177' || delimiters.IndexOf(c) >= 0) + { + currentPosition ++; + char[] ac = + } + */ throw new NotImplementedException(); + } + + private int SkipWhiteSpace() + { + char c; + while(currentPosition < maximumPosition) + { + c = content[currentPosition]; + if(c != ' ' && c != '\t' && c != '\r' && c != '\n') + return currentPosition; + currentPosition++; + } + return -1; + } + + private string ParseToken(string data, int start, int stop) + { + StringBuilder sb = new StringBuilder(); + bool escape = false; + bool newline = false; + char c; + for(int i = start; i < stop; i++) + { + c = data[i]; + if(c == '\n' && newline) + { + newline = false; + } else + { + newline = false; + if(!escape) + { + if(c == '\\') + { + escape = true; + } else if(c == '\r') + { + newline = true; + } else + { + sb.Append(c); + } + } else + { + sb.Append(c); + escape = false; + } + } + } + return sb.ToString(); } } } |