00001 /* 00002 * cynapses libc functions 00003 * 00004 * Copyright (c) 2008 by Andreas Schneider <mail@cynapses.org> 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software Foundation, 00018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 * 00020 * vim: ts=2 sw=2 et cindent 00021 */ 00022 00023 /** 00024 * @file c_string.h 00025 * 00026 * @brief Interface of the cynapses string implementations 00027 * 00028 * @defgroup cynStringInternals cynapses libc string functions 00029 * @ingroup cynLibraryAPI 00030 * 00031 * @{ 00032 */ 00033 #ifndef _C_STR_H 00034 #define _C_STR_H 00035 00036 #include "c_private.h" 00037 #include "c_macro.h" 00038 00039 #include <stdlib.h> 00040 00041 struct c_strlist_s; typedef struct c_strlist_s c_strlist_t; 00042 00043 /** 00044 * @brief Structure for a stringlist 00045 * 00046 * Using a for loop you can access the strings saved in the vector. 00047 * 00048 * c_strlist_t strlist; 00049 * int i; 00050 * for (i = 0; i < strlist->count; i++) { 00051 * printf("value: %s", strlist->vector[i]; 00052 * } 00053 */ 00054 struct c_strlist_s { 00055 /** The string vector */ 00056 char **vector; 00057 /** The count of the strings saved in the vector */ 00058 size_t count; 00059 /** Size of strings allocated */ 00060 size_t size; 00061 }; 00062 00063 /** 00064 * @brief Compare to strings if they are equal. 00065 * 00066 * @param a First string to compare. 00067 * @param b Second string to compare. 00068 * 00069 * @return 1 if they are equal, 0 if not. 00070 */ 00071 int c_streq(const char *a, const char *b); 00072 00073 /** 00074 * @brief Create a new stringlist. 00075 * 00076 * @param size Size to allocate. 00077 * 00078 * @return Pointer to the newly allocated stringlist. NULL if an error occured. 00079 */ 00080 c_strlist_t *c_strlist_new(size_t size); 00081 00082 /** 00083 * @brief Expand the stringlist 00084 * 00085 * @param strlist Stringlist to expand 00086 * @param size New size of the strlinglist to expand 00087 * 00088 * @return Pointer to the expanded stringlist. NULL if an error occured. 00089 */ 00090 c_strlist_t *c_strlist_expand(c_strlist_t *strlist, size_t size); 00091 00092 /** 00093 * @brief Add a string to the stringlist. 00094 * 00095 * Duplicates the string and stores it in the stringlist. 00096 * 00097 * @param strlist Stringlist to add the string. 00098 * @param string String to add. 00099 * 00100 * @return 0 on success, less than 0 and errno set if an error occured. 00101 * ENOBUFS if the list is full. 00102 */ 00103 int c_strlist_add(c_strlist_t *strlist, const char *string); 00104 00105 /** 00106 * @brief Removes all strings from the list. 00107 * 00108 * Frees the strings. 00109 * 00110 * @param strlist Stringlist to clear 00111 */ 00112 void c_strlist_clear(c_strlist_t *strlist); 00113 00114 /** 00115 * @brief Destroy the memory of the stringlist. 00116 * 00117 * Frees the strings and the stringlist. 00118 * 00119 * @param strlist Stringlist to destroy 00120 */ 00121 void c_strlist_destroy(c_strlist_t *strlist); 00122 00123 /** 00124 * @breif Replace a string with another string in a source string. 00125 * 00126 * @param src String to search for pattern. 00127 * 00128 * @param pattern Pattern to search for in the source string. 00129 * 00130 * @param repl The string which which should replace pattern if found. 00131 * 00132 * @return Return a pointer to the source string. 00133 */ 00134 char *c_strreplace(char *src, const char *pattern, const char *repl); 00135 00136 /** 00137 * @brief Uppercase a string. 00138 * 00139 * @param str The String to uppercase. 00140 * 00141 * @return The malloced uppered string or NULL on error. 00142 */ 00143 char *c_uppercase(const char* str); 00144 00145 /** 00146 * @brief Lowercase a string. 00147 * 00148 * @param str The String to lowercase. 00149 * 00150 * @return The malloced lowered string or NULL on error. 00151 */ 00152 char *c_lowercase(const char* str); 00153 00154 /** 00155 * @brief Convert a multibyte string to utf8 (Win32). 00156 * 00157 * @param str The multibyte encoded string to convert 00158 * 00159 * @return The malloced converted string or NULL on error. 00160 */ 00161 char* c_utf8(const _TCHAR *str); 00162 00163 /** 00164 * @brief Convert a utf8 encoded string to multibyte (Win32). 00165 * 00166 * @param str The utf8 string to convert. 00167 * 00168 * @return The malloced converted multibyte string or NULL on error. 00169 */ 00170 _TCHAR* c_multibyte(const char *wstr); 00171 00172 #if defined(_WIN32) || defined(WITH_ICONV) 00173 /** 00174 * @brief Free buffer malloced by c_multibyte. 00175 * 00176 * @param buf The buffer to free. 00177 */ 00178 00179 #define c_free_multibyte(x) SAFE_FREE(x) 00180 00181 /** 00182 * @brief Free buffer malloced by c_utf8. 00183 * 00184 * @param buf The buffer to free. 00185 * 00186 */ 00187 #define c_free_utf8(x) SAFE_FREE(x) 00188 #else 00189 #define c_free_multibyte(x) (void)x 00190 #define c_free_utf8(x) (void)x 00191 #endif 00192 00193 00194 /** 00195 * }@ 00196 */ 00197 #endif /* _C_STR_H */ 00198