summaryrefslogtreecommitdiffstats
path: root/src/translators/pilotdb/libpalm/Block.cpp
blob: c58f6f1f84caa97f9295ffe0e8ace1cf1cdd0546 (plain)
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
/*
 * palm-db-tools: Encapsulate "blocks" of data.
 * Copyright (C) 2000 by Tom Dyas ([email protected])
 *
 * The PalmLib::Block class represents a generic block of data. It is
 * used to simplify passing arrays of pi_char_t around.
 */

#include <cstring>

#include "Block.h"

void PalmLib::Block::reserve(PalmLib::Block::size_type new_size)
{
    if (new_size > capacity()) {
        // Allocate a new buffer containing a copy of the old with the
        // remainder zero'ed out.
        pointer new_data = new pi_char_t[new_size];
        memcpy(new_data, m_data, m_size);
        memset(new_data + m_size, 0, new_size - m_size);

        // Replace the existing buffer.
        delete [] m_data;
        m_data = new_data;
        m_size = new_size;
    }
}

void PalmLib::Block::resize(size_type new_size)
{
    if (new_size < m_size) {
        // Copy the data that will remain to a new buffer and switch to it.
        pointer new_data = new pi_char_t[new_size];
        memcpy(new_data, m_data, new_size);

        // Replace the existing buffer.
        delete [] m_data;
        m_data = new_data;
        m_size = new_size;
    } else if (new_size > m_size) {
        // Copy the data that will remain to a new buffer and switch to it.
        pointer new_data = new pi_char_t[new_size];
        memcpy(new_data, m_data, m_size);
        memset(new_data + m_size, 0, new_size - m_size);

        // Replace the existing buffer.
        delete [] m_data;
        m_data = new_data;
        m_size = new_size;
    }
}

void PalmLib::Block::assign(PalmLib::Block::const_pointer data,
                            const PalmLib::Block::size_type size)
{
    clear();
    if (data && size > 0) {
        m_size = size;
        m_data = new pi_char_t[m_size];
        memcpy(m_data, data, m_size);
    }
}

void PalmLib::Block::assign(const PalmLib::Block::size_type size,
                            const PalmLib::Block::value_type value)
{
    clear();
    if (size > 0) {
        m_size = size;
        m_data = new pi_char_t[m_size];
        memset(m_data, value, m_size);
    }
}

bool operator == (const PalmLib::Block& lhs, const PalmLib::Block& rhs)
{
    if (lhs.size() == rhs.size()) {
        if (lhs.data()) {
            if (memcmp(lhs.data(), rhs.data(), lhs.size()) != 0)
                return false;
        }
        return true;
    }
    return false;
}