libmoldeo (Moldeo 1.0 Core)  1.0
libmoldeo is the group of objects and functions that executes the basic operations of Moldeo 1.0 Platform.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
moFile.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 
3  moFile.cpp
4 
5  ****************************************************************************
6  * *
7  * This source is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This code is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15  * General Public License for more details. *
16  * *
17  * A copy of the GNU General Public License is available on the World *
18  * Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
19  * obtain it by writing to the Free Software Foundation, *
20  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  * *
22  ****************************************************************************
23 
24  Copyright(C) 2006 Fabricio Costa
25 
26  Authors:
27  Fabricio Costa
28  Andrés Colubri
29 
30 *******************************************************************************/
31 
32 #include "moFile.h"
33 #include "moResourceManager.h"
34 
35 #include <boost/filesystem.hpp>
36 namespace bfs=boost::filesystem;
37 
38 #include "moArray.cpp"
40 moDefineDynamicArray(moDirectoryArray)
41 
42 //===========================================
43 //
44 // moDirectory
45 //
46 //===========================================
47 
49  m_pFileManager = NULL;
50  m_CompletePath = moText("");
51  m_bExists = false;
52  m_bRemote = false;
53  m_FileIndex = 0;
54 }
55 
56 moDirectory::moDirectory( moText p_CompletePath, moFileManager* p_pFileManager ) {
57  m_pFileManager = p_pFileManager;
58  m_CompletePath = p_CompletePath;
59  m_bExists = false;
60  m_bRemote = false;
61  m_FileIndex = 0;
63 }
64 
66  Finish();
67 }
68 
69 /*
70 
71 
72  bool find_file( const path & dir_path, // in this directory,
73  const std::string & file_name, // search for this name,
74  path & path_found ) // placing path here if found
75  {
76  if ( !exists( dir_path ) ) return false;
77  directory_iterator end_itr; // default construction yields past-the-end
78  for ( directory_iterator itr( dir_path );
79  itr != end_itr;
80  ++itr )
81  {
82  if ( is_directory(itr->status()) )
83  {
84  if ( find_file( itr->path(), file_name, path_found ) ) return true;
85  }
86  else if ( itr->path().leaf() == file_name ) // see below
87  {
88  path_found = itr->path();
89  return true;
90  }
91  }
92  return false;
93  }
94 
95 
96 
97 */
98 
100 moDirectory::Open( moText p_CompletePath, moText p_Search ) {
101 
102  char *path;
103 
104  m_CompletePath = p_CompletePath;
105  path = m_CompletePath;
106 
107  moText CompletePathSearch = (moText)p_CompletePath + (moText)p_Search;
108 
109  for( int i=0; i<(int)m_Files.Count(); i++) {
110  moFile* pFile = m_Files[i];
111  if (pFile)
112  delete pFile;
113  m_Files[i] = NULL;
114 
115  }
116  m_Files.Empty();
117 
118  m_bExists = false;
119 // void show_files( const path & directory, bool recurse_into_subdirs = true )
120 // {
121  if( bfs::exists( path ) )
122  {
123  m_bExists = true;
124 
125  bfs::directory_iterator end ;
126  for( bfs::directory_iterator iter(path) ; iter != end ; ++iter )
127  if ( bfs::is_directory( *iter ) )
128  {
129  //cout << iter->native_directory_string() << " (directory)\n" ;
130  //if( recurse_into_subdirs ) show_files(*iter) ;
131  } else {
132  //cout << iter->native_file_string() << " (file)\n" ;
133  moText pFileName( iter->path().file_string().c_str() );
134 
135  moText pCompletePathFilename;
136 
137  //pCompletePathFilename = m_CompletePath + moText("/") + moText(pFileName);
138  pCompletePathFilename = pFileName;
139 
140  moFile* pFile = NULL;
141  if (m_pFileManager)
142  pFile = m_pFileManager->GetFile( pCompletePathFilename );
143  else
144  pFile = new moFile( pCompletePathFilename );
145  if (pFile) {
146  m_Files.Add(pFile);
147  }
148 
149  #ifdef _DEBUG
150  MODebug2->Message( moText("moFileManager:: file:") + (moText)pCompletePathFilename);
151  #endif
152  //printf("%-32s %s %9.ld %s",fileInfo.name, attribs, fileInfo.size , timeBuff);
153 
154  }
155 
156  }
157 
158  return m_bExists;
159 // }
160 
161 
162 
163 /*
164  printf(" checking path : %s\n",path);
165  for( int i=0; i<m_Files.Count(); i++) {
166  moFile* pFile = m_Files[i];
167  if (pFile)
168  delete pFile;
169  m_Files[i] = NULL;
170 
171  }
172  m_Files.Empty();
173 
174  if((fPtr = _findfirst(path, &fileInfo )) == -1L) {
175  m_bExists = false;
176  return false;
177  } else {
178  m_bExists = true;
179 
180  m_CompletePath = p_CompletePath;
181  do {
182 
183  moText pFileName( fileInfo.name );
184 
185  moText pCompletePathFilename;
186 
187  pCompletePathFilename = m_CompletePath + moText("/") + moText(pFileName);
188 
189  moFile* pFile = NULL;
190  if (m_pFileManager)
191  pFile = m_pFileManager->GetFile( pCompletePathFilename );
192  else
193  pFile = new moFile( pCompletePathFilename );
194  if (pFile) {
195  m_Files.Add(pFile);
196  }
197  //printf("%-32s %s %9.ld %s",fileInfo.name, attribs, fileInfo.size , timeBuff);
198 
199  } while(_findnext(fPtr, &fileInfo) == 0);
200  _findclose(fPtr);
201  }
202 
203 #else
204  //en linux hay que usar: opendir, readdir, closedir etc...
205  //http://developer.novell.com/wiki/index.php/Programming_to_the_Linux_Filesystem
206 
207  //typedef bool (*file_match_function) ( const string&, const string& );
208 
209  void findMatch( const string& pattern, const string& path, file_match_function match )
210 {
211  DIR* d = opendir( path.c_str() );
212  static struct dirent* dirp;
213  vector<string> dirs;
214  if (! d)
215  {
216  cerr << "Unable to open directory " << path << endl;
217  cerr << "Error is " << strerror(errno) << "(" << errno << ")" << endl;
218  return;
219  }
220  while ( (dirp = readdir(d)) != NULL )
221  {
222  if ( 0 == strcmp( ".", dirp->d_name ) ||
223  0 == strcmp( "..",dirp->d_name ) ) continue; // skip . and ..
224  string candidate( path + "/" + dirp->d_name );
225  try {
226  if ( LinuxFileInfo::isRegularFile(candidate) )
227  {
228  if ( LinuxFileInfo::canRead(candidate) && match(pattern, candidate) )
229  {
230  cout << candidate << endl;
231  }
232  }
233  else if ( LinuxFileInfo::isDirectory(candidate) )
234  {
235  dirs.push_back( candidate );
236  }
237  }
238  catch ( const invalid_argument& ia )
239  {
240  cerr << "Caught exception on path " << candidate << ": " << ia.what() << endl;
241  }
242  }
243  closedir(d);
244  for ( vector<string>::const_iterator ci=dirs.begin(); ci!=dirs.end(); ++ci )
245  {
246  findMatch( pattern, *ci, match );
247  }
248 
249 #endif
250 */
251 }
252 
253 
254 MOboolean
256  return true;
257 }
258 
259 MOboolean
260 moDirectory::Init( moFileManager *p_FileManager ) {
261  m_pFileManager = p_FileManager;
262  m_FileIndex = 0;
263  return true;
264 }
265 
266 MOboolean
268 
269  for( int i=0; i<(int)m_Files.Count(); i++) {
270  moFile* pFile = m_Files[i];
271  if (pFile)
272  delete pFile;
273  m_Files[i] = NULL;
274 
275  }
276  m_Files.Empty();
277  return true;
278 }
279 
280 MOboolean
282  m_bExists = bfs::exists((char*)m_CompletePath);
283  return m_bExists;
284 }
285 
286 MOboolean
288  return m_bRemote;
289 }
290 
291 moText
293  return m_CompletePath;
294 
295 }
296 
299  return m_DirType;
300 }
301 
302 moText
304  return m_Protocol;
305 }
306 
307 
310  return m_DirStatus;
311 }
312 
313 moFile*
315  if (m_bExists) {
316  if (m_Files.Count()>0) {
317  m_FileIndex = 0;
318  return m_Files[0];
319  }
320  }
321  return NULL;
322 }
323 
324 moFile*
326  if (m_bExists) {
327  if (m_Files.Count()>0) {
328  if ( ((MOuint)m_FileIndex+1) < m_Files.Count() ) {
329  m_FileIndex++;
330  return m_Files[m_FileIndex];
331  } else {
332  m_FileIndex--;
333  return NULL;
334  }
335  }
336  }
337  return NULL;
338 }
339 
340 moFile*
342  if (m_bExists) {
343  if (m_Files.Count()>0) {
344  m_FileIndex = m_Files.Count() - 1;
345  return m_Files[m_FileIndex];
346  }
347  }
348  return NULL;
349 }
350 
351 moFile*
353  if (m_bExists) {
354  if (0<=index && index<(MOint)m_Files.Count() ) {
355  m_FileIndex = index;
356  return m_Files[m_FileIndex];
357  }
358  }
359  return NULL;
360 }
361 
362 moFile*
364  if (m_bExists) {
365  for(m_FileIndex=0; m_FileIndex<(MOint)m_Files.Count(); m_FileIndex++) {
366  if (m_Files[m_FileIndex]->GetFileName()==filename)
367  return m_Files[m_FileIndex];
368  }
369  }
370  return NULL;
371 }
372 
373 void
375  //----
376 }
377 
378 
379 
380 moFileArray&
382  return m_Files;
383 }
384 
385 //===========================================
386 //
387 // moFile
388 //
389 //===========================================
390 
392 
393  m_Path = moText("");
394  m_FileName = moText("");
395  m_CompletePath = moText("");
396  m_Extension = moText("");
397  m_Protocol =moText( "");
398 
399  m_bRemote = false;
400  m_pBucketsPool = NULL;
401  m_pData = NULL;
402 }
403 
404 moFile::moFile( moText p_CompletePath ) {//could be: http://.... or ftp://... or c:\... or ...
405 
406  SetCompletePath( p_CompletePath );
407 }
408 
410  Finish();
411 }
412 
413 MOboolean
415  //check file protocol and existance
416  //...todo
417 
418  //create pool for downloading or streaming
419  if (m_bRemote && !m_pBucketsPool) {
420  m_pBucketsPool = new moBucketsPool();
421  //then start downloading...
422  }
423  return (m_pBucketsPool!=NULL);
424 }
425 
426 MOboolean
428  if (m_bRemote && m_pBucketsPool) {
429  delete m_pBucketsPool;
430  m_pBucketsPool = NULL;
431  }
432  return (m_pBucketsPool==NULL);
433 }
434 
435 MOboolean
437  m_bExists = bfs::exists((char*)m_CompletePath);
438  return m_bExists;
439 }
440 
443  return m_FileType;
444 }
445 
446 moText
448  return m_Protocol;
449 }
450 
451 
454  return m_FileStatus;
455 }
456 
457 MOboolean
459  return m_bRemote;
460 }
461 
462 MOubyte*
464  if (m_pData && m_FileStatus==MO_FILESTATUS_READY) {
465  return m_pData;
466  }
467  return NULL;
468 }
469 
470 void
472  m_FileType = p_filetype;
473 }
474 
475 
476 moText
478  return m_FileName;
479 }
480 
481 void
482 moFile::SetCompletePath( moText p_completepath ) {
483 
484  //check if http. ftp or other...set remote
485  m_CompletePath = p_completepath;
486 
487  moText Left7 = m_CompletePath;
488  Left7.Left(7);
489  moText Left6 = m_CompletePath;
490  Left6.Left(6);
491 
492  if (Left7==moText("http://") ||
493  Left7==moText("HTTP://")) {
494  m_Protocol = moText("http://");
495  m_FileType = MO_FILETYPE_HTTP;
496  m_bRemote = true;
497  m_bExists = false;
498  } else if (Left6==moText("ftp://") ||
499  Left6==moText("FTP://")) {
500  m_Protocol = moText("ftp://");
501  m_FileType = MO_FILETYPE_FTP;
502  m_bRemote = true;
503  m_bExists = false;
504  } else {
505  moTextArray FileNameA;
506  m_Protocol = moText("file:///");
507  //moText m_Drive = m_CompletePath.Scan(":");
508 
509  std::string str;
510  str = bfs::extension( (char*)m_CompletePath );
511 
512  m_Extension = str.c_str();
513 
514  m_Dirs = m_CompletePath.Explode(moText("\\/"));
515 
516  if ( m_Dirs.Count() > 0 ) {
517  m_Drive = m_Dirs[0];
518  m_FileName = m_Dirs[m_Dirs.Count()-1];
519  FileNameA = m_FileName.Explode(moText("."));
520  m_FileName = FileNameA[0];
521  }
522 
523  m_FileType = MO_FILETYPE_LOCAL;
524  m_bRemote = false;
525 
526  m_bExists = bfs::exists((char*)m_CompletePath);
527 
528  }
529 
530 }
531 
532 moText
534  return m_Path;
535 }
536 
537 moText
539  return m_CompletePath;
540 }
541 
542 moText
544  return m_Extension;
545 }
546 
547 void
549  //----
550 }
moFile * FindLast()
Definition: moFile.cpp:341
Manejador de archivos.
virtual MOboolean Finish()
Finaliza el objeto, libera recursos.
Definition: moFile.cpp:267
moFile * FindFirst()
Definition: moFile.cpp:314
moFile * GetFile(moText p_FileName)
moFileType GetType()
Definition: moFile.cpp:442
moText m_Protocol
moFileStatus GetStatus()
Definition: moFile.cpp:309
moText GetCompletePath()
Definition: moFile.cpp:292
void Update()
Definition: moFile.cpp:548
moFile * FindNext()
Definition: moFile.cpp:325
void Update()
Definition: moFile.cpp:374
moTextArray Explode(char *separator) const
Definition: moText.cpp:655
virtual MOboolean Init()
Inicializa el objeto.
Definition: moFile.cpp:414
#define MOboolean
Definition: moTypes.h:385
MOboolean m_bExists
MOboolean m_bRemote
moText0 & Left(MOuint)
Definition: moText.cpp:484
moFileStatus m_DirStatus
void SetType(moFileType p_filetype)
Definition: moFile.cpp:471
virtual MOboolean Init()
Inicializa el objeto.
Definition: moFile.cpp:255
MOboolean IsRemote()
Definition: moFile.cpp:458
moFile * Find(MOint index)
Definition: moFile.cpp:352
moText GetExtension()
Get absolute path and filename "/D/PP/myFileName.txt".
Definition: moFile.cpp:543
moFileArray m_Files
MOboolean Exists()
Definition: moFile.cpp:281
void SetCompletePath(moText p_completepath)
Definition: moFile.cpp:482
MOboolean Exists()
Definition: moFile.cpp:436
moText GetFileName()
Definition: moFile.cpp:477
MOint m_FileIndex
clase de para manejar textos
Definition: moText.h:75
moFileType GetType()
Definition: moFile.cpp:298
moText0 moText
Definition: moText.h:291
moText GetPath()
Retreive full file name: return "myFileName.txt", extension is included.
Definition: moFile.cpp:533
moText m_CompletePath
#define MOint
Definition: moTypes.h:388
moFileArray & GetFiles()
Definition: moFile.cpp:381
moFileManager * m_pFileManager
virtual MOboolean Finish()
Finaliza el objeto, libera recursos.
Definition: moFile.cpp:427
Administrador de moBucket 's.
Definition: moBuckets.h:152
MOboolean IsRemote()
Definition: moFile.cpp:287
moText GetCompletePath()
Get inmediate folder name: return "PP" for "PP/myFileName.txt".
Definition: moFile.cpp:538
moFile()
Definition: moFile.cpp:391
virtual ~moDirectory()
Definition: moFile.cpp:65
MOubyte * GetData()
Definition: moFile.cpp:463
static moDebug * MODebug2
Clase de impresión de errores para depuración
Definition: moAbstract.h:225
virtual ~moFile()
Definition: moFile.cpp:409
moFileStatus GetStatus()
Definition: moFile.cpp:453
#define MOuint
Definition: moTypes.h:387
moText GetProtocol()
Get extension ".txt".
Definition: moFile.cpp:447
moFileStatus
Definition: moFileManager.h:82
void Message(moText p_text)
Anuncia un mensaje al usuario además de guardarlo en el log de texto.
Definition: moAbstract.cpp:114
moFileType m_DirType
#define MOubyte
Definition: moTypes.h:399
MOboolean Open(moText p_CompletePath, moText p_Search="/*.*")
Definition: moFile.cpp:100
moFileType
Definition: moFileManager.h:46
moText GetProtocol()
Definition: moFile.cpp:303
moDefineDynamicArray(moFileArray) moDefineDynamicArray(moDirectoryArray) moDirectory
Definition: moFile.cpp:39