pelib  2.0.0
src/string.c
Go to the documentation of this file.
00001 /*
00002  * string.c
00003  *
00004  *      Created on: 20 Feb 2012
00005  *      Copyright 2012 Nicolas Melot
00006  *
00007  * This file is part of pelib.
00008  * 
00009  *               pelib is free software: you can redistribute it and/or modify
00010  *               it under the terms of the GNU General Public License as published by
00011  *               the Free Software Foundation, either version 3 of the License, or
00012  *               (at your option) any later version.
00013  * 
00014  *               pelib is distributed in the hope that it will be useful,
00015  *               but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *               MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.   See the
00017  *               GNU General Public License for more details.
00018  * 
00019  *               You should have received a copy of the GNU General Public License
00020  *               along with pelib. If not, see <http://www.gnu.org/licenses/>.
00021  * 
00022  */
00023 
00024 #include <stdlib.h>
00025 #include <math.h>
00026 #include <string.h>
00027 
00028 #include <pelib/string.h>
00029 
00030 #if 10
00031 #define debug(var) printf("[%s:%s:%d] %s = \"%s\"\n", __FILE__, __FUNCTION__, __LINE__, #var, var); fflush(NULL)
00032 #define debug_addr(var) printf("[%s:%s:%d] %s = \"%p\"\n", __FILE__, __FUNCTION__, __LINE__, #var, var); fflush(NULL)
00033 #define debug_int(var) printf("[%s:%s:%d] %s = \"%d\"\n", __FILE__, __FUNCTION__, __LINE__, #var, var); fflush(NULL)
00034 #define debug_size_t(var) printf("[%s:%s:%d] %s = \"%zu\"\n", __FILE__, __FUNCTION__, __LINE__, #var, var); fflush(NULL)
00035 #else
00036 #define debug(var)
00037 #define debug_addr(var)
00038 #define debug_int(var)
00039 #define debug_size_t(var)
00040 #endif
00041 
00042 /*
00043 string*
00044 pelib_alloc_struct(string)()
00045 {
00046         return 1;
00047 }
00048 */
00049 
00050 int
00051 pelib_alloc_buffer(string)(string *str, size_t n)
00052 {
00053         *str = malloc(sizeof(char) * n);
00054         return (*str != NULL);
00055 }
00056 
00057 int
00058 pelib_set_buffer(string)(string *str, void* buffer, size_t n)
00059 {
00060         *str = buffer;
00061         return 1;
00062 }
00063 
00064 int
00065 pelib_copy(string)(string s1, string *s2)
00066 {
00067         if(*s2 == NULL)
00068         {
00069                 pelib_alloc_buffer(string)(s2, (strlen(s1) + 1) * sizeof(char));
00070         }
00071         strcpy(*s2, s1);
00072         return 1;
00073 }
00074 
00075 int
00076 pelib_init(string)(string *val)
00077 {
00078         *val = NULL;
00079         return 1;
00080 }
00081 
00082 int
00083 pelib_destroy(string)(string val)
00084 {
00085         /* Do nothing */
00086         return 1;
00087 }
00088 
00089 int
00090 pelib_compare(string)(string a, string b)
00091 {
00092         return strcmp(a, b);
00093 }
00094 
00095 char*
00096 pelib_string(string)(string a)
00097 {
00098         string cpy;
00099         pelib_alloc_buffer(string)(&cpy, 1 + strlen(a) + 1 + 1);
00100         cpy[0] = '"';
00101         string cpy_ptr = &(cpy[1]);
00102         pelib_copy(string)(a, &cpy_ptr);
00103         cpy[1 + strlen(a)] = '"';
00104         cpy[1 + strlen(a) + 1] = '\0';
00105         return cpy;
00106 }
00107 
00108 char*
00109 pelib_string_detail(string)(string a, int level)
00110 {
00111         return pelib_string(string)(a);
00112 }
00113 
00114 FILE*
00115 pelib_printf(string)(FILE* stream, string a)
00116 {
00117         fprintf(stream, "%s\n", a);
00118         return stream;
00119 }
00120 
00121 // Now include the generic set implementation
00122 #define SET_T string
00123 #include "pelib/set.c"
00124 
00125 // Now include the generic set implementation
00126 #define PAIR_KEY_T string
00127 #define PAIR_VALUE_T string
00128 #include "pelib/pair.c"
00129 
00130 #define ITERATOR_T string
00131 #include "pelib/iterator.c"
00132 
00133 #define ITERATOR_T pair_t(string, string)
00134 #include "pelib/iterator.c"
00135 
00136 // Now include the generic set implementation
00137 #define MAP_KEY_T string
00138 #define MAP_VALUE_T string
00139 #include "pelib/map.c"
00140 
00141 // Now include the generic set implementation
00142 #define SET_T pair_t(string, string)
00143 #include "pelib/set.c"
00144