remove getFullFilename from output_stream and use one in fileio
[scilab.git] / scilab / modules / output_stream / src / cpp / Diary.cpp
1 /*--------------------------------------------------------------------------*/
2 /*
3 * ( http://www.scilab.org/ ) - This file is part of Scilab
4 * Copyright (C) DIGITEO - 2009 - Allan CORNET
5 *
6 * This file must be used under the terms of the CeCILL.
7 * This source file is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution.  The terms
9 * are also available at
10 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
11 *
12 */
13 /*--------------------------------------------------------------------------*/
14 #include <fstream>
15 #include <iostream>
16 #include "Diary.hxx"
17 #include "getDiaryDate.hxx"
18 #include "getUniqueFilename.hxx"
19
20 extern "C"
21 {
22 #include "getFullFilename.h"
23 #include "charEncoding.h"
24 #include "sci_malloc.h"
25 }
26 /*--------------------------------------------------------------------------*/
27 Diary::Diary(const std::wstring& _wfilename, int _mode, int ID, bool autorename)
28 {
29     std::ios::openmode wofstream_mode;
30
31     std::wstring fullfilename = getUniqueFilename(_wfilename);
32     if (autorename)
33     {
34         fullfilename = getUniqueFilename(_wfilename);
35
36         wchar_t* ws = getFullFilenameW(fullfilename.data());
37         fullfilename = ws;
38         FREE(ws);
39     }
40     else
41     {
42         wchar_t* ws = getFullFilenameW(_wfilename.data());
43         fullfilename = ws;
44         FREE(ws);
45     }
46
47     suspendwrite = false;
48
49     PrefixTimeFormat = PREFIX_TIME_FORMAT_UNIX_EPOCH;
50     IoModeFilter = DIARY_FILTER_INPUT_AND_OUTPUT;   // default command & input
51     PrefixIoModeFilter = PREFIX_FILTER_NONE;    // no prefix
52
53     if (_mode == 0)
54     {
55         wofstream_mode = std::ios::trunc | std::ios::binary;
56     }
57     else
58     {
59         wofstream_mode = std::ios::app | std::ios::binary;
60     }
61
62 #ifdef _MSC_VER
63     std::wofstream fileDiary(fullfilename, wofstream_mode);
64 #else
65     wchar_t *wcfile = (wchar_t *) fullfilename.c_str();
66     char *filename = wide_string_to_UTF8(wcfile);
67
68     std::ofstream fileDiary(filename, wofstream_mode);
69
70     if (filename)
71     {
72         FREE(filename);
73         filename = NULL;
74     }
75 #endif
76
77     if (fileDiary.bad())
78     {
79         wfilename = std::wstring(L"");
80         fileAttribMode = -1;
81         setID(-1);
82     }
83     else
84     {
85         wfilename = fullfilename;
86         fileAttribMode = wofstream_mode;
87         setID(ID);
88     }
89     fileDiary.close();
90 }
91
92 /*--------------------------------------------------------------------------*/
93 Diary::~Diary()
94 {
95     wfilename = L"";
96     fileAttribMode = -1;
97     setID(-1);
98 }
99
100 /*--------------------------------------------------------------------------*/
101 std::wstring Diary::getFilename(void)
102 {
103     return wfilename;
104 }
105
106 /*--------------------------------------------------------------------------*/
107 void Diary::write(const std::wstring& _wstr, bool bInput)
108 {
109     if (!suspendwrite)
110     {
111         std::ios::openmode wofstream_mode = std::ios::app | std::ios::binary;
112
113 #ifdef _MSC_VER
114         std::wofstream fileDiary(wfilename.c_str(), wofstream_mode);
115 #else
116         wchar_t *wcfile = (wchar_t *) wfilename.c_str();
117         char *filename = wide_string_to_UTF8(wcfile);
118
119         std::ofstream fileDiary(filename, wofstream_mode);
120         if (filename)
121         {
122             FREE(filename);
123             filename = NULL;
124         }
125 #endif
126
127         if (fileDiary.good())
128         {
129             char *line = NULL;
130             std::wstring wst = _wstr;
131 #ifdef _MSC_VER
132             /* carriage return for Windows */
133             wst = replace(wst, std::wstring(L"\n"), std::wstring(L"\r\n"));
134             wst = replace(wst, std::wstring(L"\r\r"), std::wstring(L"\r"));
135 #endif
136             line = wide_string_to_UTF8(wst.data());
137
138             if (bInput)         // input
139             {
140                 if ((IoModeFilter == DIARY_FILTER_INPUT_AND_OUTPUT) || (IoModeFilter == DIARY_FILTER_ONLY_INPUT))
141                 {
142                     if ((PrefixIoModeFilter == PREFIX_FILTER_INPUT_AND_OUTPUT) || (PrefixIoModeFilter == PREFIX_FILTER_ONLY_INPUT))
143                     {
144                         char *timeInfo = wide_string_to_UTF8((wchar_t *) getDiaryDate(PrefixTimeFormat).c_str());
145
146                         if (timeInfo)
147                         {
148                             fileDiary << timeInfo << " ";
149                             FREE(timeInfo);
150                             timeInfo = NULL;
151                         }
152                     }
153                     if (line)
154                     {
155                         fileDiary << line;
156                     }
157                 }
158             }
159             else                // output
160             {
161                 if ((IoModeFilter == DIARY_FILTER_INPUT_AND_OUTPUT) || (IoModeFilter == DIARY_FILTER_ONLY_OUTPUT))
162                 {
163                     if ((PrefixIoModeFilter == PREFIX_FILTER_INPUT_AND_OUTPUT) || (PrefixIoModeFilter == PREFIX_FILTER_ONLY_OUTPUT))
164                     {
165                         char *timeInfo = wide_string_to_UTF8((wchar_t *) getDiaryDate(PrefixTimeFormat).c_str());
166
167                         if (timeInfo)
168                         {
169                             fileDiary << timeInfo << " ";
170                             FREE(timeInfo);
171                             timeInfo = NULL;
172                         }
173                     }
174                     if (line)
175                     {
176                         fileDiary << line;
177                     }
178                 }
179             }
180
181             if (line)
182             {
183                 FREE(line);
184                 line = NULL;
185             }
186         }
187         fileDiary.close();
188     }
189 }
190
191 /*--------------------------------------------------------------------------*/
192 void Diary::writeln(const std::wstring& _wstr, bool bInput)
193 {
194 #define ENDLINE L"\n"
195     write(_wstr + ENDLINE, bInput);
196 }
197
198 /*--------------------------------------------------------------------------*/
199 int Diary::getID(void) const 
200 {
201     return ID_foutstream;
202 }
203
204 /*--------------------------------------------------------------------------*/
205 void Diary::setID(int _ID)
206 {
207     ID_foutstream = _ID;
208 }
209
210 /*--------------------------------------------------------------------------*/
211 bool Diary::setSuspendWrite(bool bWith)
212 {
213     bool previous = suspendwrite;
214
215     suspendwrite = bWith;
216     return previous;
217 }
218
219 /*--------------------------------------------------------------------------*/
220 bool Diary::getSuspendWrite(void)
221 {
222     return suspendwrite;
223 }
224
225 /*--------------------------------------------------------------------------*/
226 std::wstring Diary::replace(const std::wstring& text, const std::wstring& s, const std::wstring& replacement)
227 {
228     std::wstring::size_type pos = 0;
229     std::wstring ret = text;
230     while (pos != std::wstring::npos)
231     {
232         pos = ret.find(s, pos);
233         if (pos == std::wstring::npos)
234             // no more 's' in '*this'
235         {
236             break;
237         }
238
239         ret.replace(pos, s.length(), replacement);
240         pos += replacement.length();
241     }
242     return ret;
243 }
244
245 /*--------------------------------------------------------------------------*/
246 diary_filter Diary::getIOMode(void)
247 {
248     return IoModeFilter;
249 }
250
251 /*--------------------------------------------------------------------------*/
252 void Diary::setIOMode(diary_filter _mode)
253 {
254     IoModeFilter = _mode;
255 }
256
257 /*--------------------------------------------------------------------------*/
258 void Diary::setPrefixMode(diary_prefix_time_format iPrefixTimeFormat)
259 {
260     PrefixTimeFormat = iPrefixTimeFormat;
261 }
262
263 /*--------------------------------------------------------------------------*/
264 void Diary::setPrefixIoModeFilter(diary_prefix_time_filter mode)
265 {
266     PrefixIoModeFilter = mode;
267 }
268
269 /*--------------------------------------------------------------------------*/
270 diary_prefix_time_format Diary::getPrefixMode(void)
271 {
272     return PrefixTimeFormat;
273 }
274
275 /*--------------------------------------------------------------------------*/
276 diary_prefix_time_filter Diary::getPrefixIoModeFilter(void)
277 {
278     return PrefixIoModeFilter;
279 }
280
281 /*--------------------------------------------------------------------------*/
282 bool compareDiary(const Diary& first, const Diary& second)
283 {
284     if (first.getID() < second.getID())
285     {
286         return true;
287     }
288     else
289     {
290         return false;
291     }
292 }
293
294 /*--------------------------------------------------------------------------*/