utf: output_stream 3
[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::string& _filename, int _mode, int ID, bool autorename)
28 {
29     std::ios::openmode ofstream_mode;
30
31     std::string fullfilename = getUniqueFilename(_filename);
32     if (autorename)
33     {
34         fullfilename = getUniqueFilename(_filename);
35
36         char* s = getFullFilename(fullfilename.data());
37         fullfilename = s;
38         FREE(s);
39     }
40     else
41     {
42         char* s = getFullFilename(_filename.data());
43         fullfilename = s;
44         FREE(s);
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         ofstream_mode = std::ios::trunc | std::ios::binary;
56     }
57     else
58     {
59         ofstream_mode = std::ios::app | std::ios::binary;
60     }
61
62     std::ofstream fileDiary(fullfilename, ofstream_mode);
63
64     if (fileDiary.bad())
65     {
66         filename = std::string("");
67         fileAttribMode = -1;
68         setID(-1);
69     }
70     else
71     {
72         filename = fullfilename;
73         fileAttribMode = ofstream_mode;
74         setID(ID);
75     }
76     fileDiary.close();
77 }
78
79 /*--------------------------------------------------------------------------*/
80 Diary::~Diary()
81 {
82     filename = "";
83     fileAttribMode = -1;
84     setID(-1);
85 }
86
87 /*--------------------------------------------------------------------------*/
88 std::string Diary::getFilename(void)
89 {
90     return filename;
91 }
92
93 /*--------------------------------------------------------------------------*/
94 void Diary::write(const std::string& _str, bool bInput)
95 {
96     if (!suspendwrite)
97     {
98         std::ios::openmode ofstream_mode = std::ios::app | std::ios::binary;
99
100         std::ofstream fileDiary(filename.c_str(), ofstream_mode);
101         if (fileDiary.good())
102         {
103             const char *line = NULL;
104             std::string st = _str;
105 #ifdef _MSC_VER
106             /* carriage return for Windows */
107             st = replace(st, "\n", "\r\n");
108             st = replace(st, "\r\r", "\r");
109 #endif
110             line = st.data();
111
112             if (bInput)         // input
113             {
114                 if ((IoModeFilter == DIARY_FILTER_INPUT_AND_OUTPUT) || (IoModeFilter == DIARY_FILTER_ONLY_INPUT))
115                 {
116                     if ((PrefixIoModeFilter == PREFIX_FILTER_INPUT_AND_OUTPUT) || (PrefixIoModeFilter == PREFIX_FILTER_ONLY_INPUT))
117                     {
118                         fileDiary << getDiaryDate(PrefixTimeFormat) << " ";
119                     }
120                     if (line)
121                     {
122                         fileDiary << line;
123                     }
124                 }
125             }
126             else                // output
127             {
128                 if ((IoModeFilter == DIARY_FILTER_INPUT_AND_OUTPUT) || (IoModeFilter == DIARY_FILTER_ONLY_OUTPUT))
129                 {
130                     if ((PrefixIoModeFilter == PREFIX_FILTER_INPUT_AND_OUTPUT) || (PrefixIoModeFilter == PREFIX_FILTER_ONLY_OUTPUT))
131                     {
132                         fileDiary << getDiaryDate(PrefixTimeFormat) << " ";
133                     }
134                     if (line)
135                     {
136                         fileDiary << line;
137                     }
138                 }
139             }
140         }
141         fileDiary.close();
142     }
143 }
144
145 /*--------------------------------------------------------------------------*/
146 void Diary::writeln(const std::string& _str, bool bInput)
147 {
148 #define ENDLINE "\n"
149     write(_str + ENDLINE, bInput);
150 }
151
152 /*--------------------------------------------------------------------------*/
153 int Diary::getID(void) const 
154 {
155     return ID_foutstream;
156 }
157
158 /*--------------------------------------------------------------------------*/
159 void Diary::setID(int _ID)
160 {
161     ID_foutstream = _ID;
162 }
163
164 /*--------------------------------------------------------------------------*/
165 bool Diary::setSuspendWrite(bool bWith)
166 {
167     bool previous = suspendwrite;
168
169     suspendwrite = bWith;
170     return previous;
171 }
172
173 /*--------------------------------------------------------------------------*/
174 bool Diary::getSuspendWrite(void)
175 {
176     return suspendwrite;
177 }
178
179 /*--------------------------------------------------------------------------*/
180 std::string Diary::replace(const std::string& text, const std::string& s, const std::string& replacement)
181 {
182     std::string::size_type pos = 0;
183     std::string ret = text;
184     while (pos != std::string::npos)
185     {
186         pos = ret.find(s, pos);
187         if (pos == std::string::npos)
188             // no more 's' in '*this'
189         {
190             break;
191         }
192
193         ret.replace(pos, s.length(), replacement);
194         pos += replacement.length();
195     }
196     return ret;
197 }
198
199 /*--------------------------------------------------------------------------*/
200 diary_filter Diary::getIOMode(void)
201 {
202     return IoModeFilter;
203 }
204
205 /*--------------------------------------------------------------------------*/
206 void Diary::setIOMode(diary_filter _mode)
207 {
208     IoModeFilter = _mode;
209 }
210
211 /*--------------------------------------------------------------------------*/
212 void Diary::setPrefixMode(diary_prefix_time_format iPrefixTimeFormat)
213 {
214     PrefixTimeFormat = iPrefixTimeFormat;
215 }
216
217 /*--------------------------------------------------------------------------*/
218 void Diary::setPrefixIoModeFilter(diary_prefix_time_filter mode)
219 {
220     PrefixIoModeFilter = mode;
221 }
222
223 /*--------------------------------------------------------------------------*/
224 diary_prefix_time_format Diary::getPrefixMode(void)
225 {
226     return PrefixTimeFormat;
227 }
228
229 /*--------------------------------------------------------------------------*/
230 diary_prefix_time_filter Diary::getPrefixIoModeFilter(void)
231 {
232     return PrefixIoModeFilter;
233 }
234
235 /*--------------------------------------------------------------------------*/
236 bool compareDiary(const Diary& first, const Diary& second)
237 {
238     if (first.getID() < second.getID())
239     {
240         return true;
241     }
242     else
243     {
244         return false;
245     }
246 }
247
248 /*--------------------------------------------------------------------------*/