1 /*
2 *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 *  Copyright (C) 2008-2008 - DIGITEO - Antoine ELIAS
4 *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13 *
14 */
15 
16 #include "filemanager.hxx"
17 
18 extern "C"
19 {
20 #include "os_string.h"
21 #include <stdio.h>
22 }
23 
24 std::vector<types::File*> FileManager::m_fileList;
25 int FileManager::m_iCurrentFile = -1;
26 
getFileID(const std::wstring & _stFilename)27 int FileManager::getFileID(const std::wstring& _stFilename)
28 {
29     for (int i = 0 ; i < static_cast<int>(m_fileList.size()) ; i++)
30     {
31         if (m_fileList[i] != NULL && m_fileList[i]->getFilename() == _stFilename)
32         {
33             return i;
34         }
35     }
36     return -1;
37 }
38 
getFileMaxID()39 int FileManager::getFileMaxID()
40 {
41     return static_cast<int>(m_fileList.size());
42 }
43 
isOpened(const std::wstring & _stFilename)44 bool FileManager::isOpened(const std::wstring& _stFilename)
45 {
46     for (int i = 0 ; i < static_cast<int>(m_fileList.size()) ; i++)
47     {
48         if (m_fileList[i] != NULL && m_fileList[i]->getFilename() == _stFilename)
49         {
50             return true;
51         }
52     }
53     return false;
54 }
55 
getFile(int _iID)56 types::File* FileManager::getFile(int _iID)
57 {
58     if (_iID == -1 && m_iCurrentFile == -1)
59     {
60         return NULL;
61     }
62 
63     if (_iID == -1 && m_iCurrentFile != -1)
64     {
65         return m_fileList[m_iCurrentFile];
66     }
67 
68     if (_iID < static_cast<int>(m_fileList.size()))
69     {
70         return m_fileList[_iID];
71     }
72 
73     return NULL;
74 }
75 
addFile(types::File * _file)76 int FileManager::addFile(types::File* _file)
77 {
78     //if already opened, return previous ID
79     //if(isOpened(_file->getFilename()) == true)
80     //{
81     //    int iFile = getFileID(_file->getFilename());
82     //        _file->getReal()[0] = iFile;
83     //    return iFile;
84     //}
85 
86     //find first free space
87     for (int i = 0 ; i < static_cast<int>(m_fileList.size()); i++)
88     {
89         if (m_fileList[i] == NULL)
90         {
91             m_fileList[i] = _file;
92             m_iCurrentFile = i;
93             return i;
94         }
95     }
96 
97     //no free space, add at the end
98     int iNewId = static_cast<int>(m_fileList.size());
99     m_fileList.push_back(_file);
100     m_iCurrentFile = iNewId;
101     return iNewId;
102 }
103 
getFirstFreeFileID()104 int FileManager::getFirstFreeFileID()
105 {
106     //find first free space
107     for (int i = 0 ; i < static_cast<int>(m_fileList.size()); i++)
108     {
109         if (m_fileList[i] == NULL)
110         {
111             m_iCurrentFile = i;
112             return i;
113         }
114     }
115 
116     //no free space, add at the end
117     int iNewId = static_cast<int>(m_fileList.size());
118     m_fileList.push_back(NULL);
119     m_iCurrentFile = iNewId;
120     return iNewId;
121 }
122 
deleteFile(int _iID)123 void FileManager::deleteFile(int _iID)
124 {
125     if (0 < _iID && _iID < static_cast<int>(m_fileList.size()))
126     {
127         delete m_fileList[_iID];
128         m_fileList[_iID] = NULL;
129         if (m_iCurrentFile == _iID)
130         {
131             m_iCurrentFile = -1;
132         }
133     }
134 
135     //to clean end of list and remove empty spaces
136     while (m_fileList.size() != 0 && m_fileList.back() == NULL)
137     {
138         m_fileList.pop_back();
139     }
140 }
141 
getCurrentFile()142 int FileManager::getCurrentFile()
143 {
144     return m_iCurrentFile;
145 }
146 
getIDs()147 int* FileManager::getIDs()
148 {
149     int iFileIndex  = 0;
150     int* piIds       = NULL;
151 
152     piIds = new int[getOpenedCount()];
153     for (int i = 0 ; i < static_cast<int>(m_fileList.size()); i++)
154     {
155         if (m_fileList[i] != NULL)
156         {
157             piIds[iFileIndex++] = i;
158         }
159     }
160 
161     return piIds;
162 }
163 
getOpenedCount()164 int FileManager::getOpenedCount()
165 {
166     int iCount = 0;
167     for (int i = 0 ; i < static_cast<int>(m_fileList.size()); i++)
168     {
169         if (m_fileList[i] != NULL)
170         {
171             iCount++;
172         }
173     }
174     return iCount;
175 }
176 
getTypesAsString()177 wchar_t** FileManager::getTypesAsString()
178 {
179     int iFileIndex      = 0;
180     wchar_t** pstTypes  = NULL;
181 
182     pstTypes = new wchar_t*[getOpenedCount()];
183     for (int i = 0 ; i < static_cast<int>(m_fileList.size()); i++)
184     {
185         if (m_fileList[i] != NULL)
186         {
187             pstTypes[iFileIndex++] = os_wcsdup(m_fileList[i]->getFileTypeAsString().c_str());
188         }
189     }
190 
191     return pstTypes;
192 }
193 
getFilenames()194 wchar_t** FileManager::getFilenames()
195 {
196     int iFileIndex          = 0;
197     wchar_t** pstFilenames  = NULL;
198 
199     pstFilenames = (wchar_t**) MALLOC(getOpenedCount() * sizeof(wchar_t*));
200     for (int i = 0 ; i < static_cast<int>(m_fileList.size()); i++)
201     {
202         if (m_fileList[i] != NULL)
203         {
204             pstFilenames[iFileIndex++] = os_wcsdup(m_fileList[i]->getFilename().c_str());
205         }
206     }
207 
208     return pstFilenames;
209 }
210 
getModes()211 double* FileManager::getModes()
212 {
213     int iFileIndex      = 0;
214     double* pdblModes   = NULL;
215 
216     pdblModes = new double[getOpenedCount()];
217     for (int i = 0 ; i < static_cast<int>(m_fileList.size()); i++)
218     {
219         if (m_fileList[i] != NULL)
220         {
221             pdblModes[iFileIndex++] = (double)(m_fileList[i]->getFileModeAsInt());
222         }
223     }
224 
225     return pdblModes;
226 }
227 
getSwaps()228 double* FileManager::getSwaps()
229 {
230     int iFileIndex      = 0;
231     double* pdblSwaps   = NULL;
232 
233     pdblSwaps = new double[getOpenedCount()];
234     for (int i = 0 ; i < static_cast<int>(m_fileList.size()); i++)
235     {
236         if (m_fileList[i] != NULL)
237         {
238             pdblSwaps[iFileIndex++] = static_cast<double>(m_fileList[i]->getFileSwap());
239         }
240     }
241 
242     return pdblSwaps;
243 }
244 
initialize()245 void FileManager::initialize()
246 {
247     types::File* pErr = new types::File();
248     pErr->setFileMode(L"wb");
249     pErr->setFileDesc(stderr);
250     pErr->setFileSwap(0);
251     pErr->setFileType(3);
252     pErr->setFilename(L"stderr");
253 
254     types::File* pIn = new types::File();
255     pIn->setFileMode(L"rb");
256     pIn->setFileDesc(stdin);
257     pIn->setFileSwap(0);
258     pIn->setFileType(3);
259     pIn->setFilename(L"stdin");
260 
261     types::File* pOut = new types::File();
262     pOut->setFileMode(L"wb");
263     pOut->setFileDesc(stdout);
264     pOut->setFileSwap(0);
265     pOut->setFileType(3);
266     pOut->setFilename(L"stdout");
267 
268     //put pErr at position 0
269     m_fileList.push_back(pErr);
270 
271     //insert free space
272     m_fileList.push_back(NULL);
273     m_fileList.push_back(NULL);
274     m_fileList.push_back(NULL);
275     m_fileList.push_back(NULL);
276 
277     //put pIn at position 5
278     m_fileList.push_back(pIn);
279     //put pOut at position 6
280     m_fileList.push_back(pOut);
281 }
282 
destroy()283 void FileManager::destroy()
284 {
285     for (int i = 0 ; i < static_cast<int>(m_fileList.size()); i++)
286     {
287         if (m_fileList[i] != NULL)
288         {
289             delete m_fileList[i];
290         }
291     }
292 
293     m_fileList.clear();
294 }
295