Back to index

lightning-sunbird  0.9+nobinonly
nsTextFormatter.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; 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 nsTextFormatter_h___
00039 #define nsTextFormatter_h___
00040 
00041 /*
00042 ** API for PR printf like routines. Supports the following formats
00043 **     %d - decimal
00044 **     %u - unsigned decimal
00045 **     %x - unsigned hex
00046 **     %X - unsigned uppercase hex
00047 **     %o - unsigned octal
00048 **     %hd, %hu, %hx, %hX, %ho - 16-bit versions of above
00049 **     %ld, %lu, %lx, %lX, %lo - 32-bit versions of above
00050 **     %lld, %llu, %llx, %llX, %llo - 64 bit versions of above
00051 **     %s - utf8 string
00052 **     %S - PRUnichar string
00053 **     %c - character
00054 **     %p - pointer (deals with machine dependent pointer size)
00055 **     %f - float
00056 **     %g - float
00057 */
00058 #include "prtypes.h"
00059 #include "prio.h"
00060 #include <stdio.h>
00061 #include <stdarg.h>
00062 #include "nscore.h"
00063 #include "nsAString.h"
00064 
00065 
00066 class NS_COM nsTextFormatter {
00067 
00068 public:
00069 
00070 /*
00071 ** sprintf into a fixed size buffer. Guarantees that a NUL is at the end
00072 ** of the buffer. Returns the length of the written output, NOT including
00073 ** the NUL, or (PRUint32)-1 if an error occurs.
00074 */
00075 static PRUint32  snprintf(PRUnichar *out, PRUint32 outlen, const PRUnichar *fmt, ...);
00076 
00077 /*
00078 ** sprintf into a PR_MALLOC'd buffer. Return a pointer to the malloc'd
00079 ** buffer on success, NULL on failure. Call "smprintf_free" to release
00080 ** the memory returned.
00081 */
00082 static PRUnichar*  smprintf(const PRUnichar *fmt, ...);
00083 
00084 
00085 static PRUint32 ssprintf(nsAString& out, const PRUnichar* fmt, ...);
00086 /*
00087 ** Free the memory allocated, for the caller, by smprintf
00088 */
00089 static void smprintf_free(PRUnichar *mem);
00090 
00091 /*
00092 ** "append" sprintf into a PR_MALLOC'd buffer. "last" is the last value of
00093 ** the PR_MALLOC'd buffer. sprintf will append data to the end of last,
00094 ** growing it as necessary using realloc. If last is NULL, PR_sprintf_append
00095 ** will allocate the initial string. The return value is the new value of
00096 ** last for subsequent calls, or NULL if there is a malloc failure.
00097 */
00098 static PRUnichar*  sprintf_append(PRUnichar *last, const PRUnichar *fmt, ...);
00099 
00100 /*
00101 ** va_list forms of the above.
00102 */
00103 static PRUint32  vsnprintf(PRUnichar *out, PRUint32 outlen, const PRUnichar *fmt, va_list ap);
00104 static PRUnichar*  vsmprintf(const PRUnichar *fmt, va_list ap);
00105 static PRUint32    vssprintf(nsAString& out, const PRUnichar *fmt, va_list ap);
00106 static PRUnichar*  vsprintf_append(PRUnichar *last, const PRUnichar *fmt, va_list ap);
00107 
00108 #ifdef DEBUG
00109 static PRBool SelfTest();
00110 #endif
00111 
00112 
00113 };
00114 
00115 #endif /* nsTextFormatter_h___ */