aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/QPDF_Array.cc
blob: 7d42508c42d37b1966174556f06a814e70773cdc (plain) (blame)
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
#include <qpdf/QPDF_Array.hh>

#include <qpdf/QIntC.hh>
#include <qpdf/QUtil.hh>
#include <stdexcept>

QPDF_Array::QPDF_Array(std::vector<QPDFObjectHandle> const& v) :
    QPDFValue(::ot_array, "array")
{
    setFromVector(v);
}

QPDF_Array::QPDF_Array(SparseOHArray const& items) :
    QPDFValue(::ot_array, "array"),
    elements(items)
{
}

std::shared_ptr<QPDFObject>
QPDF_Array::create(std::vector<QPDFObjectHandle> const& items)
{
    return do_create(new QPDF_Array(items));
}

std::shared_ptr<QPDFObject>
QPDF_Array::create(SparseOHArray const& items)
{
    return do_create(new QPDF_Array(items));
}

std::shared_ptr<QPDFObject>
QPDF_Array::shallowCopy()
{
    return create(elements);
}

std::string
QPDF_Array::unparse()
{
    std::string result = "[ ";
    size_t size = this->elements.size();
    for (size_t i = 0; i < size; ++i) {
        result += this->elements.at(i).unparse();
        result += " ";
    }
    result += "]";
    return result;
}

JSON
QPDF_Array::getJSON(int json_version)
{
    JSON j = JSON::makeArray();
    size_t size = this->elements.size();
    for (size_t i = 0; i < size; ++i) {
        j.addArrayElement(this->elements.at(i).getJSON(json_version));
    }
    return j;
}

int
QPDF_Array::getNItems() const
{
    // This should really return a size_t, but changing it would break
    // a lot of code.
    return QIntC::to_int(this->elements.size());
}

QPDFObjectHandle
QPDF_Array::getItem(int n) const
{
    if ((n < 0) || (n >= QIntC::to_int(elements.size()))) {
        throw std::logic_error(
            "INTERNAL ERROR: bounds error accessing QPDF_Array element");
    }
    return this->elements.at(QIntC::to_size(n));
}

void
QPDF_Array::getAsVector(std::vector<QPDFObjectHandle>& v) const
{
    size_t size = this->elements.size();
    for (size_t i = 0; i < size; ++i) {
        v.push_back(this->elements.at(i));
    }
}

void
QPDF_Array::setItem(int n, QPDFObjectHandle const& oh)
{
    this->elements.setAt(QIntC::to_size(n), oh);
}

void
QPDF_Array::setFromVector(std::vector<QPDFObjectHandle> const& v)
{
    this->elements = SparseOHArray();
    for (auto const& iter: v) {
        this->elements.append(iter);
    }
}

void
QPDF_Array::insertItem(int at, QPDFObjectHandle const& item)
{
    // As special case, also allow insert beyond the end
    if ((at < 0) || (at > QIntC::to_int(this->elements.size()))) {
        throw std::logic_error(
            "INTERNAL ERROR: bounds error accessing QPDF_Array element");
    }
    this->elements.insert(QIntC::to_size(at), item);
}

void
QPDF_Array::appendItem(QPDFObjectHandle const& item)
{
    this->elements.append(item);
}

void
QPDF_Array::eraseItem(int at)
{
    this->elements.erase(QIntC::to_size(at));
}

void
QPDF_Array::addExplicitElementsToList(std::list<QPDFObjectHandle>& l) const
{
    for (auto const& iter: this->elements) {
        l.push_back(iter.second);
    }
}