]> Shamusworld >> Repos - architektonas/blob - dxflib/src/dl_writer_ascii.cpp
Initial import
[architektonas] / dxflib / src / dl_writer_ascii.cpp
1 /****************************************************************************
2 ** $Id: dl_writer_ascii.cpp 242 2004-04-12 22:39:43Z andrew $
3 **
4 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
5 ** Copyright (C) 2001 Robert J. Campbell Jr.
6 **
7 ** This file is part of the dxflib project.
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 LICENSE.GPL included in the
12 ** packaging of this file.
13 **
14 ** Licensees holding valid dxflib Professional Edition licenses may use 
15 ** this file in accordance with the dxflib Commercial License
16 ** Agreement provided with the Software.
17 **
18 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
19 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 **
21 ** See http://www.ribbonsoft.com for further details.
22 **
23 ** Contact info@ribbonsoft.com if any conditions of this licensing are
24 ** not clear to you.
25 **
26 **********************************************************************/
27
28 #if _MSC_VER > 1000
29 #pragma once
30 #endif // _MSC_VER > 1000
31
32 #include <stdio.h>
33
34 #include "dl_writer_ascii.h"
35 #include "dl_exception.h"
36
37
38 /**
39  * Closes the output file.
40  */
41 void DL_WriterA::close() const {
42     m_ofile.close();
43 }
44
45
46 /**
47  * @retval true Opening file has failed.
48  * @retval false Otherwise.
49  */
50 bool DL_WriterA::openFailed() const {
51         return m_ofile.fail();
52 }
53
54
55
56 /**
57  * Writes a real (double) variable to the DXF file.
58  *
59  * @param gc Group code.
60  * @param value Double value
61  */
62 void DL_WriterA::dxfReal(int gc, double value) const {
63     char str[256];
64     sprintf(str, "%.16lf", value);
65         
66         // fix for german locale:
67         strReplace(str, ',', '.');
68
69     // Cut away those zeros at the end:
70     bool dot = false;
71     int end = -1;
72     for (unsigned int i=0; i<strlen(str); ++i) {
73         if (str[i]=='.') {
74             dot = true;
75             end = i+2;
76             continue;
77         } else if (dot && str[i]!='0') {
78             end = i+1;
79         }
80     }
81     if (end>0 && end<(int)strlen(str)) {
82         str[end] = '\0';
83     }
84
85     dxfString(gc, str);
86     m_ofile.flush();
87 }
88
89
90
91 /**
92  * Writes an int variable to the DXF file.
93  *
94  * @param gc Group code.
95  * @param value Int value
96  */
97 void DL_WriterA::dxfInt(int gc, int value) const {
98     m_ofile << (gc<10 ? "  " : (gc<100 ? " " : "")) << gc << "\n"
99     << value << "\n";
100 }
101
102
103
104 /**
105  * Writes a hex int variable to the DXF file.
106  *
107  * @param gc Group code.
108  * @param value Int value
109  */
110 void DL_WriterA::dxfHex(int gc, int value) const {
111     char str[12];
112     sprintf(str, "%0X", value);
113     dxfString(gc, str);
114 }
115
116
117
118 /**
119  * Writes a string variable to the DXF file.
120  *
121  * @param gc Group code.
122  * @param value String
123  */
124 void DL_WriterA::dxfString(int gc, const char* value) const {
125     if (value==NULL) {
126 #ifndef __GCC2x__
127         throw DL_NullStrExc();
128 #endif
129     }
130     m_ofile << (gc<10 ? "  " : (gc<100 ? " " : "")) << gc << "\n"
131     << value << "\n";
132 }
133
134
135
136 void DL_WriterA::dxfString(int gc, const string& value) const {
137     m_ofile << (gc<10 ? "  " : (gc<100 ? " " : "")) << gc << "\n"
138     << value << "\n";
139 }
140
141
142 /**
143  * Replaces every occurence of src with dest in the null terminated str.
144  */
145 void DL_WriterA::strReplace(char* str, char src, char dest) {
146         size_t i;
147         for     (i=0; i<strlen(str); i++) {
148                 if (str[i]==src) {
149                         str[i] = dest;
150                 }
151         }
152 }
153