~ubuntu-branches/ubuntu/saucy/librecad/saucy

« back to all changes in this revision

Viewing changes to libraries/libdxfrw/src/dxfreader.cpp

  • Committer: Package Import Robot
  • Author(s): Scott Howard
  • Date: 2013-09-13 21:52:29 UTC
  • mfrom: (1.1.7) (5.2.7 experimental)
  • Revision ID: package-import@ubuntu.com-20130913215229-b18g6qjb8nacro28
Tags: 2.0.0~rc2+nolibs-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************************
 
2
**  libDXFrw - Library to read/write DXF files (ascii & binary)              **
 
3
**                                                                           **
 
4
**  Copyright (C) 2011 Rallaz, rallazz@gmail.com                             **
 
5
**                                                                           **
 
6
**  This library is free software, licensed under the terms of the GNU       **
 
7
**  General Public License as published by the Free Software Foundation,     **
 
8
**  either version 2 of the License, or (at your option) any later version.  **
 
9
**  You should have received a copy of the GNU General Public License        **
 
10
**  along with this program.  If not, see <http://www.gnu.org/licenses/>.    **
 
11
******************************************************************************/
 
12
 
 
13
#include <stdlib.h>
 
14
#include <fstream>
 
15
#include <string>
 
16
#include <sstream>
 
17
#include "dxfreader.h"
 
18
#include "drw_textcodec.h"
 
19
 
 
20
#ifdef DRW_DBG
 
21
#include <iostream> //for debug
 
22
#define DBG(a) std::cerr << a
 
23
#else
 
24
#define DBG(a)
 
25
#endif
 
26
 
 
27
bool dxfReader::readRec(int *codeData, bool skip) {
 
28
//    std::string text;
 
29
    int code;
 
30
 
 
31
#ifdef DRW_DBG
 
32
    count = count+2; //DBG
 
33
/*    if (count > 10250)
 
34
        DBG("line 10256");*/
 
35
#endif
 
36
 
 
37
    if (!readCode(&code))
 
38
        return false;
 
39
    *codeData = code;
 
40
 
 
41
    if (code < 10)
 
42
        readString();
 
43
    else if (code < 60)
 
44
        readDouble();
 
45
    else if (code < 80)
 
46
        readInt();
 
47
    else if (code > 89 && code < 100) //TODO this is an int 32b
 
48
        readInt32();
 
49
    else if (code == 100 || code == 102 || code == 105)
 
50
        readString();
 
51
    else if (code > 109 && code < 150) //skip not used at the v2012
 
52
        readDouble();
 
53
    else if (code > 159 && code < 170) //skip not used at the v2012
 
54
        readInt64();
 
55
    else if (code < 180)
 
56
        readInt();
 
57
    else if (code > 209 && code < 240) //skip not used at the v2012
 
58
        readDouble();
 
59
    else if (code > 269 && code < 290) //skip not used at the v2012
 
60
        readInt();
 
61
    else if (code < 300) //TODO this is a boolean indicator, int in Binary?
 
62
        readBool();
 
63
    else if (code < 370)
 
64
        readString();
 
65
    else if (code < 390)
 
66
        readInt();
 
67
    else if (code < 400)
 
68
        readString();
 
69
    else if (code < 410)
 
70
        readInt();
 
71
    else if (code < 420)
 
72
        readString();
 
73
    else if (code < 430) //TODO this is an int 32b
 
74
        readInt32();
 
75
    else if (code < 440)
 
76
        readString();
 
77
    else if (code < 450) //TODO this is an int 32b
 
78
        readInt32();
 
79
    else if (code < 460) //TODO this is long??
 
80
        readInt();
 
81
    else if (code < 470) //TODO this is a floating point double precision??
 
82
        readDouble();
 
83
    else if (code < 481)
 
84
        readString();
 
85
    else if (code > 998 && code < 1009) //skip not used at the v2012
 
86
        readString();
 
87
    else if (code < 1060) //TODO this is a floating point double precision??
 
88
        readDouble();
 
89
    else if (code < 1071)
 
90
        readInt();
 
91
    else if (code == 1071) //TODO this is an int 32b
 
92
        readInt32();
 
93
    else if (skip)
 
94
        //skip safely this dxf entry ( ok for ascii dxf)
 
95
        readString();
 
96
    else
 
97
        //break in binary files because the conduct is unpredictable
 
98
        return false;
 
99
 
 
100
    return (filestr->good());
 
101
}
 
102
int dxfReader::getHandleString(){
 
103
    int res;
 
104
#if defined(__APPLE__)
 
105
    int Succeeded = sscanf ( strData.c_str(), "%x", &res );
 
106
    if ( !Succeeded || Succeeded == EOF )
 
107
        res = 0;
 
108
#else
 
109
    std::istringstream Convert(strData);
 
110
    if ( !(Convert >> std::hex >>res) )
 
111
        res = 0;
 
112
#endif
 
113
    return res;
 
114
}
 
