1 | /*
|
---|
2 | * TOPPERS Software
|
---|
3 | * Toyohashi Open Platform for Embedded Real-Time Systems
|
---|
4 | *
|
---|
5 | * Copyright (C) 2007-2008 by TAKAGI Nobuhisa
|
---|
6 | *
|
---|
7 | * ãLì ÒÍCȺÌ(1)`(4)Ìðð½·êÉÀèC{\tgEF
|
---|
8 | * Ai{\tgEFAðüϵ½àÌðÜÞDȺ¯¶jðgpE¡»Eü
|
---|
9 | * ÏEÄzziȺCpÆÄÔj·é±Æð³Åø·éD
|
---|
10 | * (1) {\tgEFAð\[XR[hÌ`Åp·éêÉÍCãLÌì
|
---|
11 | * \¦C±Ìpð¨æÑºLÌ³ÛØKèªC»ÌÜÜÌ`Å\[
|
---|
12 | * XR[hÉÜÜêĢ鱯D
|
---|
13 | * (2) {\tgEFAðCCu`®ÈÇC¼Ì\tgEFAJÉg
|
---|
14 | * pÅ«é`ÅÄzz·éêÉÍCÄzzɺ¤hL
|
---|
15 | gip
|
---|
16 | * Ò}j
|
---|
17 | AÈÇjÉCãLÌì \¦C±Ìpð¨æÑºL
|
---|
18 | * Ì³ÛØKèðfÚ·é±ÆD
|
---|
19 | * (3) {\tgEFAðC@íÉgÝÞÈÇC¼Ì\tgEFAJÉg
|
---|
20 | * pūȢ`ÅÄzz·éêÉÍCÌ¢¸ê©Ìðð½·±
|
---|
21 | * ÆD
|
---|
22 | * (a) Äzzɺ¤hL
|
---|
23 | gipÒ}j
|
---|
24 | AÈÇjÉCãLÌ
|
---|
25 | * ì \¦C±Ìpð¨æÑºLÌ³ÛØKèðfÚ·é±ÆD
|
---|
26 | * (b) ÄzzÌ`ÔðCÊÉèßéû@ÉæÁÄCTOPPERSvWFNgÉ
|
---|
27 | * ñ·é±ÆD
|
---|
28 | * (4) {\tgEFAÌpÉæè¼ÚIܽÍÔÚIɶ¶é¢©Èé¹
|
---|
29 | * Q©çàCãLì Ò¨æÑTOPPERSvWFNgðÆÓ·é±ÆD
|
---|
30 | * ܽC{\tgEFAÌ[UܽÍGh[U©çÌ¢©Èé
|
---|
31 | * RÉîÿ©çàCãLì Ò¨æÑTOPPERSvWFNgð
|
---|
32 | * ÆÓ·é±ÆD
|
---|
33 | *
|
---|
34 | * {\tgEFAÍC³ÛØÅñ³êÄ¢éàÌÅ éDãLì Ò¨
|
---|
35 | * æÑTOPPERSvWFNgÍC{\tgEFAÉÖµÄCÁèÌgpÚI
|
---|
36 | * ÉηéK«àÜßÄC¢©ÈéÛØàsíÈ¢DܽC{\tgEF
|
---|
37 | * AÌpÉæè¼ÚIܽÍÔÚIɶ¶½¢©Èé¹QÉÖµÄàC»
|
---|
38 | * ÌÓCðíÈ¢D
|
---|
39 | *
|
---|
40 | */
|
---|
41 | #include <map>
|
---|
42 | #include <iostream>
|
---|
43 | #include <sstream>
|
---|
44 | #include <boost/filesystem/path.hpp>
|
---|
45 | #include <boost/filesystem/operations.hpp>
|
---|
46 | #include "toppers/workaround.hpp"
|
---|
47 | #include "toppers/output_file.hpp"
|
---|
48 | #include "toppers/io.hpp"
|
---|
49 | #include "toppers/global.hpp"
|
---|
50 | #include "toppers/diagnostics.hpp"
|
---|
51 |
|
---|
52 | namespace toppers
|
---|
53 | {
|
---|
54 | namespace
|
---|
55 | {
|
---|
56 | struct context
|
---|
57 | {
|
---|
58 | std::ostringstream ostr;
|
---|
59 | std::string tempname;
|
---|
60 | };
|
---|
61 |
|
---|
62 | typedef std::map< std::string, std::tr1::shared_ptr< context > > context_map;
|
---|
63 |
|
---|
64 | inline context_map& get_context_map()
|
---|
65 | {
|
---|
66 | static context_map m;
|
---|
67 | return m;
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | /*!
|
---|
72 | * \brief oÍXg[ÌQÆ
|
---|
73 | * \return oÍXg[
|
---|
74 | *
|
---|
75 | * Ýè³êÄ¢ét@CÉoÍ·é½ßÌoÍXg[ðÔ·B
|
---|
76 | * ùÉoÍXg[ª¶¬³êÄ¢éêÍ»ÌQÆðÔµA»¤ÅȯêÎV½ÉoÍXg[𶬷éB
|
---|
77 | *
|
---|
78 | * t@C¼ÆµÄA"stdout"ªÝè³êÄ¢êÎWoÍA"stderr"ªÝè³êÄ¢êÎWG[oÍÉÈéB
|
---|
79 | */
|
---|
80 | std::ostream& output_file::ostr() const
|
---|
81 | {
|
---|
82 | if ( filename_ == "stdout" )
|
---|
83 | {
|
---|
84 | return std::cout;
|
---|
85 | }
|
---|
86 | else if ( filename_ == "stderr" )
|
---|
87 | {
|
---|
88 | return std::cerr;
|
---|
89 | }
|
---|
90 |
|
---|
91 | std::string path( path_name( filename_ ) );
|
---|
92 | context_map::const_iterator iter( get_context_map().find( path ) );
|
---|
93 | if ( iter == get_context_map().end() )
|
---|
94 | {
|
---|
95 | std::tr1::shared_ptr< context > ctx( new context );
|
---|
96 | get_context_map().insert( std::make_pair( path, ctx ) );
|
---|
97 | return ctx->ostr;
|
---|
98 | }
|
---|
99 | return iter->second->ostr;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /*!
|
---|
103 | * \brief ãÉi[³êÄ¢éàeðÀÛÉt@CÉoÍ·éB
|
---|
104 | *
|
---|
105 | * context_map ƵÄãÉi[³ê½àeðA»ê¼êÌt@CÉεÄoÍ·éB
|
---|
106 | * oÍ·éÉ ½èAܸ³Ìt@Cð .org ðt¯½t@C¼ÉÏX·éB
|
---|
107 | * ±±ÅAÈOÉ Á½ .org Íjó³êéB
|
---|
108 | * t@CðÉ«ÝArÅG[ª¶µ½êÍA.org ð³Ìt@C¼Éß·B
|
---|
109 | * rÅÐÆÂÅàG[ª¶µ½êÍASt@CÉ墀 .org ð³Éß·B
|
---|
110 | * ÈOÉ Á½ .org ͵ȢB
|
---|
111 | */
|
---|
112 | void output_file::save()
|
---|
113 | {
|
---|
114 | namespace fs = boost::filesystem;
|
---|
115 | std::vector< std::string > saved_files;
|
---|
116 |
|
---|
117 | try
|
---|
118 | {
|
---|
119 | for ( context_map::const_iterator iter( get_context_map().begin() ), last( get_context_map().end() );
|
---|
120 | iter != last;
|
---|
121 | ++iter )
|
---|
122 | {
|
---|
123 | if ( iter->first != "" )
|
---|
124 | {
|
---|
125 | fs::path filename( iter->first, fs::native );
|
---|
126 | fs::path backup( iter->first + ".org", fs::native );
|
---|
127 | bool existed = fs::exists( filename );
|
---|
128 |
|
---|
129 | try
|
---|
130 | {
|
---|
131 | std::string file( filename.native_file_string() );
|
---|
132 | if ( existed )
|
---|
133 | {
|
---|
134 | fs::rename( filename, backup );
|
---|
135 | }
|
---|
136 | write( file, iter->second->ostr.str() ); // ±±Å«Þ
|
---|
137 | saved_files.push_back( file );
|
---|
138 | fs::remove( backup );
|
---|
139 | }
|
---|
140 | catch ( ... )
|
---|
141 | {
|
---|
142 | if ( existed )
|
---|
143 | {
|
---|
144 | fs::remove( filename );
|
---|
145 | fs::rename( backup, filename );
|
---|
146 | }
|
---|
147 | throw;
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|
151 | }
|
---|
152 | catch ( ... )
|
---|
153 | {
|
---|
154 | // ·ÅÉ«ñ¾t@Cð³Éß·B
|
---|
155 | for ( std::vector< std::string >::const_iterator iter( saved_files.begin() ), last( saved_files.end() );
|
---|
156 | iter != last;
|
---|
157 | ++iter )
|
---|
158 | {
|
---|
159 | fs::path filename( *iter, fs::native );
|
---|
160 | fs::remove( filename );
|
---|
161 | fs::path backup( *iter + ".org", fs::native );
|
---|
162 | if ( fs::exists( backup ) )
|
---|
163 | {
|
---|
164 | fs::rename( backup, filename );
|
---|
165 | }
|
---|
166 | }
|
---|
167 | throw;
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | /*!
|
---|
172 | * \brief wèµ½t@C¼Éε½pX¼ðÔ·B
|
---|
173 | * \param[in] filename t@C¼
|
---|
174 | * \return pX¼
|
---|
175 | *
|
---|
176 | * --output-directory IvVÅwèµ½oÍæfBNgð½fµ½pX¼ð¶¬·éB
|
---|
177 | * ½¾µAºLÌt@C¼ÌêÍ»ÌÜÜ̶ñðÔ·B
|
---|
178 | * - "stdout"
|
---|
179 | * - "stderr"
|
---|
180 | * - ""
|
---|
181 | */
|
---|
182 | std::string output_file::path_name( std::string const& filename )
|
---|
183 | {
|
---|
184 | if ( filename == "stdin" || filename == "stderr" || filename == "" )
|
---|
185 | {
|
---|
186 | return filename;
|
---|
187 | }
|
---|
188 | namespace fs = boost::filesystem;
|
---|
189 | boost::any output_directory = global( "output-directory" );
|
---|
190 | if ( output_directory.empty() )
|
---|
191 | {
|
---|
192 | return filename;
|
---|
193 | }
|
---|
194 | fs::path dir( get_global< std::string >( "output-directory" ), fs::native );
|
---|
195 | return ( dir/filename ).native_file_string();
|
---|
196 | }
|
---|
197 |
|
---|
198 | /*!
|
---|
199 | * \brief wèt@CÖÌ«Ýf[^ðQÆ
|
---|
200 | * \param[in] filename oÍæÌt@C¼
|
---|
201 | * \return «Ýf[^
|
---|
202 | */
|
---|
203 | std::string output_file::get_file_data( std::string const& filename )
|
---|
204 | {
|
---|
205 | context_map::const_iterator iter( get_context_map().find( path_name( filename ) ) ), last( get_context_map().end() );
|
---|
206 | if ( iter != last )
|
---|
207 | {
|
---|
208 | return iter->second->ostr.str();
|
---|
209 | }
|
---|
210 | return "";
|
---|
211 | }
|
---|
212 |
|
---|
213 | /*!
|
---|
214 | * \brief wèt@CÖÌ«Ýf[^ðÝè
|
---|
215 | * \param[in] filename oÍæÌt@C¼
|
---|
216 | * \param[in] data «Ýf[^
|
---|
217 | * \retval true Ýè¬÷
|
---|
218 | * \retval false Ýè¸s
|
---|
219 | */
|
---|
220 | bool output_file::set_file_data( std::string const& filename, std::string const& data )
|
---|
221 | {
|
---|
222 | output_file ofile( filename, std::ios_base::out );
|
---|
223 | dynamic_cast< std::ostringstream& >( ofile.ostr() ).str( data );
|
---|
224 | return true;
|
---|
225 | }
|
---|
226 |
|
---|
227 | /*!
|
---|
228 | * \brief ãÉi[³êÄ¢é«Ýæ¤f[^ðÁ
|
---|
229 | * \param[in] filename oÍæÌt@C¼
|
---|
230 | * \retval true f[^ÌÁ¬÷
|
---|
231 | * \retval false wèµ½t@CÖÌoÍf[^ª¶ÝµÈ¢
|
---|
232 | */
|
---|
233 | bool output_file::clear_file_data( std::string const& filename )
|
---|
234 | {
|
---|
235 | context_map::const_iterator iter( get_context_map().find( path_name( filename ) ) ), last( get_context_map().end() );
|
---|
236 | if ( iter != last )
|
---|
237 | {
|
---|
238 | iter->second->ostr.str( "" );
|
---|
239 | return true;
|
---|
240 | }
|
---|
241 | return false;
|
---|
242 | }
|
---|
243 |
|
---|
244 | }
|
---|