uPDFParser

uPDFParser Git Source Tree

Root/src/uPDFTypes.cpp

1/*
2 Copyright 2021 Grégory Soutadé
3
4 This file is part of uPDFParser.
5
6 uPDFParser is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 uPDFParser is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with uPDFParser. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#include <unistd.h>
21#include <algorithm>
22
23#include "uPDFTypes.h"
24#include "uPDFParser_common.h"
25
26namespace uPDFParser
27{
28 Name::Name(const std::string& name):
29DataType(DataType::TYPE::NAME)
30 {
31_value = name;
32 }
33
34 String::String(const std::string& value):
35DataType(DataType::TYPE::STRING)
36 {
37_value = value;
38 }
39
40 HexaString::HexaString(const std::string& value):
41DataType(DataType::TYPE::HEXASTRING)
42 {
43_value = value;
44 }
45
46 std::string Integer::str()
47 {
48std::string sign("");
49// Sign automatically added for negative numbers
50if (_signed && _value >= 0)
51 sign = "+";
52
53return " " + sign + std::to_string(_value);
54 }
55
56 std::string Real::str()
57 {
58std::string res;
59std::string sign("");
60if (_signed && _value >= 0)
61 sign = "+";
62
63res = " " + sign + std::to_string(_value);
64std::replace( res.begin(), res.end(), ',', '.');
65
66return res;
67 }
68
69 std::string Array::str()
70 {
71std::string res("[");
72std::vector<DataType*>::iterator it;
73
74for(it = _value.begin(); it!=_value.end(); it++)
75{
76 if (res.size() > 1 &&
77(*it)->type() != DataType::TYPE::INTEGER &&
78(*it)->type() != DataType::TYPE::REAL)
79res += " ";
80 res += (*it)->str();
81}
82
83return res + "]";
84 }
85
86 void Dictionary::addData(const std::string& key, DataType* value)
87 {
88_value[key] = value;
89 }
90
91 std::string Dictionary::str()
92 {
93std::string res("<<");
94std::map<std::string, DataType*>::iterator it;
95
96for(it = _value.begin(); it!=_value.end(); it++)
97{
98 res += std::string("/") + it->first;
99 if (it->second)
100res += it->second->str();
101}
102
103return res + std::string(">>\n");
104 }
105
106 std::string Stream::str()
107 {
108std::string res = "stream\n";
109const char* streamData = (const char*)data(); // Force reading if not in memory
110res += std::string(streamData, _dataLength);
111res += "\nendstream\n";
112
113return res;
114 }
115
116 unsigned char* Stream::data()
117 {
118if (!_data)
119{
120 if (!fd)
121EXCEPTION(INVALID_STREAM, "Accessing data, but no file descriptor supplied");
122
123 _dataLength = endOffset - startOffset;
124 _data = new unsigned char[_dataLength];
125 freeData = true;
126
127 lseek(fd, startOffset, SEEK_SET);
128 int ret = ::read(fd, _data, _dataLength);
129
130 if ((unsigned int)ret != _dataLength)
131EXCEPTION(INVALID_STREAM, "Not enough data to read (" << ret << ")");
132}
133
134return _data;
135 }
136
137 void Stream::setData(unsigned char* data, unsigned int dataLength, bool freeData)
138 {
139if (_data && freeData)
140 delete[] _data;
141
142dict.deleteKey("Length");
143dict.addData("Length", new Integer(dataLength));
144
145this->_data = data;
146this->_dataLength = dataLength;
147this->freeData = freeData;
148 }
149}

Archive Download this file

Branches