115
 
 
116
bool dxfReaderBinary::readCode(int *code) {
 
117
    unsigned short *int16p;
 
118
    char buffer[2];
 
119
    filestr->read(buffer,2);
 
120
    int16p = (unsigned short *) buffer;
 
121
//exist a 32bits int (code 90) with 2 bytes???
 
122
    if ((*code == 90) && (*int16p>2000)){
 
123
        DBG(*code); DBG(" de 16bits\n");
 
124
        filestr->seekg(-4, std::ios_base::cur);
 
125
        filestr->read(buffer,2);
 
126
        int16p = (unsigned short *) buffer;
 
127
    }
 
128
    *code = *int16p;
 
129
    DBG(*code); DBG("\n");
 
130
 
 
131
    return (filestr->good());
 
132
}
 
133
 
 
134
bool dxfReaderBinary::readString() {
 
135
    std::getline(*filestr, strData, '\0');
 
136
    DBG(strData); DBG("\n");
 
137
    return (filestr->good());
 
138
}
 
139
 
 
140
bool dxfReaderBinary::readString(std::string *text) {
 
141
    std::getline(*filestr, *text, '\0');
 
142
    DBG(*text); DBG("\n");
 
143
    return (filestr->good());
 
144
}
 
145
 
 
146
bool dxfReaderBinary::readInt() {
 
147
    char buffer[2];
 
148
    filestr->read(buffer,2);
 
149
    intData = (int)((buffer[1] << 8) | buffer[0]);
 
150
    DBG(intData); DBG("\n");
 
151
    return (filestr->good());
 
152
}
 
153
 
 
154
bool dxfReaderBinary::readInt32() {
 
155
    unsigned int *int32p;
 
156
    char buffer[4];
 
157
    filestr->read(buffer,4);
 
158
    int32p = (unsigned int *) buffer;
 
159
    intData = *int32p;
 
160
    DBG(intData); DBG("\n");
 
161
    return (filestr->good());
 
162
}
 
163
 
 
164
bool dxfReaderBinary::readInt64() {
 
165
    unsigned long long int *int64p; //64 bits integer pointer
 
166
    char buffer[8];
 
167
    filestr->read(buffer,8);
 
168
    int64p = (unsigned long long int *) buffer;
 
169
    int64 = *int64p;
 
170
    DBG(int64); DBG(" int64\n");
 
171
    return (filestr->good());
 
172
}
 
173
 
 
174
bool dxfReaderBinary::readDouble() {
 
175
    double *result;
 
176
    char buffer[8];
 
177
    filestr->read(buffer,8);
 
178
    result = (double *) buffer;
 
179
    doubleData = *result;
 
180
    DBG(doubleData); DBG("\n");
 
181
    return (filestr->good());
 
182
}
 
183
 
 
184
//saved as int or add a bool member??
 
185
bool dxfReaderBinary::readBool() {
 
186
    char buffer[1];
 
187
    filestr->read(buffer,1);
 
188
    intData = (int)(buffer[0]);
 
189
    DBG(intData); DBG("\n");
 
190
    return (filestr->good());
 
191
}
 
192
 
 
193
bool dxfReaderAscii::readCode(int *code) {
 
194
    std::string text;
 
195
    std::getline(*filestr, text);
 
196
    *code = atoi(text.c_str());
 
197
    DBG(*code); DBG("\n");
 
198
    return (filestr->good());
 
199
}
 
200
bool dxfReaderAscii::readString(std::string *text) {
 
201
    std::getline(*filestr, *text);
 
202
    if (!text->empty() && text->at(text->size()-1) == '\r')
 
203
        text->erase(text->size()-1);
 
204
    return (filestr->good());
 
205
}
 
206
 
 
207
bool dxfReaderAscii::readString() {
 
208
    std::getline(*filestr, strData);
 
209
    if (!strData.empty() && strData.at(strData.size()-1) == '\r')
 
210
        strData.erase(strData.size()-1);
 
211
    DBG(strData); DBG("\n");
 
212
    return (filestr->good());
 
213
}
 
214
 
 
215
bool dxfReaderAscii::readInt() {
 
216
    std::string text;
 
217
    if (readString(&text)){
 
218
        intData = atoi(text.c_str());
 
219
        DBG(intData); DBG("\n");
 
220
        return true;
 
221
    } else
 
222
        return false;
 
223
}
 
224
 
 
225
bool dxfReaderAscii::readInt32() {
 
226
    return readInt();
 
227
}
 
228
 
 
229
bool dxfReaderAscii::readInt64() {
 
230
    return readInt();
 
231
}
 
232
 
 
233
bool dxfReaderAscii::readDouble() {
 
234
    std::string text;
 
235
    if (readString(&text)){
 
236
#if defined(__APPLE__)
 
237
        int succeeded=sscanf( & (text[0]), "%lg", &doubleData);
 
238
        if(succeeded != 1) {
 
239
            DBG("dxfReaderAscii::readDouble(): reading double error: ");
 
240
            DBG(text);
 
241
            DBG('\n');
 
242
        }
 
243
#else
 
244
        std::istringstream sd(text);
 
245
        sd >> doubleData;
 
246
        DBG(doubleData); DBG('\n');
 
247
#endif
 
248
        return true;
 
249
    } else
 
250
        return false;
 
251
}
 
252
 
 
253
//saved as int or add a bool member??
 
254
bool dxfReaderAscii::readBool() {
 
255
    std::string text;
 
256
    if (readString(&text)){
 
257
        intData = atoi(text.c_str());
 
258
        DBG(intData); DBG("\n");
 
259
        return true;
 
260
    } else
 
261
        return false;
 
262
}
 
263