michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsLinebreakConverter_h_ michael@0: #define nsLinebreakConverter_h_ michael@0: michael@0: #include "nscore.h" michael@0: #include "nsString.h" michael@0: michael@0: // utility class for converting between different line breaks. michael@0: michael@0: class nsLinebreakConverter michael@0: { michael@0: public: michael@0: michael@0: // Note: enum must match char* array in GetLinebreakString michael@0: typedef enum { michael@0: eLinebreakAny, // any kind of linebreak (i.e. "don't care" source) michael@0: michael@0: eLinebreakPlatform, // platform linebreak michael@0: eLinebreakContent, // Content model linebreak (LF) michael@0: eLinebreakNet, // Form submission linebreak (CRLF) michael@0: michael@0: eLinebreakMac, // CR michael@0: eLinebreakUnix, // LF michael@0: eLinebreakWindows, // CRLF michael@0: michael@0: eLinebreakSpace // space characters. Only valid as destination type michael@0: michael@0: } ELinebreakType; michael@0: michael@0: enum { michael@0: kIgnoreLen = -1 michael@0: }; michael@0: michael@0: /* ConvertLineBreaks michael@0: * Convert line breaks in the supplied string, allocating and returning michael@0: * a new buffer. Returns nullptr on failure. michael@0: * @param aSrc: the source string. if aSrcLen == kIgnoreLen this string is assumed michael@0: * to be null terminated, otherwise it must be at least aSrcLen long. michael@0: * @param aSrcBreaks: the line breaks in the source. If unknown, pass eLinebreakAny. michael@0: * If known, pass the known value, as this may be more efficient. michael@0: * @param aDestBreaks: the line breaks you want in the output. michael@0: * @param aSrcLen: length of the source. If -1, the source is assumed to be a null- michael@0: * terminated string. michael@0: * @param aOutLen: used to return character length of returned buffer, if not null. michael@0: */ michael@0: static char* ConvertLineBreaks(const char* aSrc, michael@0: ELinebreakType aSrcBreaks, ELinebreakType aDestBreaks, michael@0: int32_t aSrcLen = kIgnoreLen, int32_t* aOutLen = nullptr); michael@0: michael@0: michael@0: /* ConvertUnicharLineBreaks michael@0: * Convert line breaks in the supplied string, allocating and returning michael@0: * a new buffer. Returns nullptr on failure. michael@0: * @param aSrc: the source string. if aSrcLen == kIgnoreLen this string is assumed michael@0: * to be null terminated, otherwise it must be at least aSrcLen long. michael@0: * @param aSrcBreaks: the line breaks in the source. If unknown, pass eLinebreakAny. michael@0: * If known, pass the known value, as this may be more efficient. michael@0: * @param aDestBreaks: the line breaks you want in the output. michael@0: * @param aSrcLen: length of the source, in characters. If -1, the source is assumed to be a null- michael@0: * terminated string. michael@0: * @param aOutLen: used to return character length of returned buffer, if not null. michael@0: */ michael@0: static char16_t* ConvertUnicharLineBreaks(const char16_t* aSrc, michael@0: ELinebreakType aSrcBreaks, ELinebreakType aDestBreaks, michael@0: int32_t aSrcLen = kIgnoreLen, int32_t* aOutLen = nullptr); michael@0: michael@0: michael@0: /* ConvertStringLineBreaks michael@0: * Convert line breaks in the supplied string, changing the string buffer (i.e. in-place conversion) michael@0: * @param ioString: the string to be converted. michael@0: * @param aSrcBreaks: the line breaks in the source. If unknown, pass eLinebreakAny. michael@0: * If known, pass the known value, as this may be more efficient. michael@0: * @param aDestBreaks: the line breaks you want in the output. michael@0: * @param aSrcLen: length of the source, in characters. If -1, the source is assumed to be a null- michael@0: * terminated string. michael@0: */ michael@0: static nsresult ConvertStringLineBreaks(nsString& ioString, ELinebreakType aSrcBreaks, ELinebreakType aDestBreaks); michael@0: michael@0: michael@0: /* ConvertLineBreaksInSitu michael@0: * Convert line breaks in place if possible. NOTE: THIS MAY REALLOCATE THE BUFFER, michael@0: * BUT IT WON'T FREE THE OLD BUFFER (because it doesn't know how). So be prepared michael@0: * to keep a copy of the old pointer, and free it if this passes back a new pointer. michael@0: * ALSO NOTE: DON'T PASS A STATIC STRING POINTER TO THIS FUNCTION. michael@0: * michael@0: * @param ioBuffer: the source buffer. if aSrcLen == kIgnoreLen this string is assumed michael@0: * to be null terminated, otherwise it must be at least aSrcLen long. michael@0: * @param aSrcBreaks: the line breaks in the source. If unknown, pass eLinebreakAny. michael@0: * If known, pass the known value, as this may be more efficient. michael@0: * @param aDestBreaks: the line breaks you want in the output. michael@0: * @param aSrcLen: length of the source. If -1, the source is assumed to be a null- michael@0: * terminated string. michael@0: * @param aOutLen: used to return character length of returned buffer, if not null. michael@0: */ michael@0: static nsresult ConvertLineBreaksInSitu(char **ioBuffer, ELinebreakType aSrcBreaks, ELinebreakType aDestBreaks, michael@0: int32_t aSrcLen = kIgnoreLen, int32_t* aOutLen = nullptr); michael@0: michael@0: michael@0: /* ConvertUnicharLineBreaksInSitu michael@0: * Convert line breaks in place if possible. NOTE: THIS MAY REALLOCATE THE BUFFER, michael@0: * BUT IT WON'T FREE THE OLD BUFFER (because it doesn't know how). So be prepared michael@0: * to keep a copy of the old pointer, and free it if this passes back a new pointer. michael@0: * michael@0: * @param ioBuffer: the source buffer. if aSrcLen == kIgnoreLen this string is assumed michael@0: * to be null terminated, otherwise it must be at least aSrcLen long. michael@0: * @param aSrcBreaks: the line breaks in the source. If unknown, pass eLinebreakAny. michael@0: * If known, pass the known value, as this may be more efficient. michael@0: * @param aDestBreaks: the line breaks you want in the output. michael@0: * @param aSrcLen: length of the source in characters. If -1, the source is assumed to be a null- michael@0: * terminated string. michael@0: * @param aOutLen: used to return character length of returned buffer, if not null. michael@0: */ michael@0: static nsresult ConvertUnicharLineBreaksInSitu(char16_t **ioBuffer, ELinebreakType aSrcBreaks, ELinebreakType aDestBreaks, michael@0: int32_t aSrcLen = kIgnoreLen, int32_t* aOutLen = nullptr); michael@0: michael@0: }; michael@0: michael@0: #endif // nsLinebreakConverter_h_