Back to index

lightning-sunbird  0.9+nobinonly
nsLinebreakConverter.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef nsLinebreakConverter_h_
00039 #define nsLinebreakConverter_h_
00040 
00041 
00042 #include "nscore.h"
00043 #include "nsString.h"
00044 
00045 // utility class for converting between different line breaks.
00046 
00047 class NS_COM nsLinebreakConverter
00048 {
00049 public:
00050 
00051   // Note: enum must match char* array in GetLinebreakString
00052   typedef enum {
00053     eLinebreakAny,          // any kind of linebreak (i.e. "don't care" source)
00054     
00055     eLinebreakPlatform,     // platform linebreak
00056     eLinebreakContent,      // Content model linebreak (LF)
00057     eLinebreakNet,          // Form submission linebreak (CRLF)
00058   
00059     eLinebreakMac,          // CR
00060     eLinebreakUnix,         // LF
00061     eLinebreakWindows       // CRLF
00062   
00063   } ELinebreakType;
00064 
00065   enum {
00066     kIgnoreLen = -1
00067   };
00068   
00069   /* ConvertLineBreaks
00070    * Convert line breaks in the supplied string, allocating and returning
00071    * a new buffer. Returns nsnull on failure.
00072    * @param aSrc: the source string. if aSrcLen == kIgnoreLen this string is assumed
00073    *              to be null terminated, otherwise it must be at least aSrcLen long.
00074    * @param aSrcBreaks: the line breaks in the source. If unknown, pass eLinebreakAny.
00075    *              If known, pass the known value, as this may be more efficient.
00076    * @param aDestBreaks: the line breaks you want in the output.
00077    * @param aSrcLen: length of the source. If -1, the source is assumed to be a null-
00078    *              terminated string.
00079    * @param aOutLen: used to return character length of returned buffer, if not null.
00080    */
00081   static char* ConvertLineBreaks(const char* aSrc,
00082                 ELinebreakType aSrcBreaks, ELinebreakType aDestBreaks,
00083                 PRInt32 aSrcLen = kIgnoreLen, PRInt32* aOutLen = nsnull);
00084   
00085 
00086   /* ConvertUnicharLineBreaks
00087    * Convert line breaks in the supplied string, allocating and returning
00088    * a new buffer. Returns nsnull on failure.
00089    * @param aSrc: the source string. if aSrcLen == kIgnoreLen this string is assumed
00090    *              to be null terminated, otherwise it must be at least aSrcLen long.
00091    * @param aSrcBreaks: the line breaks in the source. If unknown, pass eLinebreakAny.
00092    *              If known, pass the known value, as this may be more efficient.
00093    * @param aDestBreaks: the line breaks you want in the output.
00094    * @param aSrcLen: length of the source, in characters. If -1, the source is assumed to be a null-
00095    *              terminated string.
00096    * @param aOutLen: used to return character length of returned buffer, if not null.
00097    */
00098   static PRUnichar* ConvertUnicharLineBreaks(const PRUnichar* aSrc,
00099                 ELinebreakType aSrcBreaks, ELinebreakType aDestBreaks,
00100                 PRInt32 aSrcLen = kIgnoreLen, PRInt32* aOutLen = nsnull);
00101   
00102 
00103   /* ConvertStringLineBreaks
00104    * Convert line breaks in the supplied string, changing the string buffer (i.e. in-place conversion)
00105    * @param ioString: the string to be converted.
00106    * @param aSrcBreaks: the line breaks in the source. If unknown, pass eLinebreakAny.
00107    *              If known, pass the known value, as this may be more efficient.
00108    * @param aDestBreaks: the line breaks you want in the output.
00109    * @param aSrcLen: length of the source, in characters. If -1, the source is assumed to be a null-
00110    *              terminated string.
00111    */
00112   static nsresult ConvertStringLineBreaks(nsString& ioString, ELinebreakType aSrcBreaks, ELinebreakType aDestBreaks);
00113 
00114 
00115   /* ConvertLineBreaksInSitu
00116    * Convert line breaks in place if possible. NOTE: THIS MAY REALLOCATE THE BUFFER,
00117    * BUT IT WON'T FREE THE OLD BUFFER (because it doesn't know how). So be prepared
00118    * to keep a copy of the old pointer, and free it if this passes back a new pointer.
00119    * ALSO NOTE: DON'T PASS A STATIC STRING POINTER TO THIS FUNCTION.
00120    * 
00121    * @param ioBuffer: the source buffer. if aSrcLen == kIgnoreLen this string is assumed
00122    *              to be null terminated, otherwise it must be at least aSrcLen long.
00123    * @param aSrcBreaks: the line breaks in the source. If unknown, pass eLinebreakAny.
00124    *              If known, pass the known value, as this may be more efficient.
00125    * @param aDestBreaks: the line breaks you want in the output.
00126    * @param aSrcLen: length of the source. If -1, the source is assumed to be a null-
00127    *              terminated string.
00128    * @param aOutLen: used to return character length of returned buffer, if not null.
00129    */
00130   static nsresult ConvertLineBreaksInSitu(char **ioBuffer, ELinebreakType aSrcBreaks, ELinebreakType aDestBreaks,
00131                         PRInt32 aSrcLen = kIgnoreLen, PRInt32* aOutLen = nsnull);
00132 
00133 
00134   /* ConvertUnicharLineBreaksInSitu
00135    * Convert line breaks in place if possible. NOTE: THIS MAY REALLOCATE THE BUFFER,
00136    * BUT IT WON'T FREE THE OLD BUFFER (because it doesn't know how). So be prepared
00137    * to keep a copy of the old pointer, and free it if this passes back a new pointer.
00138    * 
00139    * @param ioBuffer: the source buffer. if aSrcLen == kIgnoreLen this string is assumed
00140    *              to be null terminated, otherwise it must be at least aSrcLen long.
00141    * @param aSrcBreaks: the line breaks in the source. If unknown, pass eLinebreakAny.
00142    *              If known, pass the known value, as this may be more efficient.
00143    * @param aDestBreaks: the line breaks you want in the output.
00144    * @param aSrcLen: length of the source in characters. If -1, the source is assumed to be a null-
00145    *              terminated string.
00146    * @param aOutLen: used to return character length of returned buffer, if not null.
00147    */
00148   static nsresult ConvertUnicharLineBreaksInSitu(PRUnichar **ioBuffer, ELinebreakType aSrcBreaks, ELinebreakType aDestBreaks,
00149                         PRInt32 aSrcLen = kIgnoreLen, PRInt32* aOutLen = nsnull);
00150     
00151 };
00152 
00153 
00154 
00155 
00156 #endif // nsLinebreakConverter_h_