1 /******************************************************************************
2 **  libDXFrw - Library to read/write DXF files (ascii & binary)              **
3 **                                                                           **
4 **  Copyright (C) 2011-2015 José F. Soriano, 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 <cstdlib>
14 #include <fstream>
15 #include <string>
16 #include <algorithm>
17 #include "dxfwriter.h"
18 
19 //RLZ TODO change std::endl to x0D x0A (13 10)
20 /*bool dxfWriter::readRec(int *codeData, bool skip) {
21 //    std::string text;
22     int code;
23 
24 #ifdef DRW_DBG
25     count = count+2; //DBG
26 #endif
27 
28     if (!readCode(&code))
29         return false;
30     *codeData = code;
31 
32     if (code < 10)
33         readString();
34     else if (code < 60)
35         readDouble();
36     else if (code < 80)
37         readInt();
38     else if (code > 89 && code < 100) //TODO this is an int 32b
39         readInt32();
40     else if (code == 100 || code == 102 || code == 105)
41         readString();
42     else if (code > 109 && code < 150) //skip not used at the v2012
43         readDouble();
44     else if (code > 159 && code < 170) //skip not used at the v2012
45         readInt64();
46     else if (code < 180)
47         readInt();
48     else if (code > 209 && code < 240) //skip not used at the v2012
49         readDouble();
50     else if (code > 269 && code < 290) //skip not used at the v2012
51         readInt();
52     else if (code < 300) //TODO this is a boolean indicator, int in Binary?
53         readBool();
54     else if (code < 370)
55         readString();
56     else if (code < 390)
57         readInt();
58     else if (code < 400)
59         readString();
60     else if (code < 410)
61         readInt();
62     else if (code < 420)
63         readString();
64     else if (code < 430) //TODO this is an int 32b
65         readInt32();
66     else if (code < 440)
67         readString();
68     else if (code < 450) //TODO this is an int 32b
69         readInt32();
70     else if (code < 460) //TODO this is long??
71         readInt();
72     else if (code < 470) //TODO this is a floating point double precision??
73         readDouble();
74     else if (code < 481)
75         readString();
76     else if (code > 998 && code < 1009) //skip not used at the v2012
77         readString();
78     else if (code < 1060) //TODO this is a floating point double precision??
79         readDouble();
80     else if (code < 1071)
81         readInt();
82     else if (code == 1071) //TODO this is an int 32b
83         readInt32();
84     else if (skip)
85         //skip safely this dxf entry ( ok for ascii dxf)
86         readString();
87     else
88         //break in binary files because the conduct is unpredictable
89         return false;
90 
91     return (filestr->good());
92 }*/
93 
writeUtf8String(int code,std::string text)94 bool dxfWriter::writeUtf8String(int code, std::string text) {
95     std::string t = encoder.fromUtf8(text);
96     return writeString(code, t);
97 }
98 
writeUtf8Caps(int code,std::string text)99 bool dxfWriter::writeUtf8Caps(int code, std::string text) {
100     std::string strname = text;
101     std::transform(strname.begin(), strname.end(), strname.begin(),::toupper);
102     std::string t = encoder.fromUtf8(strname);
103     return writeString(code, t);
104 }
105 
writeString(int code,std::string text)106 bool dxfWriterBinary::writeString(int code, std::string text) {
107     char bufcode[2];
108     bufcode[0] =code & 0xFF;
109     bufcode[1] =code  >> 8;
110     filestr->write(bufcode, 2);
111     *filestr << text << '\0';
112     return (filestr->good());
113 }
114 
115 /*bool dxfWriterBinary::readCode(int *code) {
116     unsigned short *int16p;
117     char buffer[2];
118     filestr->read(buffer,2);
119     int16p = (unsigned short *) buffer;
120 //exist a 32bits int (code 90) with 2 bytes???
121     if ((*code == 90) && (*int16p>2000)){
122         DBG(*code); DBG(" de 16bits\n");
123         filestr->seekg(-4, std::ios_base::cur);
124         filestr->read(buffer,2);
125         int16p = (unsigned short *) buffer;
126     }
127     *code = *int16p;
128     DBG(*code); DBG("\n");
129 
130     return (filestr->good());
131 }*/
132 
133 /*bool dxfWriterBinary::readString() {
134     std::getline(*filestr, strData, '\0');
135     DBG(strData); DBG("\n");
136     return (filestr->good());
137 }*/
138 
139 /*bool dxfWriterBinary::readString(std::string *text) {
140     std::getline(*filestr, *text, '\0');
141     DBG(*text); DBG("\n");
142     return (filestr->good());
143 }*/
144 
writeInt16(int code,int data)145 bool dxfWriterBinary::writeInt16(int code, int data) {
146     char bufcode[2];
147     char buffer[2];
148     bufcode[0] =code & 0xFF;
149     bufcode[1] =code  >> 8;
150     buffer[0] =data & 0xFF;
151     buffer[1] =data  >> 8;
152     filestr->write(bufcode, 2);
153     filestr->write(buffer, 2);
154     return (filestr->good());
155 }
156 
writeInt32(int code,int data)157 bool dxfWriterBinary::writeInt32(int code, int data) {
158     char buffer[4];
159     buffer[0] =code & 0xFF;
160     buffer[1] =code  >> 8;
161     filestr->write(buffer, 2);
162 
163     buffer[0] =data & 0xFF;
164     buffer[1] =data  >> 8;
165     buffer[2] =data  >> 16;
166     buffer[3] =data  >> 24;
167     filestr->write(buffer, 4);
168     return (filestr->good());
169 }
170 
writeInt64(int code,unsigned long long int data)171 bool dxfWriterBinary::writeInt64(int code, unsigned long long int data) {
172     char buffer[8];
173     buffer[0] =code & 0xFF;
174     buffer[1] =code  >> 8;
175     filestr->write(buffer, 2);
176 
177     buffer[0] =data & 0xFF;
178     buffer[1] =data  >> 8;
179     buffer[2] =data  >> 16;
180     buffer[3] =data  >> 24;
181     buffer[4] =data  >> 32;
182     buffer[5] =data  >> 40;
183     buffer[6] =data  >> 48;
184     buffer[7] =data  >> 56;
185     filestr->write(buffer, 8);
186     return (filestr->good());
187 }
188 
writeDouble(int code,double data)189 bool dxfWriterBinary::writeDouble(int code, double data) {
190     char bufcode[2];
191     char buffer[8];
192     bufcode[0] =code & 0xFF;
193     bufcode[1] =code  >> 8;
194     filestr->write(bufcode, 2);
195 
196     unsigned char *val;
197     val = (unsigned char *) &data;
198     for (int i=0; i<8; i++) {
199         buffer[i] =val[i];
200     }
201     filestr->write(buffer, 8);
202     return (filestr->good());
203 }
204 
205 //saved as int or add a bool member??
writeBool(int code,bool data)206 bool dxfWriterBinary::writeBool(int code, bool data) {
207     char buffer[1];
208     char bufcode[2];
209     bufcode[0] =code & 0xFF;
210     bufcode[1] =code  >> 8;
211     filestr->write(bufcode, 2);
212     buffer[0] = data;
213     filestr->write(buffer, 1);
214     return (filestr->good());
215 }
216 
dxfWriterAscii(std::ofstream * stream)217 dxfWriterAscii::dxfWriterAscii(std::ofstream *stream):dxfWriter(stream){
218     filestr->precision(16);
219 }
220 
writeString(int code,std::string text)221 bool dxfWriterAscii::writeString(int code, std::string text) {
222 //    *filestr << code << std::endl << text << std::endl ;
223     filestr->width(3);
224     *filestr << std::right << code << std::endl;
225     filestr->width(0);
226     *filestr << std::left << text << std::endl;
227     /*    std::getline(*filestr, strData, '\0');
228     DBG(strData); DBG("\n");*/
229     return (filestr->good());
230 }
231 
writeInt16(int code,int data)232 bool dxfWriterAscii::writeInt16(int code, int data) {
233 //    *filestr << std::right << code << std::endl << data << std::endl;
234     filestr->width(3);
235     *filestr << std::right << code << std::endl;
236     filestr->width(5);
237     *filestr << data << std::endl;
238     return (filestr->good());
239 }
240 
writeInt32(int code,int data)241 bool dxfWriterAscii::writeInt32(int code, int data) {
242     return writeInt16(code, data);
243 }
244 
writeInt64(int code,unsigned long long int data)245 bool dxfWriterAscii::writeInt64(int code, unsigned long long int data) {
246 //    *filestr << code << std::endl << data << std::endl;
247     filestr->width(3);
248     *filestr << std::right << code << std::endl;
249     filestr->width(5);
250     *filestr << data << std::endl;
251     return (filestr->good());
252 }
253 
writeDouble(int code,double data)254 bool dxfWriterAscii::writeDouble(int code, double data) {
255 //    std::streamsize prec = filestr->precision();
256 //    filestr->precision(12);
257 //    *filestr << code << std::endl << data << std::endl;
258     filestr->width(3);
259     *filestr << std::right << code << std::endl;
260     *filestr << data << std::endl;
261 //    filestr->precision(prec);
262     return (filestr->good());
263 }
264 
265 //saved as int or add a bool member??
writeBool(int code,bool data)266 bool dxfWriterAscii::writeBool(int code, bool data) {
267     *filestr << code << std::endl << data << std::endl;
268     return (filestr->good());
269 }
270 
271