gx_maybe_string.c

Go to the documentation of this file.
00001 #include "gx_maybe_string.h"
00002 
00003 #include "common/gxerror.h"
00004 #include "common/gxlowmem.h"
00005 
00006 #include <string.h> // strcmp
00007 
00008 void GMaybeString_init(GMaybeString* m)
00009 {
00010   m->just = FALSE;
00011   m->value = NULL;
00012 }
00013 
00014 const gchar* GMaybeString_get(GMaybeString* m)
00015 {
00016   if (!m->just)
00017     return NULL;
00018   return m->value->str;
00019 }
00020 
00021 gboolean GMaybeString_is_nothing(GMaybeString* m)
00022 {
00023   return GMaybeString_get(m) == NULL;
00024 }
00025 
00026 gboolean GMaybeString_is(GMaybeString* m, const gchar* other)
00027 {
00028   const gchar* mine = GMaybeString_get(m);
00029   if (mine && other) {
00030     return strcmp(mine, other) == 0;
00031   } else if (!mine && !other) {
00032     return TRUE;
00033   } else {
00034     return FALSE;
00035   }
00036 }
00037 
00038 gboolean GMaybeString_assign(GMaybeString* m, const gchar* rval,
00039            GError** error)
00040 {
00041   SET_TRAP_OOM(goto fail);
00042   if (m->value) {
00043     m->value = g_string_assign(m->value, rval);
00044   } else {
00045     m->value = g_string_new(rval);
00046   }
00047   m->just = TRUE;
00048   UNSET_TRAP_OOM();
00049   return TRUE;
00050 
00051 #if HAVE_TRAP_OOM
00052  fail:
00053   if (error) *error = gx_error_no_memory;
00054   return FALSE;
00055 #else
00056   (void)error;
00057 #endif
00058 }
00059 
00060 void GMaybeString_free(GMaybeString* m)
00061 {
00062   if (m->value) {
00063     g_string_free(m->value, TRUE);
00064     m->value = NULL;
00065   }
00066   m->just = FALSE;
00067 }
00068 
00069 /**
00070 
00071 Copyright 2010 Helsinki Institute for Information Technology (HIIT)
00072 and the authors. All rights reserved.
00073 
00074 Authors: Tero Hasu <tero.hasu@hut.fi>
00075 
00076 Permission is hereby granted, free of charge, to any person
00077 obtaining a copy of this software and associated documentation files
00078 (the "Software"), to deal in the Software without restriction,
00079 including without limitation the rights to use, copy, modify, merge,
00080 publish, distribute, sublicense, and/or sell copies of the Software,
00081 and to permit persons to whom the Software is furnished to do so,
00082 subject to the following conditions:
00083 
00084 The above copyright notice and this permission notice shall be
00085 included in all copies or substantial portions of the Software.
00086 
00087 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00088 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00089 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00090 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
00091 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
00092 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00093 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00094 SOFTWARE.
00095 
00096  **/

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