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
moFileManager.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 
3  moFileManager.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 
29 
30 *******************************************************************************/
31 #include "moTypes.h"
32 #include "moDebugManager.h"
33 
34 #include <boost/version.hpp>
35 #include <boost/filesystem.hpp>
36 
37 #include "moFileManager.h"
38 
39 #include "moArray.h"
40 moDefineDynamicArray(moFileArray)
41 moDefineDynamicArray(moDirectoryArray)
42 
43 
44 #include <set>
45 
46 namespace bfs=boost::filesystem;
47 
48 //===========================================
49 //
50 // moDirectory
51 //
52 //===========================================
53 
55  m_pFileManager = NULL;
56  m_CompletePath = moText("");
57  m_bExists = false;
58  m_bRemote = false;
59  m_FileIndex = 0;
60 }
61 
62 moDirectory::moDirectory( moText p_CompletePath, moFileManager* p_pFileManager ) {
63  m_pFileManager = p_pFileManager;
64  m_CompletePath = p_CompletePath;
65  m_bExists = false;
66  m_bRemote = false;
67  m_FileIndex = 0;
69 }
70 
72  Finish();
73 }
74 
75 /*
76 
77 
78  bool find_file( const path & dir_path, // in this directory,
79  const std::string & file_name, // search for this name,
80  path & path_found ) // placing path here if found
81  {
82  if ( !exists( dir_path ) ) return false;
83  directory_iterator end_itr; // default construction yields past-the-end
84  for ( directory_iterator itr( dir_path );
85  itr != end_itr;
86  ++itr )
87  {
88  if ( is_directory(itr->status()) )
89  {
90  if ( find_file( itr->path(), file_name, path_found ) ) return true;
91  }
92  else if ( itr->path().leaf() == file_name ) // see below
93  {
94  path_found = itr->path();
95  return true;
96  }
97  }
98  return false;
99  }
100 
101 
102 
103 */
104 
105 
106 MOboolean
107 moDirectory::Open( moText p_CompletePath, moText p_Search ) {
108 
109  char *path;
110  string stdFileName;
111  string stdCompleteFileName;
112  set<string> stdListOfFileNames;
113  set<string> stdListOfCompleteFileNames;
114 
115  m_CompletePath = p_CompletePath;
116  path = m_CompletePath;
117 
118  //m_CompletePath = "\\\\\\";
119 
121 
122  if (m_DirNameArray.Count()>0) {
123 
124  m_DirName = m_DirNameArray[ m_DirNameArray.Count() - 1 ];
125 
126  } else {
128  }
129 
130 
131  moText CompletePathSearch = (moText)p_CompletePath + (moText)p_Search;
132 
134  for( int i=0; i<(int)m_Files.Count(); i++) {
135  moFile* pFile = m_Files[i];
136  if (pFile)
137  delete pFile;
138  m_Files[i] = NULL;
139 
140  }
141  m_Files.Empty();
142 
144  for( int i=0; i<(int)m_SubDirs.Count(); i++) {
145  moDirectory* pDir = m_SubDirs[i];
146  if (pDir)
147  delete pDir;
148  m_SubDirs[i] = NULL;
149 
150  }
151  m_SubDirs.Empty();
152 
154  m_bExists = false;
155 // void show_files( const path & directory, bool recurse_into_subdirs = true )
156 // {
158  if( bfs::exists( path ) )
159  {
160  m_bExists = true;
161 
162  bfs::directory_iterator end ;
163  for( bfs::directory_iterator iter(path) ; iter != end ; ++iter )
164  if ( bfs::is_directory( *iter ) )
165  {
166  //cout << iter->native_directory_string() << " (directory)\n" ;
167  //if( recurse_into_subdirs ) show_files(*iter) ;
168  #if BOOST_VERSION > 103500
169 
170  #if BOOST_VERSION < 104800
171  moText pSubDirName( iter->path().filename().c_str() );
172  #else
173  moText pSubDirName( iter->path().filename().string().c_str() );
174  #endif
175  #else
176  moText pSubDirName( iter->path().leaf().c_str() );
177  #endif
178 
179  moText pCompletePathSubdirName( iter->path().string().c_str() );
180 
181  if (pSubDirName.Left(1) != "." ) {
182  moDirectory* pSubdir = new moDirectory( pCompletePathSubdirName );
183  if (pSubdir)
184  m_SubDirs.Add( pSubdir );
185  }
186 
187  } else {
188  //cout << iter->native_file_string() << " (file)\n" ;
189  //ATENCION SEGUN LA VERSION DE BOOST hya que usar filename() o leaf()
190  //moText pFileName( iter->path().leaf().c_str() );
191 
192 
193  #if BOOST_VERSION > 103500
194 
195 
196  #if BOOST_VERSION < 104800
197  moText pFileName( iter->path().filename().c_str() );
198  stdFileName = iter->path().filename().c_str();
199  #else
200  moText pFileName( iter->path().filename().string().c_str() );
201  stdFileName = iter->path().filename().string().c_str();
202  #endif
203 
204  #else
205 
206  moText pFileName( iter->path().leaf().c_str() );
207 
208  stdFileName = iter->path().leaf().c_str();
209 
210  #endif
211 
212  moText pCompletePathFilename( iter->path().string().c_str() );
213  stdCompleteFileName = iter->path().string().c_str();
214 
215 
216 
217  if (stdFileName!="Thumbs.db") {
218  stdListOfFileNames.insert(stdFileName);
219  stdListOfCompleteFileNames.insert(stdCompleteFileName);
220  }
221 
222  #ifdef _DEBUG
223  //MODebug2->Message( moText("moFileManager:: file:") + (moText)pCompletePathFilename);
224  //MODebug2->Message( moText("moFileManager:: filesize:") + IntToStr((int) bfs::file_size( iter->path().file_string().c_str() )));
225  #endif
226  //printf("%-32s %s %9.ld %s",fileInfo.name, attribs, fileInfo.size , timeBuff);
227 
228  }
229 
230  }
231 
233  moText pCompletePathFilename;
234 
235  for(std::set<string>::iterator Name = stdListOfCompleteFileNames.begin() ; Name != stdListOfCompleteFileNames.end() ; ++Name)
236  {
237  //cout << *Name << endl;
238  string comp = *Name;
239  pCompletePathFilename = moText((char*)comp.c_str());
240 
241  moFile* pFile = NULL;
242  if (m_pFileManager)
243  pFile = m_pFileManager->GetFile( pCompletePathFilename );
244  else
245  pFile = new moFile( pCompletePathFilename );
246 
247  if (pFile) m_Files.Add(pFile);
248  }
249 
250  return m_bExists;
251 // }
252 
253 
254 
255 /*
256  printf(" checking path : %s\n",path);
257  for( int i=0; i<m_Files.Count(); i++) {
258  moFile* pFile = m_Files[i];
259  if (pFile)
260  delete pFile;
261  m_Files[i] = NULL;
262 
263  }
264  m_Files.Empty();
265 
266  if((fPtr = _findfirst(path, &fileInfo )) == -1L) {
267  m_bExists = false;
268  return false;
269  } else {
270  m_bExists = true;
271 
272  m_CompletePath = p_CompletePath;
273  do {
274 
275  moText pFileName( fileInfo.name );
276 
277  moText pCompletePathFilename;
278 
279  pCompletePathFilename = m_CompletePath + moText("/") + moText(pFileName);
280 
281  moFile* pFile = NULL;
282  if (m_pFileManager)
283  pFile = m_pFileManager->GetFile( pCompletePathFilename );
284  else
285  pFile = new moFile( pCompletePathFilename );
286  if (pFile) {
287  m_Files.Add(pFile);
288  }
289  //printf("%-32s %s %9.ld %s",fileInfo.name, attribs, fileInfo.size , timeBuff);
290 
291  } while(_findnext(fPtr, &fileInfo) == 0);
292  _findclose(fPtr);
293  }
294 
295 #else
296  //en linux hay que usar: opendir, readdir, closedir etc...
297  //http://developer.novell.com/wiki/index.php/Programming_to_the_Linux_Filesystem
298 
299  //typedef bool (*file_match_function) ( const string&, const string& );
300 
301  void findMatch( const string& pattern, const string& path, file_match_function match )
302 {
303  DIR* d = opendir( path.c_str() );
304  static struct dirent* dirp;
305  vector<string> dirs;
306  if (! d)
307  {
308  cerr << "Unable to open directory " << path << endl;
309  cerr << "Error is " << strerror(errno) << "(" << errno << ")" << endl;
310  return;
311  }
312  while ( (dirp = readdir(d)) != NULL )
313  {
314  if ( 0 == strcmp( ".", dirp->d_name ) ||
315  0 == strcmp( "..",dirp->d_name ) ) continue; // skip . and ..
316  string candidate( path + "/" + dirp->d_name );
317  try {
318  if ( LinuxFileInfo::isRegularFile(candidate) )
319  {
320  if ( LinuxFileInfo::canRead(candidate) && match(pattern, candidate) )
321  {
322  cout << candidate << endl;
323  }
324  }
325  else if ( LinuxFileInfo::isDirectory(candidate) )
326  {
327  dirs.push_back( candidate );
328  }
329  }
330  catch ( const invalid_argument& ia )
331  {
332  cerr << "Caught exception on path " << candidate << ": " << ia.what() << endl;
333  }
334  }
335  closedir(d);
336  for ( vector<string>::const_iterator ci=dirs.begin(); ci!=dirs.end(); ++ci )
337  {
338  findMatch( pattern, *ci, match );
339  }
340 
341 #endif
342 */
343 }
344 
345 
346 MOboolean
348  return true;
349 }
350 
351 MOboolean
352 moDirectory::Init( moFileManager *p_FileManager ) {
353  m_pFileManager = p_FileManager;
354  m_FileIndex = 0;
355  return true;
356 }
357 
358 MOboolean
360 
361  for( int i=0; i<(int)m_Files.Count(); i++) {
362  moFile* pFile = m_Files[i];
363  if (pFile)
364  delete pFile;
365  m_Files[i] = NULL;
366 
367  }
368  m_Files.Empty();
369 
371  for( int i=0; i<(int)m_SubDirs.Count(); i++) {
372  moDirectory* pDir = m_SubDirs[i];
373  if (pDir)
374  delete pDir;
375  m_SubDirs[i] = NULL;
376 
377  }
378  m_SubDirs.Empty();
379 
380  return true;
381 }
382 
383 MOboolean
385  m_bExists = bfs::exists((char*)m_CompletePath);
386  return m_bExists;
387 }
388 
389 MOboolean
391  return m_SubDirs.Count();
392 }
393 
394 MOboolean
396  return m_bRemote;
397 }
398 
399 moText
401  return m_CompletePath;
402 
403 }
404 
405 moText
407  return m_DirName;
408 }
409 
412  return m_DirType;
413 }
414 
415 moText
417  return m_Protocol;
418 }
419 
420 
423  return m_DirStatus;
424 }
425 
426 moFile*
428  if (m_bExists) {
429  if (m_Files.Count()>0) {
430  m_FileIndex = 0;
431  return m_Files[0];
432  }
433  }
434  return NULL;
435 }
436 
437 moFile*
439  if (m_bExists) {
440  if (m_Files.Count()>0) {
441  if ( ((MOuint)m_FileIndex+1) < m_Files.Count() ) {
442  m_FileIndex++;
443  return m_Files[m_FileIndex];
444  } else {
445  m_FileIndex--;
446  return NULL;
447  }
448  }
449  }
450  return NULL;
451 }
452 
453 moFile*
455  if (m_bExists) {
456  if (m_Files.Count()>0) {
457  m_FileIndex = m_Files.Count() - 1;
458  return m_Files[m_FileIndex];
459  }
460  }
461  return NULL;
462 }
463 
464 moFile*
465 moDirectory::Find( MOint index ) {
466  if (m_bExists) {
467  if (0<=index && index<(MOint)m_Files.Count() ) {
468  m_FileIndex = index;
469  return m_Files[m_FileIndex];
470  }
471  }
472  return NULL;
473 }
474 
475 moFile*
476 moDirectory::Find( moText filename) {
477  if (m_bExists) {
478  for(m_FileIndex=0; m_FileIndex<(MOint)m_Files.Count(); m_FileIndex++) {
479  if (m_Files[m_FileIndex]->GetFileName()==filename)
480  return m_Files[m_FileIndex];
481  }
482  }
483  return NULL;
484 }
485 
486 void
488 
489  char *path;
490 
491  path = m_CompletePath;
492 
493 // void show_files( const path & directory, bool recurse_into_subdirs = true )
494 // {
496  if( bfs::exists( path ) )
497  {
498  m_bExists = true;
499 
500  bfs::directory_iterator end ;
501  for( bfs::directory_iterator iter(path) ; iter != end ; ++iter )
502  if ( bfs::is_directory( *iter ) )
503  {
504  //cout << iter->native_directory_string() << " (directory)\n" ;
505  //if( recurse_into_subdirs ) show_files(*iter) ;
506  } else {
507  //cout << iter->native_file_string() << " (file)\n" ;
508 
509  //ATENCION SEGUN LA VERSION DE BOOST hya que usar filename() o leaf()
510 
511  #if BOOST_VERSION < 104300
512  moText pFileName( iter->path().leaf().c_str() );
513  moText pCompletePathFilename( iter->path().string().c_str() );
514  #else
515  moText pFileName( iter->path().filename().string().c_str() );
516  moText pCompletePathFilename( iter->path().string().c_str() );
517  #endif
518 
519 
520 
521 
522  moFile* pFile = NULL;
523 
524  if (pFileName!=moText("Thumbs.db")) {
525 
526  bool founded = false;
527 
528  for( int i=0; i<(int)m_Files.Count(); i++) {
529  pFile = m_Files[i];
530  if (pFile->GetCompletePath()==pCompletePathFilename) {
531  founded = true;
532  }
533  }
534  if (!founded) {
535  if (m_pFileManager) {
536  pFile = m_pFileManager->GetFile( pCompletePathFilename );
537  } else {
538  pFile = new moFile( pCompletePathFilename );
539  }
540  if (pFile) {
541  MODebug2->Message( moText("moFileManager::moDirectory::Update file added:") + (moText)pCompletePathFilename);
542  m_Files.Add(pFile);
543  }
544  }
545 
546  }
547 
548 
549  //#ifdef _DEBUG
550  //MODebug2->Message( moText("moFileManager:: moDirectory::Update:") + (moText)pCompletePathFilename);
551  //#endif
552  //printf("%-32s %s %9.ld %s",fileInfo.name, attribs, fileInfo.size , timeBuff);
553 
554  }
555 
556  }
557 
558  return;
559 
560 }
561 
562 
563 
564 moFileArray&
566  return m_Files;
567 }
568 
569 moDirectoryArray&
571  return m_SubDirs;
572 }
573 
574 //===========================================
575 //
576 // moFile
577 //
578 //===========================================
579 
580 moFile::moFile() {
581 
582  m_Path = moText("");
583  m_FileName = moText("");
584  m_CompletePath = moText("");
585  m_Extension = moText("");
586  m_Protocol =moText( "");
587 
588  m_bRemote = false;
589  m_pBucketsPool = NULL;
590  m_pData = NULL;
591 }
592 
593 moFile::moFile( moText p_CompletePath ) {//could be: http://.... or ftp://... or c:\... or ...
594 
595  SetCompletePath( p_CompletePath );
596 
597 }
598 
599 moFile::~moFile() {
600  Finish();
601 }
602 
603 MOboolean
604 moFile::Init() {
605  //check file protocol and existance
606  //...todo
607 
608  //create pool for downloading or streaming
609  if (m_bRemote && !m_pBucketsPool) {
610  m_pBucketsPool = new moBucketsPool();
611  //then start downloading...
612  }
613  return (m_pBucketsPool!=NULL);
614 }
615 
616 MOboolean
617 moFile::Finish() {
618  if (m_bRemote && m_pBucketsPool) {
619  delete m_pBucketsPool;
620  m_pBucketsPool = NULL;
621  }
622  return (m_pBucketsPool==NULL);
623 }
624 
625 MOboolean
626 moFile::Exists() {
627  try {
628  m_bExists = bfs::exists((char*)m_CompletePath);
629  } catch( const bfs::filesystem_error& e ) {
630  moDebugManager::Error("moFile::Exists > m_CompletePath:" + m_CompletePath + " failed.");
631  m_bExists = false;
632  }
633  return m_bExists;
634 }
635 
637 moFile::GetType() {
638  return m_FileType;
639 }
640 
641 moText
643  return m_Protocol;
644 }
645 
646 
649  return m_FileStatus;
650 }
651 
652 MOboolean
654  return m_bRemote;
655 }
656 
658  return m_FileDate;
659 }
660 
662  return m_FileSize;
663 }
664 
665 MOubyte*
666 moFile::GetData() {
667  if (m_pData && m_FileStatus==MO_FILESTATUS_READY) {
668  return m_pData;
669  }
670  return NULL;
671 }
672 
673 void
674 moFile::SetType( moFileType p_filetype ) {
675  m_FileType = p_filetype;
676 }
677 
678 
679 moText
681  return m_FileName;
682 }
683 
684 moText
686  return ( m_FileName + m_Extension );
687 }
688 
689 void
690 moFile::SetCompletePath( moText p_completepath ) {
691 
692  //check if http. ftp or other...set remote
693  m_CompletePath = p_completepath;
694 
695  moText Left8 = m_CompletePath;
696  Left8.Left(8);
697  moText Left7 = m_CompletePath;
698  Left7.Left(7);
699  moText Left6 = m_CompletePath;
700  Left6.Left(6);
701 
702  if (Left7==moText("http://") ||
703  Left7==moText("HTTP://")) {
704  m_Protocol = moText("http://");
705  m_FileType = MO_FILETYPE_HTTP;
706  m_bRemote = true;
707  m_bExists = false;
708  } else if (Left8==moText("https://") ||
709  Left8==moText("HTTPS://")) {
710  m_Protocol = moText("https://");
711  m_FileType = MO_FILETYPE_HTTPS;
712  m_bRemote = true;
713  m_bExists = false;
714  } else if (Left6==moText("ftp://") ||
715  Left6==moText("FTP://")) {
716  m_Protocol = moText("ftp://");
717  m_FileType = MO_FILETYPE_FTP;
718  m_bRemote = true;
719  m_bExists = false;
720  } else {
721  moTextArray FileNameA;
722  m_Protocol = moText("file:///");
723  //moText m_Drive = m_CompletePath.Scan(":");
724 
725  std::string str;
726 
727  str = bfs::extension( (char*)m_CompletePath );
728 
729 
730  m_Extension = str.c_str();
731  m_Path = "";
732  moText pathStart = m_CompletePath;
733  pathStart = pathStart.SubText(0,0);
734  //cout << "pathStart:" << pathStart << " slash:" << moSlash << endl;
735 
736  m_Dirs = m_CompletePath.Explode(moText("\\/"));
737  #ifndef MO_WIN32
738  if (m_CompletePath.Length()>0)
739  if ( pathStart == moSlash )
740  m_Path = moSlash;
741  #endif
742 
743  if ( m_Dirs.Count() > 0 ) {
744  m_Drive = m_Dirs[0];
745  m_FileName = m_Dirs[m_Dirs.Count()-1];
746  FileNameA = m_FileName.Explode(moText("."));
747  m_FileName = FileNameA[0];
748  m_Dirs.Remove(m_Dirs.Count()-1);
749  }
750 
751  for( MOuint d=0; d < m_Dirs.Count(); d++ ) {
752  if (m_Dirs[d]!="" && m_Dirs[d]!="/" && m_Dirs[d]!=".")// && m_Dirs[d]!="..")
753  m_Path+= m_Dirs[d] + moSlash;
754 
755  }
756 
757  m_FileType = MO_FILETYPE_LOCAL;
758  m_bRemote = false;
759 
760  m_CompletePath = m_Path + m_FileName + m_Extension;
761 
762  m_bExists = bfs::exists((char*)m_CompletePath);
763 
764  char *path;
765  path = m_CompletePath;
766  try {
767  if ( Exists() && !bfs::is_directory(path)) m_FileSize = (long) bfs::file_size( path );
768  else m_FileSize = 0;
769  } catch( const bfs::filesystem_error& e ) {
770  moDebugManager::Error("moFile::moFile > error: " + moText( e.what()) );
771  m_FileSize = 0;
772  }
773  }
774 
775 }
776 
777 moText
778 moFile::GetPath() {
779  return m_Path;
780 }
781 
782 moText
784  return m_CompletePath;
785 }
786 
787 moText
789  char *path;
790  moText absolutePath = m_CompletePath;
791 
792  if (!m_bExists) {
793  return m_CompletePath;
794  }
795 
796  path = m_CompletePath;
797 
798  try {
799 
800  bfs::path abspath = bfs::canonical( path );
801  absolutePath = (char*)abspath.string().c_str();
802 
803  } catch( const bfs::filesystem_error& e ) {
804 
805  moDebugManager::Error( "moFile::GetAbsolutePath failed for " + m_CompletePath + " boost::filesystem error: " + e.what() );
806 
807  }
808 
809  return absolutePath;
810 }
811 
812 
813 
814 moText
816  return m_Extension;
817 }
818 
819 moText
821  if (m_Dirs.Count()>0) {
822  return m_Dirs[m_Dirs.Count()-1];
823  }
824  return moText("");
825 }
826 
827 void
828 moFile::Update() {
829  //----
830 }
831 
832 //===========================================
833 //
834 // moFileManager
835 //
836 //===========================================
837 
839 
842 
843  SetName("filemanager");
844  SetLabelName("filemanager");
845 
846 }
847 
849 
850 }
851 
853  return true;
854 }
855 
856 
858  return true;
859 }
860 
861 
862 MOboolean
863 moFileManager::Load( moText p_FileName, MOboolean bWaitForDownload ) {
864 
865  moFile pFileName(p_FileName);
866  p_FileName = pFileName.GetCompletePath();
867 
868  for(MOuint i = 0; i< m_Files.Count(); i++ ) {
869  if ( m_Files[i]->GetCompletePath() == p_FileName ) {
870  return true;
871  }
872  }
873 
874  moFile* pFile = new moFile( p_FileName );
875 
876  if ( pFile!=NULL ) {
877  if (pFile->GetType()==MO_FILETYPE_LOCAL && !pFile->Exists())
878  return false;
879  if (bWaitForDownload && pFile->IsRemote()) {
880  while(pFile->GetStatus()!=MO_FILESTATUS_READY) {
881  pFile->Update();
882  }
883  }
884  m_Files.Add(pFile);
885  return true;
886  }
887  return false;
888 }
889 
890 moFile*
892  moFile pFileName(p_FileName);
893  p_FileName = pFileName.GetCompletePath();
894 
895  if ( Load(p_FileName) ) {
896  for(MOuint i = 0; i< m_Files.Count(); i++ ) {
897  if ( m_Files[i]->GetCompletePath() == p_FileName ) {
898  return m_Files[i];
899  }
900  }
901  }
902  return NULL;
903 }
904 
905 
906 MOboolean
907 moFileManager::Open( moText p_Path, MOboolean bWaitForDownload ) {
908 
909  for(MOuint i = 0; i< m_Directories.Count(); i++ ) {
910  if ( m_Directories[i]->GetCompletePath() == p_Path ) {
911  return true;
912  }
913  }
914 
915  moDirectory* pDir = new moDirectory( p_Path, this );
916 
917  if ( pDir!=NULL ) {
918  if (pDir->GetType()==MO_FILETYPE_LOCAL && !pDir->Exists()) {
919  delete pDir;
920  return false;
921  }
922  if (bWaitForDownload && pDir->IsRemote()) {
923  while(pDir->GetStatus()!=MO_FILESTATUS_READY) {
924  pDir->Update();
925  }
926  }
927  m_Directories.Add(pDir);
928  return true;
929  }
930  return false;
931 }
932 
935  if ( Open(p_Path) ) {
936  for(MOuint i = 0; i< m_Directories.Count(); i++ ) {
937  if ( m_Directories[i]->GetCompletePath() == p_Path ) {
938  return m_Directories[i];
939  }
940  }
941  }
942  return NULL;
943 }
944 
945 moText
947 
948  #ifdef MO_WIN32
949 
950  char ownPth[MAX_PATH];
951 
952  // Will contain exe path
953  HMODULE hModule = GetModuleHandle(NULL);
954  if (hModule != NULL)
955  {
956  // When passing NULL to GetModuleHandle, it returns handle of exe itself
957  GetModuleFileNameA( hModule, ownPth, (sizeof(ownPth)));
958  }
959  moFile exeFile( ownPth );
960  return exeFile.GetCompletePath();
961 
962  #endif
963  return moText("[ WARNING -- function not implemented ]");
964 
965 }
966 #ifdef MO_USING_VC
967 #include <direct.h>
968 #endif
969 
970 moText
972  char lbuf[1024];
973 
974  #ifdef MO_USING_VC
975  _getcwd( lbuf , sizeof( lbuf ) );
976  #else
977  getcwd( lbuf , sizeof( lbuf ) );
978  #endif
979 
980  moText workpath = lbuf;
981 
982  return workpath;
983 
984 }
985 
986 bool
988 
989  std::string dirname;
990 
991  dirname = Dir.GetCompletePath();
992 
993  bfs::create_directory( dirname );
994 
995  return Dir.Exists();
996 }
997 
998 
999 bool
1001  moFile file( filename );
1002  return file.Exists();
1003 }
1004 
1005 bool
1007  // moDirectory directory( dirname );
1008  //return directory.Exists();
1009  return bfs::exists((char*)dirname);
1010 
1011 }
1012 
1013 
1014 
1015 bool
1017 
1018 /*
1019  ifstream initialFile(FileSrc, ios::in|ios::binary);
1020  ofstream outputFile(FileDst, ios::out|ios::binary);
1021  //defines the size of the buffer
1022  initialFile.seekg(0, ios::end);
1023  long fileSize = initialFile.tellg();
1024  //Requests the buffer of the predefined size
1025 
1026 
1027  //As long as both the input and output files are open...
1028  if(initialFile.is_open() && outputFile.is_open())
1029  {
1030  short * buffer = new short[fileSize/2];
1031  //Determine the file's size
1032  //Then starts from the beginning
1033  initialFile.seekg(0, ios::beg);
1034  //Then read enough of the file to fill the buffer
1035  initialFile.read((char*)buffer, fileSize);
1036  //And then write out all that was read
1037  outputFile.write((char*)buffer, fileSize);
1038  delete[] buffer;
1039  }
1040  //If there were any problems with the copying process, let the user know
1041  else if(!outputFile.is_open())
1042  {
1043  MODebug2->Error( moText("I couldn't open ") + (moText)FileDst + moText(" for copying!"));
1044  return 0;
1045  }
1046  else if(!initialFile.is_open())
1047  {
1048  MODebug2->Error( moText("I couldn't open ") + (moText)FileSrc + moText(" for copying!"));
1049  return 0;
1050  }
1051 
1052  initialFile.close();
1053  outputFile.close();
1054 */
1055  std::string name, new_name;
1056 
1057  name = FileSrc;
1058  new_name = FileDst;
1059 
1060  try {
1061  bfs::copy_file(name, new_name);
1062  }
1063  catch( const std::exception & ex ) {
1064  MODebug2->Error( ex.what() );
1065  }
1066 
1067 
1068  return 1;
1069 
1070 }
1071 
1072 
1073 
1074 bool moFileManager::CopyDirectory( const moText& DirSrc, const moText& DirDst)
1075 {
1076  std::string dsrc, ddst;
1077  dsrc = DirSrc;
1078  ddst = DirDst;
1079  bfs::path source = dsrc;
1080  bfs::path destination = ddst;
1081 
1082  try
1083  {
1084  // Check whether the function call is valid
1085  if(
1086  !bfs::exists(source) ||
1087  !bfs::is_directory(source)
1088  )
1089  {
1090  std::cerr << "Source directory " << source.string()
1091  << " does not exist or is not a directory." << '\n'
1092  ;
1093  return false;
1094  }
1095  if(bfs::exists(destination))
1096  {
1097  std::cerr << "Destination directory " << destination.string()
1098  << " already exists." << '\n'
1099  ;
1100  //return false;
1101  } else
1102  // Create the destination directory
1103  if(!bfs::create_directory(destination))
1104  {
1105  std::cerr << "Unable to create destination directory"
1106  << destination.string() << '\n'
1107  ;
1108  return false;
1109  }
1110  }
1111  catch(bfs::filesystem_error const & e)
1112  {
1113  std::cerr << e.what() << '\n';
1114  return false;
1115  }
1116  // Iterate through the source directory
1117  for(
1118  bfs::directory_iterator file(source);
1119  file != bfs::directory_iterator(); ++file
1120  )
1121  {
1122  try
1123  {
1124  bfs::path current(file->path());
1125  bfs::path destin(destination / current.filename());
1126  moText destinT = (char*)destin.string().c_str();
1127  moText currentT = (char*)current.string().c_str();
1128  if(bfs::is_directory(current))
1129  {
1130  // Found directory: Recursion
1131  if(
1132  !CopyDirectory(
1133  currentT,
1134  destinT
1135  )
1136  )
1137  {
1138  return false;
1139  }
1140  }
1141  else
1142  {
1143  // Found file: Copy
1144  bfs::copy_file(
1145  current,
1146  destin
1147  );
1148  }
1149  }
1150  catch(bfs::filesystem_error const & e)
1151  {
1152  std:: cerr << e.what() << '\n';
1153  }
1154  }
1155  return true;
1156 }
1157 
moFile * FindLast()
Definition: moFile.cpp:341
Manejador de archivos.
virtual MOboolean Finish()
Finaliza el objeto, libera recursos.
Definition: moFile.cpp:267
static bool CopyFile(moText FileSrc, moText FileDst)
moText GetFullName()
Retreive full file name: return "myFileName" for "myFileName.txt".
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
void Error(moText p_text)
Anuncia y registra un error.
Definition: moAbstract.cpp:79
moFileSize GetSize()
void SetName(const moText &p_name)
moFile * FindNext()
Definition: moFile.cpp:325
void Update()
Definition: moFile.cpp:374
moTextArray Explode(char *separator) const
Definition: moText.cpp:655
static bool FileExists(moText filename)
static moText GetWorkPath()
long moFileDate
Definition: moFileManager.h:56
virtual MOboolean Init()
Inicializa el objeto.
Definition: moFile.cpp:414
#define MOboolean
Definition: moTypes.h:385
static void Error(moText p_text)
Anuncia un error.
MOboolean m_bExists
MOboolean m_bRemote
moText0 & Left(MOuint)
Definition: moText.cpp:484
moDirectoryArray & GetSubDirs()
moFileStatus m_DirStatus
void SetResourceType(moResourceType p_restype)
virtual MOboolean Init()
moText GetFolderName()
Get path of file: return "XX/YY/PP" for "XX/YY/PP/myFileName.txt".
void SetType(moFileType p_filetype)
Definition: moFile.cpp:471
virtual MOboolean Init()
Inicializa el objeto.
Definition: moFile.cpp:255
virtual ~moFileManager()
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
static bool CreateDirectory(moDirectory Dir)
moFileArray m_Files
MOboolean Exists()
Definition: moFile.cpp:281
void SetCompletePath(moText p_completepath)
Definition: moFile.cpp:482
static bool CopyDirectory(const moText &DirSrc, const moText &DirDst)
static moText GetExePath()
MOboolean Exists()
Definition: moFile.cpp:436
moText GetFileName()
Definition: moFile.cpp:477
MOint m_FileIndex
clase de para manejar textos
Definition: moText.h:75
long moFileSize
Definition: moFileManager.h:57
Dispositivo de entrada/salida, típicamente, interfaces humanas de IO y datos ( teclado, mouse, tableta, tcp, udp, serial )
Definition: moTypes.h:532
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
moText GetDirName()
virtual MOboolean Finish()
Finaliza el objeto, libera recursos.
Definition: moFile.cpp:427
moFileDate GetDate()
Return http:// | ftp:// | https:// | file:///.
moDefineDynamicArray(moFileArray) moDefineDynamicArray(moDirectoryArray) namespace bfs
Definition: moFile.cpp:39
Administrador de moBucket 's.
Definition: moBuckets.h:152
moFileArray m_Files
moDirectoryArray m_Directories
moDirectory * GetDirectory(moText p_Path)
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
MOuint Length() const
Definition: moText.cpp:347
void SetLabelName(const moText &p_labelname)
virtual ~moDirectory()
Definition: moFile.cpp:65
MOubyte * GetData()
Definition: moFile.cpp:463
moTextArray m_DirNameArray
static moDebug * MODebug2
Clase de impresión de errores para depuración
Definition: moAbstract.h:225
MOboolean Load(moText p_FileName, MOboolean bWaitForDownload=false)
virtual ~moFile()
Definition: moFile.cpp:409
moText m_DirName
moFileStatus GetStatus()
Definition: moFile.cpp:453
MOboolean HasSubdirs()
#define MOuint
Definition: moTypes.h:387
moText GetProtocol()
Get extension ".txt".
Definition: moFile.cpp:447
moFileStatus
Definition: moFileManager.h:82
virtual MOboolean Finish()
MOboolean Open(moText p_Path, MOboolean bWaitForDownload=false)
moText GetAbsolutePath()
Get relative path and filename "PP/myFileName.txt".
moText0 & SubText(MOuint, MOuint)
Definition: moText.cpp:504
void Message(moText p_text)
Anuncia un mensaje al usuario además de guardarlo en el log de texto.
Definition: moAbstract.cpp:114
static bool DirectoryExists(moText dirname)
void SetType(moMoldeoObjectType p_type)
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
moDirectoryArray m_SubDirs