2005-05-14 Gabor Kelemen <kelemeng@gnome.hu>
[beagle.git] / beagled / CalendarQueryable / ICalParser / ICalUtil.cs
blob36c433759d6cf6566c6b7330269421883051387d
1 using System;
2 using System.IO;
3 using System.Text;
5 /***
6 * <copyright>
7 * ICalParser is a general purpose .Net parser for iCalendar format files (RFC 2445)
8 *
9 * Copyright (C) 2004 J. Tim Spurway
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * </copyright>
27 namespace Semaview.Shared.ICalParser
29 /// <summary>
30 /// Summary description for ICalUtil.
31 /// </summary>
32 public class ICalUtil
34 private ICalUtil()
38 /**
39 * From RFC2445
41 * Lines of text SHOULD NOT be longer than 75 octets, excluding the line
42 * break. Long content lines SHOULD be split into a multiple line
43 * representations using a line "folding" technique. That is, a long
44 * line can be split between any two characters by inserting a CRLF
45 * immediately followed by a single linear white space character (i.e.,
46 * SPACE, US-ASCII decimal 32 or HTAB, US-ASCII decimal 9). Any sequence
47 * of CRLF followed immediately by a single linear white space character
48 * is ignored (i.e., removed) when processing the content type.
50 * For example the line:
52 * DESCRIPTION:This is a long description that exists on a long line.
54 * Can be represented as:
56 * DESCRIPTION:This is a lo
57 * ng description
58 * that exists on a long line.
60 **/
61 public static string FoldLines( StringBuilder iCal )
63 StringReader reader = new StringReader( iCal.ToString() );
64 StringBuilder folded = new StringBuilder();
65 string line = reader.ReadLine();
66 while( line != null )
68 int start = 0;
69 int len = line.Length;
71 // I chose 72 instead of the max 75 cause I wanted to, so :P
72 while( len > 72 )
74 // Indent if not first line
75 if( start != 0 )
76 folded.Append( " " );
77 folded.AppendFormat( "{0}\r\n", line.Substring( start, 72 ) );
78 start += 72;
79 len -= 72;
81 // Indent if not first line
82 if( start != 0 )
83 folded.Append( " " );
84 folded.AppendFormat( "{0}\r\n", line.Substring( start, len ) );
85 line = reader.ReadLine();
87 return folded.ToString();
90 /// <summary>
91 /// Quotes newlines, tabs, commas, colons, semi-colons, double quotes and back slashes
92 /// </summary>
93 /// <param name="str"></param>
94 /// <returns></returns>
95 public static string Quote( string str )
97 string quoted = str.Replace( "\\", "\\\\" );
98 quoted = quoted.Replace( "\r\n", "\\n" );
99 quoted = quoted.Replace( "\n", "\\n" );
100 quoted = quoted.Replace( "\t", "\\t" );
101 quoted = quoted.Replace( ",", "\\," );
102 quoted = quoted.Replace( ":", "\\:" );
103 quoted = quoted.Replace( ";", "\\;" );
104 quoted = quoted.Replace( "\"", "\\\"" );
105 return quoted;
108 public static string Unquote( string str )
110 string unquoted = str.Replace( "\\r\\n", "\r\n" );
111 unquoted = unquoted.Replace( "\\n", "\r\n" );
112 unquoted = unquoted.Replace( "\\t", "\t" );
113 unquoted = unquoted.Replace( "\\,", "," );
114 unquoted = unquoted.Replace( "\\:", ":" );
115 unquoted = unquoted.Replace( "\\;", ";" );
116 unquoted = unquoted.Replace( "\\\"", "\"" );
117 unquoted = unquoted.Replace( "\\\\", "\\" );
118 return unquoted.ToString();