]> err.no Git - sope/blob - sope-core/NGExtensions/NGExtensions/NGCharBuffers.h
84732431dba261dafeed32708d1e5feaa55a8c23
[sope] / sope-core / NGExtensions / NGExtensions / NGCharBuffers.h
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21
22 #ifndef __NGExtensions_NGCharBuffers_H__
23 #define __NGExtensions_NGCharBuffers_H__
24
25 #include <string.h>
26 #include <ctype.h>
27 #import <Foundation/NSString.h>
28 #import <Foundation/NSException.h>
29 #import <NGExtensions/NGMemoryAllocation.h>
30
31 typedef struct {
32   unsigned      capacity;
33   unsigned      length;
34   unsigned char increaseRatio;
35   unsigned char *buffer; // zero terminated buffer
36 } NGCharBuffer8Struct;
37
38 typedef NGCharBuffer8Struct *NGCharBuffer8;
39
40 static inline NGCharBuffer8 NGCharBuffer8_init(NGCharBuffer8 _str,
41                                                unsigned _capacity) {
42   _str->capacity      = _capacity;
43   _str->length        = 0;
44   _str->increaseRatio = 2;
45   _str->buffer        = NGMallocAtomic(_capacity);
46   _str->buffer[0]     = '\0';
47   return _str;
48 }
49 static inline void NGCharBuffer8_reset(NGCharBuffer8 _str) {
50   if (_str) {
51     _str->capacity = 0;
52     _str->length   = 0;
53     _str->increaseRatio = 0;
54     NGFree(_str->buffer);
55     _str->buffer = NULL;
56   }
57 }
58
59 static inline NGCharBuffer8 NGCharBuffer8_new(unsigned _capacity) {
60   NGCharBuffer8 str = NULL;
61   str = NGMalloc(sizeof(NGCharBuffer8Struct));
62   return NGCharBuffer8_init(str, _capacity);
63 }
64
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);
70   return str;
71 }
72
73 static inline void NGCharBuffer8_dealloc(NGCharBuffer8 _str) {
74   if (_str) {
75     NGCharBuffer8_reset(_str);
76     NGFree(_str);
77     _str = NULL;
78   }
79 }
80
81 static inline void NGCharBuffer8_checkCapacity(NGCharBuffer8 _str,
82                                                unsigned _needed) {
83   if (_str->capacity < (_str->length + _needed + 1)) {
84     // increase size
85     unsigned char *oldBuffer = _str->buffer;
86
87     _str->capacity *= _str->increaseRatio;
88     if (_str->capacity < (_str->length + _needed + 1))
89       _str->capacity += _needed + 1;
90     
91     _str->buffer   = NGMallocAtomic(_str->capacity + 2);
92     memcpy(_str->buffer, oldBuffer, (_str->length + 1));
93     NGFree(oldBuffer);
94     oldBuffer = NULL;
95   }
96 }
97
98 static inline void NGCharBuffer8_addChar(NGCharBuffer8 _str, unsigned char _c) {
99   NGCharBuffer8_checkCapacity(_str, 1);
100
101   _str->buffer[_str->length] = _c;
102   (_str->length)++;
103   _str->buffer[_str->length] = '\0';
104 }
105
106 static inline void NGCharBuffer8_addCString(NGCharBuffer8 _str, char *_cstr) {
107   unsigned len;
108   
109   if (_cstr == NULL)
110     return;
111
112   len = strlen(_cstr);
113   NGCharBuffer8_checkCapacity(_str, len);
114   strcat(_str->buffer, _cstr);
115   _str->length += len;
116 }
117
118 static inline void NGCharBuffer8_removeContents(NGCharBuffer8 _str) {
119   _str->length = 0;
120   _str->buffer[0] = '\0';
121 }
122
123 static inline NSString *NGCharBuffer8_makeStringAndDealloc(NGCharBuffer8 _str) {
124   if (_str == NULL) {
125     return nil;
126   }
127   else {
128     NSString *str = [NSString stringWithCString:_str->buffer length:_str->length];
129
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);
133
134     NGCharBuffer8_dealloc(_str); _str = NULL;
135     return str;
136   }
137 }
138
139 static inline void NGCharBuffer8_stripTrailingSpaces(NGCharBuffer8 _str) {
140   if (_str == NULL)
141     return;
142   else if (_str->length == 0)
143     return;
144   else {
145     while (_str->length > 0) {
146       unsigned char c = _str->buffer[_str->length - 1];
147
148       if (isspace((int)c) || (c == '\n') || (c == '\r')) {
149         (_str->length)--;
150       }
151       else {
152         break;
153       }
154     }
155     _str->buffer[_str->length] = '\0';
156   }
157 }
158
159 #endif /* __NGExtensions_NGCharBuffers_H__ */