File: wvbuffer.cc

package info (click to toggle)
wvstreams 4.6.1-19
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,420 kB
  • sloc: cpp: 64,196; ansic: 4,154; sh: 4,025; makefile: 546; perl: 402
file content (115 lines) | stat: -rw-r--r-- 2,628 bytes parent folder | download | duplicates (11)
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
/*
 * Worldvisions Weaver Software:
 *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
 * 
 * Specializations of the generic buffering API.
 */
#include "wvbuf.h"

/***** Specialization for raw memory buffers *****/

void WvBufBase<unsigned char>::putstr(WvStringParm str)
{
    put((const unsigned char*)str.cstr(), str.len());
}


WvString WvBufBase<unsigned char>::getstr()
{
    /* Copy the contents into the string.
     * We used to just return a reference to those bytes, but
     * that required modifying the buffer to append a null
     * terminator, which does not work with read-only buffers.
     * This method is also somewhat safer if a little slower.
     */ 
    WvString result;
    size_t len = used();
    result.setsize(len + 1);
    char *str = result.edit();
    move(str, len);
    str[len] = '\0';
    return result;
}


WvString WvBufBase<unsigned char>::getstr(size_t len)
{
    WvString result;
    result.setsize(len + 1);
    char *str = result.edit();
    move(str, len);
    str[len] = '\0';
    return result;
}


size_t WvBufBase<unsigned char>::strchr(int ch)
{
    size_t offset = 0;
    size_t avail = used();
    while (offset < avail)
    {
        size_t len = optpeekable(offset);
        const unsigned char *str = peek(offset, len);
        for (size_t i = 0; i < len; ++i)
            if (str[i] == ch)
                return offset + i + 1;
        offset += len;
    }
    return 0;
}


size_t WvBufBase<unsigned char>::_match(const void *bytelist,
    size_t numbytes, bool reverse)
{
    size_t offset = 0;
    size_t avail = used();
    const unsigned char *chlist = (const unsigned char*)bytelist;
    while (offset < avail)
    {
        size_t len = optpeekable(offset);
        const unsigned char *str = peek(offset, len);
        for (size_t i = 0; i < len; ++i)
        {
            int ch = str[i];
            size_t c;
            for (c = 0; c < numbytes; ++c)
                if (chlist[c] == ch)
                    break;
            if (reverse)
            {
                if (c == numbytes)
                    continue;
            }
            else
            {
                if (c != numbytes)
                    continue;
            }
            return offset + i;
        }
        offset += len;
    }
    return reverse ? offset : 0;
}


/***** WvConstStringBuffer *****/

WvConstStringBuffer::WvConstStringBuffer(WvStringParm _str)
{
    reset(_str);
}


WvConstStringBuffer::WvConstStringBuffer()
{
}


void WvConstStringBuffer::reset(WvStringParm _str)
{
    xstr = _str;
    WvConstInPlaceBuf::reset(xstr.cstr(), xstr.len());
}