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