Overview
Comment: | Remove wchar_t stuff, as that's not portable anyway. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
4360177618d7440bb53147dc2313840c |
User & Date: | js on 2008-12-20 14:19:36 |
Other Links: | manifest | tags |
Context
2008-12-20
| ||
14:26 | Fix printf in tests. check-in: b023058e08 user: js tags: trunk | |
14:19 | Remove wchar_t stuff, as that's not portable anyway. check-in: 4360177618 user: js tags: trunk | |
2008-12-19
| ||
23:15 | Add errno in exceptions where it's useful. check-in: e668c03098 user: js tags: trunk | |
Changes
Modified src/OFString.h from [d8d41ef5ac] to [80f285a39b].
1 2 3 4 5 6 7 8 9 10 11 | /* * Copyright (c) 2008 * Jonathan Schleifer <js@webkeks.org> * * All rights reserved. * * This file is part of libobjfw. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE included in * the packaging of this file. */ | < < | < < < < < < < < < < < < < < < < | < < < < < < | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | /* * Copyright (c) 2008 * Jonathan Schleifer <js@webkeks.org> * * All rights reserved. * * This file is part of libobjfw. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE included in * the packaging of this file. */ #import "OFObject.h" /** * A class for storing and modifying strings. */ @interface OFString: OFObject { char *string; size_t length; } /** * Creates a new OFString. * * \return An initialized OFString */ + new; /** * Creates a new OFString from a C string. * * \param str A C string to initialize the OFString with * \return A new OFString */ + newFromCString: (const char*)str; /** * Initializes an already allocated OFString. * * \return An initialized OFString */ - init; /** * Initializes an already allocated OFString from a C string. * * \param str A C string to initialize the OFString with * \return An initialized OFString */ - initFromCString: (const char*)str; /** * \return The OFString as a wide C string */ - (const char*)cString; /** * \return The length of the OFString */ - (size_t)length; /** * Clones the OFString, creating a new one. * * \return A copy of the OFString */ - (OFString*)clone; |
︙ | ︙ | |||
117 118 119 120 121 122 123 | /** * Append a C string to the OFString. * * \param str A C string to append */ - appendCString: (const char*)str; | < < < < < < < | 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | /** * Append a C string to the OFString. * * \param str A C string to append */ - appendCString: (const char*)str; /** * Reverse the OFString. */ - reverse; /** * Upper the OFString. |
︙ | ︙ |
Modified src/OFString.m from [9f7f197b86] to [737d54ed05].
︙ | ︙ | |||
9 10 11 12 13 14 15 | * the packaging of this file. */ #import "config.h" #import <stdlib.h> #import <string.h> | | < < < < < < < < < < < | | < < | < < < < < < < < < < < < < < < < < < < < | < < < < < < < < < < < < < < < < < < | | | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | | | < | | 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | * the packaging of this file. */ #import "config.h" #import <stdlib.h> #import <string.h> #import <ctype.h> #import "OFString.h" #import "OFExceptions.h" @implementation OFString + new { return [[self alloc] init]; } + newFromCString: (const char*)str { return [[self alloc] initFromCString: str]; } - init { if ((self = [super init])) { length = 0; string = NULL; } return self; } - initFromCString: (const char*)str { if ((self = [super init])) { if (str == NULL) { length = 0; string = NULL; } else { length = strlen(str); string = [self getMemWithSize: length + 1]; memcpy(string, str, length + 1); } } return self; } - (const char*)cString { return string; } - (size_t)length { return length; } - (OFString*)clone { return [OFString newFromCString: string]; } - (OFString*)setTo: (OFString*)str { [self free]; return (self = [str clone]); } - (int)compareTo: (OFString*)str { return strcmp(string, [str cString]); } - append: (OFString*)str { return [self appendCString: [str cString]]; } - appendCString: (const char*)str { char *newstr; size_t newlen, strlength; if (string == NULL) return [self setTo: [OFString newFromCString: str]]; strlength = strlen(str); newlen = length + strlength; newstr = [self resizeMem: string toSize: newlen + 1]; memcpy(newstr + length, str, strlength + 1); length = newlen; string = newstr; return self; } |
︙ | ︙ | |||
208 209 210 211 212 213 214 | } - upper { size_t i = length; while (i--) | | | | 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | } - upper { size_t i = length; while (i--) string[i] = toupper(string[i]); return self; } - lower { size_t i = length; while (i--) string[i] = tolower(string[i]); return self; } @end |
Modified src/OFXMLFactory.h from [74179aac78] to [3e158df444].
1 2 3 4 5 6 7 8 9 10 11 | /* * Copyright (c) 2008 * Jonathan Schleifer <js@webkeks.org> * * All rights reserved. * * This file is part of libobjfw. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE included in * the packaging of this file. */ | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | /* * Copyright (c) 2008 * Jonathan Schleifer <js@webkeks.org> * * All rights reserved. * * This file is part of libobjfw. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE included in * the packaging of this file. */ #import "OFObject.h" /** * The OFXMLFactory class provides an easy way to create XML stanzas. */ @interface OFXMLFactory: OFObject {} /** * XML-escapes a C string. * * \param s The C string to escape * \return The escaped C string. * You need to free it manually! */ + (char*)escapeCString: (const char*)s; /** * Creates an XML stanza. * * \param name The name of the tag as a C string * \param close A boolean whether the tag should be closed * \param data Data that should be inside the tag as a C string. * It will NOT be escaped, so you can also include other stanzas. * \param ... Field / value pairs for the tag in the form "field", "value" as * C strings. * Last element must be NULL. * Example: "field1", "value1", "field2", "value2", NULL * \return The created XML stanza as a C string. * You need to free it manually! */ + (char*)createStanza: (const char*)name withCloseTag: (BOOL)close andData: (const char*)data, ...; /** * Concats an array of C strings into one C string and frees the array of C * strings. * * \param strs An array of C strings * \return The concatenated C strings. * You need to free it manually! */ + (char*)concatAndFreeCStrings: (char**)strs; @end |
Modified src/OFXMLFactory.m from [cb13f5e9ce] to [6c7f478ccb].
︙ | ︙ | |||
10 11 12 13 14 15 16 | */ #import "config.h" #import <stdarg.h> #import <stdlib.h> #import <string.h> | < | 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | */ #import "config.h" #import <stdarg.h> #import <stdlib.h> #import <string.h> #import "OFXMLFactory.h" #import "OFExceptions.h" #import "OFMacros.h" /* * We don't use OFString in this file for performance reasons! |
︙ | ︙ | |||
47 48 49 50 51 52 53 | *str = str2; *len = len2; return YES; } | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | *str = str2; *len = len2; return YES; } static inline BOOL xf_add2chars(char **str, size_t *len, size_t *pos, const char *add) { size_t add_len; add_len = strlen(add); if (!xf_resize_chars(str, len, add_len)) return NO; memcpy(*str + *pos, add, add_len); *pos += add_len; return YES; } @implementation OFXMLFactory + (char*)escapeCString: (const char*)s { char *ret; |
︙ | ︙ | |||
155 156 157 158 159 160 161 | case '&': if (OF_UNLIKELY(!xf_add2chars(&ret, &nlen, &j, "&"))) @throw [OFNoMemException newWithObject: nil andSize: nlen + 5]; break; | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | case '&': if (OF_UNLIKELY(!xf_add2chars(&ret, &nlen, &j, "&"))) @throw [OFNoMemException newWithObject: nil andSize: nlen + 5]; break; default: ret[j++] = s[i]; break; } } ret[j] = 0; |
︙ | ︙ | |||
329 330 331 332 333 334 335 | } else xml[i++] = '>'; xml[i] = 0; return xml; } | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | } else xml[i++] = '>'; xml[i] = 0; return xml; } + (char*)concatAndFreeCStrings: (char**)strs { char *ret; size_t i, len, pos; if (strs[0] == NULL) return NULL; |
︙ | ︙ | |||
459 460 461 462 463 464 465 | free(ret); @throw [OFNoMemException newWithObject: nil andSize: len + strlen(strs[i])]; } } | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | free(ret); @throw [OFNoMemException newWithObject: nil andSize: len + strlen(strs[i])]; } } for (i = 0; strs[i] != NULL; i++) free(strs[i]); ret[pos] = 0; return ret; } @end |
Modified tests/OFList/OFList.m from [4f9ae6f06a] to [2042f99cd4].
1 2 3 4 5 6 7 8 9 10 11 12 13 | /* * Copyright (c) 2008 * Jonathan Schleifer <js@webkeks.org> * * All rights reserved. * * This file is part of libobjfw. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE included in * the packaging of this file. */ #import "config.h" | < < | | | | | | | | | | | | | | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | /* * Copyright (c) 2008 * Jonathan Schleifer <js@webkeks.org> * * All rights reserved. * * This file is part of libobjfw. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE included in * the packaging of this file. */ #import "config.h" #import <stdio.h> #import <string.h> #import "OFString.h" #import "OFList.h" #define NUM_TESTS 5 #define SUCCESS \ { \ printf("\r\033[1;%dmTests successful: %d/%d\033[0m", \ (i == NUM_TESTS - 1 ? 32 : 33), i + 1, NUM_TESTS); \ fflush(stdout); \ } #define FAIL \ { \ printf("\r\033[K\033[1;31mTest %d/%d failed!\033[m\n", \ i + 1, NUM_TESTS); \ return 1; \ } #define CHECK(cond) \ if (cond) \ SUCCESS \ else \ FAIL \ i++; const char *strings[] = { "First String Object", "Second String Object", "Third String Object" }; int main() { size_t i; OFList *list; OFListObject *iter; list = [OFList new]; [list addNew: [OFString newFromCString: strings[0]]]; [list addNew: [OFString newFromCString: strings[1]]]; [list addNew: [OFString newFromCString: strings[2]]]; for (iter = [list first], i = 0; iter != nil; iter = [iter next], i++) if (!strcmp([(OFString*)[iter data] cString], strings[i])) SUCCESS else FAIL CHECK(!strcmp([(OFString*)[[list first] data] cString], strings[0])) CHECK(!strcmp([(OFString*)[[list last] data] cString], strings[2])) puts(""); [list freeIncludingData]; return 0; } |
Modified tests/OFString/OFString.m from [8dc320290e] to [acd4c64051].
︙ | ︙ | |||
39 40 41 42 43 44 45 | if (![s2 compareTo: s4]) puts("s2 and s4 match! GOOD!"); else { puts("s2 and s4 don't match!"); return 1; } | | | | | | | 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | if (![s2 compareTo: s4]) puts("s2 and s4 match! GOOD!"); else { puts("s2 and s4 don't match!"); return 1; } if (!strcmp([[s1 append: s2] cString], "test123")) puts("s1 appended with s2 is the expected string! GOOD!"); else { puts("s1 appended with s2 is not the expected string!"); return 1; } if (strlen([s1 cString]) == [s1 length] && [s1 length] == 7) puts("s1 has the expected length. GOOD!"); else { puts("s1 does not have the expected length!"); return 1; } if (!strcmp([[s1 reverse] cString], "321tset")) puts("Reversed s1 is expected string! GOOD!"); else { puts("Reversed s1 is NOT the expected string!"); return 1; } if (!strcmp([[s1 upper] cString], "321TSET")) puts("Upper s1 is expected string! GOOD!"); else { puts("Upper s1 is NOT expected string!"); return 1; } if (!strcmp([[s1 lower] cString], "321tset")) puts("Lower s1 is expected string! GOOD!"); else { puts("Lower s1 is NOT expected string!"); return 1; } /* Also clears all the memory of the returned C strings */ |
︙ | ︙ |
Modified tests/OFXMLFactory/OFXMLFactory.m from [7fe10add51] to [38666c43a6].
1 2 3 4 5 6 7 8 9 10 11 12 13 | /* * Copyright (c) 2008 * Jonathan Schleifer <js@webkeks.org> * * All rights reserved. * * This file is part of libobjfw. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE included in * the packaging of this file. */ #import "config.h" | < < < < | | | < < < < < < < < < < < < < < < < < < < | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | /* * Copyright (c) 2008 * Jonathan Schleifer <js@webkeks.org> * * All rights reserved. * * This file is part of libobjfw. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE included in * the packaging of this file. */ #import "config.h" #import <stdlib.h> #import <string.h> #import "OFXMLFactory.h" #define NUM_TESTS 10 static int i; inline void check_result(char *result, const char *should) { i++; if (!strcmp(result, should)) { printf("\r\033[1;%dmTests successful: %2d/%d\033[0m", (i == NUM_TESTS ? 32 : 33), i, NUM_TESTS); fflush(stdout); } else { printf("\r\033[K\033[1;31mTest %d/%d failed!\033[0m\n", i, NUM_TESTS); printf("%s is NOT expected result!\n", result); exit(1); } free(result); } inline void |
︙ | ︙ | |||
83 84 85 86 87 88 89 | strs[2] = s3; strs[3] = NULL; check_result([OFXMLFactory concatAndFreeCStrings: strs], "<foo>bar<test/>"); } | < < < < < < < < < < < < < < < < < < < < < < < < < | 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | strs[2] = s3; strs[3] = NULL; check_result([OFXMLFactory concatAndFreeCStrings: strs], "<foo>bar<test/>"); } inline void test_create_stanza() { check_result([OFXMLFactory createStanza: "foo" withCloseTag: NO andData: NULL, NULL], |
︙ | ︙ | |||
163 164 165 166 167 168 169 170 | "bar", "b'az", "x", "y", "a", "b", NULL], "<foo bar='b'az' x='y' a='b'>bar</foo>"); } inline void | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | < < < < < | 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | "bar", "b'az", "x", "y", "a", "b", NULL], "<foo bar='b'az' x='y' a='b'>bar</foo>"); } inline void test_escape() { check_result([OFXMLFactory escapeCString: "<hallo> &welt'\"!&"], "<hallo> &welt'"!&"); } int main() { i = 0; test_escape(); test_create_stanza(); test_concat(); puts(""); return 0; } |