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 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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
/* vstrlist.h: class vstrlist
This file is part of Cygwin.
This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */
#ifdef __cplusplus
struct allocator_interface
{
virtual void * alloc (size_t) = 0;
virtual void free (void *) = 0;
};
/* The allocated_type makes sure to use the free () method of the
same allocator_interface than the alloc () method was used of.
Stores the allocator_interface address before the real object,
to hide it from (construction & destruction of) real object. */
class allocated_type
{
union allocator_store
{
allocator_interface * allocator_;
char alignment_[8];
union pointer
{
void * vptr;
allocator_store * real;
};
};
public:
void * operator new (size_t class_size, allocator_interface & allocator)
{
allocator_store::pointer astore;
astore.vptr = allocator.alloc (sizeof (allocator_store) + class_size);
astore.real->allocator_ = &allocator;
++ astore.real;
return astore.vptr;
}
void operator delete (void * p)
{
allocator_store::pointer astore;
astore.vptr = p;
-- astore.real;
astore.real->allocator_->free (astore.vptr);
}
};
/* Double linked list of char arrays, each being a string buffer,
which's final buffer size and initial string content is defined
by a NULL terminated variable argument list of STRING+LEN pairs,
where each STRING (up to LEN) is concatenated for the initial
string buffer content, and each LEN is added to the final size
of the allocated string buffer.
If LEN is -1, strlen(STRING) is used for LEN.
Needs:
An implementation of the allocator_interface.
Provides:
iterator:
short name for the string_iterator
string_iterator:
provides readonly access via member methods:
string (): readonly string buffer
stringlength (): length (readonly) of initial string
buffer_iterator:
extends string_iterator
provides writeable access via member methods:
buffer (): writeable string buffer
bufferlength (): length (readonly) of allocated buffer
Usage sample:
char * text = "snipe";
vstrlist l;
l.appendv (text, 4, text+3, 2, "", 2, NULL);
buffer_iterator it (l.begin ());
strcpy (it->buffer () + it->stringlength (), "ts");
printf ("Sample result is: '%s'", it->string ());
Sample result is: 'snippets' */
class vstrlist
{
public:
class member
: public allocated_type
{
friend class vstrlist;
friend class string_iterator;
member * prev_;
member * next_;
size_t bufferlength_;
size_t stringlength_;
char buffer_[1]; /* we always have space for the trailing zero */
/* no copy, just swap */
member (member const &);
member & operator = (member const &);
/* anchor */
void * operator new (size_t class_size, allocator_interface & allocator)
{
return allocated_type::operator new (class_size, allocator);
}
/* anchor */
member ()
: allocated_type ()
, prev_ (this)
, next_ (this)
, bufferlength_ (0)
, stringlength_ (0)
, buffer_ ()
{}
/* entry: determine memory size from args */
void * operator new (size_t class_size, allocator_interface & allocator,
char const * part0, va_list parts)
{
char const * part = part0;
va_list partsdup;
va_copy (partsdup, parts);
while (part)
{
int partlen = va_arg (partsdup, int);
if (partlen < 0)
partlen = strlen (part);
class_size += partlen;
part = va_arg (partsdup, char const *);
}
va_end (partsdup);
return allocated_type::operator new (class_size, allocator);
}
/* entry: instantly insert into list */
member (member * before, char const * part0, va_list parts)
: allocated_type ()
, prev_ (NULL)
, next_ (NULL)
, bufferlength_ (0)
, stringlength_ ()
, buffer_ ()
{
prev_ = before->prev_;
next_ = before;
prev_->next_ = this;
next_->prev_ = this;
char * dest = buffer_;
char const * part = part0;
va_list partsdup;
va_copy (partsdup, parts);
while (part)
{
int partlen = va_arg (partsdup, int);
if (partlen < 0)
{
char * old = dest;
dest = stpcpy (old, part);
partlen = dest - old;
}
else
dest = stpncpy (dest, part, partlen);
bufferlength_ += partlen;
part = va_arg (partsdup, const char *);
}
va_end (partsdup);
*dest = (char)0;
stringlength_ = dest - buffer_;
if (bufferlength_ > stringlength_)
memset (++dest, 0, bufferlength_ - stringlength_);
}
/* remove entry from list */
~member ()
{
member * next = next_;
member * prev = prev_;
if (next)
next->prev_ = prev;
if (prev)
prev->next_ = next;
prev_ = NULL;
next_ = NULL;
}
public:
member const * next () const { return next_; }
member * next () { return next_; }
member const * prev () const { return next_; }
member * prev () { return next_; }
/* readonly access */
char const * string () const { return buffer_; }
size_t stringlength () const { return stringlength_; }
/* writeable access */
char * buffer () { return buffer_; }
size_t bufferlength () { return bufferlength_; }
};
/* readonly access */
class string_iterator
{
friend class vstrlist;
friend class buffer_iterator;
member * current_;
string_iterator ();
string_iterator (member * current)
: current_ (current)
{}
public:
string_iterator (string_iterator const & rhs)
: current_ (rhs.current_)
{}
string_iterator & operator = (string_iterator const & rhs)
{
current_ = rhs.current_;
return *this;
}
string_iterator & operator ++ ()
{
current_ = current_->next ();
return *this;
}
string_iterator operator ++ (int)
{
string_iterator ret (*this);
current_ = current_->next ();
return ret;
}
string_iterator & operator -- ()
{
current_ = current_->prev ();
return *this;
}
string_iterator operator -- (int)
{
string_iterator ret (*this);
current_ = current_->prev ();
return ret;
}
bool operator == (string_iterator const & rhs) const
{
return current_ == rhs.current_;
}
bool operator != (string_iterator const & rhs) const
{
return current_ != rhs.current_;
}
/* readonly member access */
member const & operator * () const { return *current_; }
member const * operator -> () const { return current_; }
void remove ()
{
member * old = current_;
++ *this;
delete old;
}
};
/* writeable access */
class buffer_iterator
: public string_iterator
{
public:
explicit /* can be used with vstrlist.begin () */
buffer_iterator (string_iterator const & begin)
: string_iterator (begin)
{}
buffer_iterator (buffer_iterator const & rhs)
: string_iterator (rhs)
{}
buffer_iterator & operator = (buffer_iterator const & rhs)
{
string_iterator::operator = (rhs);
return *this;
}
/* writeable member access */
member & operator * () const { return *current_; }
member * operator -> () const { return current_; }
};
private:
allocator_interface & allocator_;
member * anchor_;
/* not without an allocator */
vstrlist ();
/* no copy, just swap () */
vstrlist (vstrlist const &);
vstrlist & operator = (vstrlist const &);
public:
/* iterator is the string_iterator */
typedef class string_iterator iterator;
iterator begin () { return iterator (anchor_->next ()); }
iterator end () { return iterator (anchor_ ); }
iterator rbegin () { return iterator (anchor_->prev ()); }
iterator rend () { return iterator (anchor_ ); }
vstrlist (allocator_interface & a)
: allocator_ (a)
, anchor_ (NULL) /* exception safety */
{
anchor_ = new (allocator_) member ();
}
~vstrlist ()
{
if (anchor_ != NULL)
{
for (iterator it = begin (); it != end (); it.remove ());
delete anchor_;
}
}
void swap (vstrlist & that)
{
allocator_interface & a = allocator_;
member * m = anchor_;
allocator_ = that.allocator_;
anchor_ = that.anchor_;
that.allocator_ = a;
that.anchor_ = m;
}
string_iterator appendv (char const * part0, va_list parts)
{
member * ret = new (allocator_, part0, parts)
member (anchor_, part0, parts);
return string_iterator (ret);
}
string_iterator appendv (char const * part0, ...)
{
va_list parts;
va_start (parts, part0);
string_iterator ret = appendv (part0, parts);
va_end (parts);
return ret;
}
};
#endif /* __cplusplus */
|