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