utilities.c

Go to the documentation of this file.
00001 // An implementation of some of the utilities. See also
00002 // utilities_cxx.cpp.
00003 
00004 #include "common/utilities.h"
00005 
00006 #include "common/gxlowmem.h"
00007 
00008 #include <string.h>
00009 
00010 #include <glib.h>
00011 
00012 // --------------------------------------------------
00013 // sh_utils.h implementation
00014 // --------------------------------------------------
00015 
00016 // This fixes a bug in Open C MR.
00017 //
00018 // xxx This bug might no longer apply for newer versions of Open C.
00019 #ifdef __SYMBIAN32__
00020 #undef g_utf8_next_char
00021 #define g_utf8_next_char(p) (char *)((p) + *(*_g_utf8_skip() + *(guchar *)(p)))
00022 #endif
00023 
00024 // Forms a JSON String text out of the given UTF-8 text. The string
00025 // will be quoted, and still UTF-8.
00026 // 
00027 // "JSON text SHALL be encoded in Unicode. The default encoding is
00028 // UTF-8. All Unicode characters may be placed within the quotation
00029 // marks except for the characters that must be escaped: quotation
00030 // mark, reverse solidus, and the control characters (U+0000 through
00031 // U+001F)."
00032 //
00033 // The caller is responsible for freeing the result buffer.
00034 char* utf8ToJsonString(const char* text)
00035 {
00036     GString* gs = g_string_sized_new(strlen(text) + 8);
00037 
00038     gunichar ch;
00039     g_string_append_c(gs, '\"');
00040     while (*text) {
00041   ch = g_utf8_get_char(text);
00042 
00043   if (ch == '"') {
00044       g_string_append(gs, "\\\"");
00045   } else if (ch == '\\') {
00046       g_string_append(gs, "\\\\");
00047   } else if (ch > 0 && ch < 0x20) {
00048       g_string_append_printf(gs, "\\u%04x", ch);
00049   } else {
00050       g_string_append_unichar(gs, ch);
00051   }
00052 
00053   text = g_utf8_next_char(text);
00054     }
00055     g_string_append_c(gs, '\"');
00056 
00057     char* res = gs->str;
00058     g_string_free(gs, FALSE);
00059     return res;
00060 }
00061 
00062 // --------------------------------------------------
00063 // gxutils.h implementation
00064 // --------------------------------------------------
00065 
00066 gchar* gx_strdup (const gchar *str)
00067 {
00068   gchar* s;
00069   TRAP_OOM_VALUE(NULL, s = g_strdup(str));
00070   return s;
00071 }
00072 
00073 /* // Not such an attractive approach. Perhaps better to preallocate all quarks needed in an application, trapping any error at that point.
00074 GQuark gx_quark_from_static_string (const gchar *string)
00075 {
00076   GQuark q;
00077   // 0 might be a valid quark value, meaning that the caller cannot
00078   // know there was an error, but we do not really want to crash
00079   // either.
00080   TRAP_OOM_VALUE(0, q = g_quark_from_static_string(string));
00081   return q;
00082 }
00083 */
00084 
00085 /**
00086 
00087 utilities.c
00088 
00089 Copyright 2009 Helsinki Institute for Information Technology (HIIT)
00090 and the authors. All rights reserved.
00091 
00092 Authors: Tero Hasu <tero.hasu@hut.fi>
00093 
00094 Permission is hereby granted, free of charge, to any person
00095 obtaining a copy of this software and associated documentation files
00096 (the "Software"), to deal in the Software without restriction,
00097 including without limitation the rights to use, copy, modify, merge,
00098 publish, distribute, sublicense, and/or sell copies of the Software,
00099 and to permit persons to whom the Software is furnished to do so,
00100 subject to the following conditions:
00101 
00102 The above copyright notice and this permission notice shall be
00103 included in all copies or substantial portions of the Software.
00104 
00105 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00106 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00107 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00108 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
00109 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
00110 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00111 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00112 SOFTWARE.
00113 
00114  **/

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:57 2011 by Doxygen 1.6.1