1 /****************************************************************************
2 **
3 ** This file is part of the LibreCAD project, a 2D CAD program
4 **
5 ** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
6 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
7 **
8 **
9 ** This file may be distributed and/or modified under the terms of the
10 ** GNU General Public License version 2 as published by the Free Software
11 ** Foundation and appearing in the file gpl-2.0.txt included in the
12 ** packaging of this file.
13 **
14 ** This program is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ** GNU General Public License for more details.
18 **
19 ** You should have received a copy of the GNU General Public License
20 ** along with this program; if not, write to the Free Software
21 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 **
23 ** This copyright notice MUST APPEAR in all copies of the script!
24 **
25 **********************************************************************/
26
27 #if _MSC_VER > 1000
28 #pragma once
29 #endif // _MSC_VER > 1000
30
31 #include <cstdio>
32
33 #include "dl_writer_ascii.h"
34 #include "dl_exception.h"
35
36
37 /**
38 * Closes the output file.
39 */
close() const40 void DL_WriterA::close() const {
41 m_ofile.close();
42 }
43
44
45 /**
46 * @retval true Opening file has failed.
47 * @retval false Otherwise.
48 */
openFailed() const49 bool DL_WriterA::openFailed() const {
50 return m_ofile.fail();
51 }
52
53
54
55 /**
56 * Writes a real (double) variable to the DXF file.
57 *
58 * @param gc Group code.
59 * @param value Double value
60 */
dxfReal(int gc,double value) const61 void DL_WriterA::dxfReal(int gc, double value) const {
62 char str[256];
63 snprintf(str, 256,"%.16lf", value);
64
65 // fix for german locale:
66 strReplace(str, ',', '.');
67
68 // Cut away those zeros at the end:
69 bool dot = false;
70 int end = -1;
71 for (unsigned int i=0; i<strlen(str); ++i) {
72 if (str[i]=='.') {
73 dot = true;
74 end = i+2;
75 continue;
76 } else if (dot && str[i]!='0') {
77 end = i+1;
78 }
79 }
80 if (end>0 && end<(int)strlen(str)) {
81 str[end] = '\0';
82 }
83
84 dxfString(gc, str);
85 m_ofile.flush();
86 }
87
88
89
90 /**
91 * Writes an int variable to the DXF file.
92 *
93 * @param gc Group code.
94 * @param value Int value
95 */
dxfInt(int gc,int value) const96 void DL_WriterA::dxfInt(int gc, int value) const {
97 m_ofile << (gc<10 ? " " : (gc<100 ? " " : "")) << gc << "\n"
98 << value << "\n";
99 }
100
101
102
103 /**
104 * Writes a hex int variable to the DXF file.
105 *
106 * @param gc Group code.
107 * @param value Int value
108 */
dxfHex(int gc,int value) const109 void DL_WriterA::dxfHex(int gc, int value) const {
110 char str[12];
111 snprintf(str,12, "%0X", value);
112 dxfString(gc, str);
113 }
114
115
116
117 /**
118 * Writes a string variable to the DXF file.
119 *
120 * @param gc Group code.
121 * @param value String
122 */
dxfString(int gc,const char * value) const123 void DL_WriterA::dxfString(int gc, const char* value) const {
124 if (value==NULL) {
125 #ifndef __GCC2x__
126 //throw DL_NullStrExc();
127 #endif
128 }
129 m_ofile << (gc<10 ? " " : (gc<100 ? " " : "")) << gc << "\n"
130 << value << "\n";
131 }
132
133
134
dxfString(int gc,const string & value) const135 void DL_WriterA::dxfString(int gc, const string& value) const {
136 m_ofile << (gc<10 ? " " : (gc<100 ? " " : "")) << gc << "\n"
137 << value << "\n";
138 }
139
140
141 /**
142 * Replaces every occurence of src with dest in the null terminated str.
143 */
strReplace(char * str,char src,char dest)144 void DL_WriterA::strReplace(char* str, char src, char dest) {
145 size_t i;
146 for (i=0; i<strlen(str); i++) {
147 if (str[i]==src) {
148 str[i] = dest;
149 }
150 }
151 }
152
153