2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #ifndef __NGExtensions_NGCharBuffers_H__
23 #define __NGExtensions_NGCharBuffers_H__
27 #import <Foundation/NSString.h>
28 #import <Foundation/NSException.h>
29 #import <NGExtensions/NGMemoryAllocation.h>
34 unsigned char increaseRatio;
35 unsigned char *buffer; // zero terminated buffer
36 } NGCharBuffer8Struct;
38 typedef NGCharBuffer8Struct *NGCharBuffer8;
40 static inline NGCharBuffer8 NGCharBuffer8_init(NGCharBuffer8 _str,
42 _str->capacity = _capacity;
44 _str->increaseRatio = 2;
45 _str->buffer = NGMallocAtomic(_capacity);
46 _str->buffer[0] = '\0';
49 static inline void NGCharBuffer8_reset(NGCharBuffer8 _str) {
53 _str->increaseRatio = 0;
59 static inline NGCharBuffer8 NGCharBuffer8_new(unsigned _capacity) {
60 NGCharBuffer8 str = NULL;
61 str = NGMalloc(sizeof(NGCharBuffer8Struct));
62 return NGCharBuffer8_init(str, _capacity);
65 static inline NGCharBuffer8 NGCharBuffer8_newWithCString(const char *_cstr) {
66 NGCharBuffer8 str = NULL;
67 str = NGMalloc(sizeof(NGCharBuffer8Struct));
68 str = NGCharBuffer8_init(str, strlen(_cstr) + 2);
69 strcpy(str->buffer, _cstr);
73 static inline void NGCharBuffer8_dealloc(NGCharBuffer8 _str) {
75 NGCharBuffer8_reset(_str);
81 static inline void NGCharBuffer8_checkCapacity(NGCharBuffer8 _str,
83 if (_str->capacity < (_str->length + _needed + 1)) {
85 unsigned char *oldBuffer = _str->buffer;
87 _str->capacity *= _str->increaseRatio;
88 if (_str->capacity < (_str->length + _needed + 1))
89 _str->capacity += _needed + 1;
91 _str->buffer = NGMallocAtomic(_str->capacity + 2);
92 memcpy(_str->buffer, oldBuffer, (_str->length + 1));
98 static inline void NGCharBuffer8_addChar(NGCharBuffer8 _str, unsigned char _c) {
99 NGCharBuffer8_checkCapacity(_str, 1);
101 _str->buffer[_str->length] = _c;
103 _str->buffer[_str->length] = '\0';
106 static inline void NGCharBuffer8_addCString(NGCharBuffer8 _str, char *_cstr) {
113 NGCharBuffer8_checkCapacity(_str, len);
114 strcat(_str->buffer, _cstr);
118 static inline void NGCharBuffer8_removeContents(NGCharBuffer8 _str) {
120 _str->buffer[0] = '\0';
123 static inline NSString *NGCharBuffer8_makeStringAndDealloc(NGCharBuffer8 _str) {
128 NSString *str = [NSString stringWithCString:_str->buffer length:_str->length];
130 NSCAssert3(strlen(_str->buffer) == _str->length,
131 @"length of cstring(%s) and the buffer do not match (%i vs %i)",
132 _str->buffer, strlen(_str->buffer), _str->length);
134 NGCharBuffer8_dealloc(_str); _str = NULL;
139 static inline void NGCharBuffer8_stripTrailingSpaces(NGCharBuffer8 _str) {
142 else if (_str->length == 0)
145 while (_str->length > 0) {
146 unsigned char c = _str->buffer[_str->length - 1];
148 if (isspace((int)c) || (c == '\n') || (c == '\r')) {
155 _str->buffer[_str->length] = '\0';
159 #endif /* __NGExtensions_NGCharBuffers_H__ */