Xmipp  v3.23.11-Nereus
xmipp_filename.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Authors: Carlos Oscar S. Sorzano (coss@cnb.csic.es)
3  *
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program 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 program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 #ifndef CORE_XMIPP_FILENAME_H_
27 #define CORE_XMIPP_FILENAME_H_
28 
29 //#ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 //#endif /* HAVE_UNISTD_H */
32 
33 #include "xmipp_strings.h"
34 
35 #ifndef __MINGW32__
36 #include <fcntl.h>
37 #endif
38 
39 #define FILENAMENUMBERLENGTH 6
40 
41 typedef struct stat Stat;
42 
43 //extern variable with magic Word used to define metadata version
44 // xmipp XMIPP_STAR_1
45 // sqlite SQLite format 3
46 
47 #define METADATA_XMIPP_STAR "# XMIPP_STAR_1"
48 #define METADATA_XMIPP_SQLITE "SQLite format 3"
49 
50 
52 #define AT '@'
53 #define COMMA ','
54 #define COLON ':'
55 #define NUM '#'
56 
58 
65 class FileName: public String
66 {
67 public:
70 
80  inline FileName(): String("")
81  {}
82 
94  FileName(const String& str): String(str)
95  {}
96 
99  FileName(const char* str) : String(str)
100  {}
101 
105  {
106  return (FileName&) std::string::operator=(op);
107  }
108 
111  FileName(const FileName& fn) : String(fn)
112  {}
113 
124  FileName(const char* str, int no, const String& ext = "")
125  {
126  compose(str, no, ext);
127  }
128 
137  FileName(const char* str, const String& ext): String(str + ext)
138  {}
139 
148  FileName(const String& str, const String& ext): String(str + "." + ext)
149  {}
151 
154 
158  inline String getString() const
159  {
160  return (String)(*this);
161  }
162 
165  inline bool isEmpty() const
166  {
167  return *this == "";
168  }
169 
176  void compose(const String& str, const size_t no, const String& ext = "");
177 
184  void compose(const size_t no, const String& str);
185 
186 
187  // Constructor: prefix number, filename root and extension, mainly for selfiles..
194  void compose(size_t no , const String &str , const String &ext);
195 
196  // Constructor: string and filename, mainly for metadata blocks..
203  void compose(const String &blockName , const String &str);
204 
213  void composeBlock(const String &blockName, size_t no, const String &str, const String &ext="");
214 
215 
218  bool isInStack() const;
219 
228  void decompose(size_t &no, String& str) const;
229 
239 
247  FileName getRoot() const;
248 
251  FileName getBaseName() const;
252 
262  int getNumber() const;
263 
273  size_t getPrefixNumber(size_t pos = 0) const;
274 
275  FileName getDir() const;
276 
286  String getExtension() const;
287 
299  String getFileFormat() const;
300 
303  bool hasImageExtension() const;
304 
307  bool hasStackExtension() const;
308 
311  bool hasVolumeExtension() const;
312 
315  bool hasMetadataExtension() const;
316 
319  size_t getFileSize() const;
320 
321 
330  String getBlockName() const;
331 
341 
350  FileName removeBlockName() const;
351 
359  FileName removeAllPrefixes() const;
360 
365  void initRandom(int length);
366 
373  void initUniqueName(const char * templateStr = "xmippTemp_XXXXXX", const String &fnDir="");
375 
377 
378 
385  FileName toLowercase() const;
386 
394  FileName toUppercase() const;
395 
403  bool contains(const String& str) const;
404 
412  FileName beforeFirstOf(const String& str) const;
413 
421  FileName beforeLastOf(const String& str) const;
422 
430  FileName afterFirstOf(const String& str) const;
431 
439  FileName afterLastOf(const String& str) const;
440 
453  FileName addPrefix(const String& prefix) const;
454 
465  FileName addExtension(const String& ext) const;
466 
477  FileName withoutExtension() const;
478 
489  FileName withoutRoot() const;
490 
505  FileName insertBeforeExtension(const String& str) const;
506 
519  FileName removeExtension(const String& ext) const;
520 
522 
523 
528  FileName removeFilename() const;
529 
533 
544  FileName removeFileFormat() const;
545 
550  bool isMetaData(bool failIfNotExists=true) const;
551 
553  bool isStar1(bool failIfNotExists) const;
554 
569  FileName replaceSubstring(const String& subOld, const String& subNew) const;
570 
572  FileName replaceCharacter(char oldChar, char newChar) const;
573 
582  FileName replaceExtension(const String& newExt) const;
583 
590  FileName removeSubstring(const String& sub) const;
591 
598  FileName removeUntilPrefix(const String& prefix) const;
599 
604  FileName removeDirectories(int keep = 0) const;
607  void copyFile(const FileName & target) const;
608 
609  // This function is in funcs, cannot be here because need metadata_generic and metadata_generic
610  // need filename
611  // /* Copy one image
612  // *
613  // */
614  // void copyImage(const FileName & target) const;
615 
624  bool exists() const;
625 
627  void deleteFile() const;
628 
637  bool existsTrim() const;
638 
640  bool isDir() const;
641 
643  void getFiles(std::vector<FileName> &files) const;
644 
652  void waitUntilStableSize(size_t time_step = 250000);
653 
659  void createEmptyFile(size_t size, size_t block_size = 102400);
660 
662  void createEmptyFileWithGivenLength(size_t length = 0) const;
663 
670  int makePath(mode_t mode = 0755) const;
671 
682  void assertExists();
684 };
685 
688 char* getXmippPath();
689 
692 char* getXmippSrcPath();
693 
696 inline bool fileExists( const char *filename )
697 {
698  // do not use stat since updates the atime flag. This may be dangerous
699  // for mpi processes in which the same file is accessed many times
700  // from different computers even if not writting is made
701  //return ( stat(filename, &buffer) == 0 ? true : false );
702  return ( access (filename, F_OK) == 0 ? true : false);
703 
704 }
705 
708 inline bool fileExists( const FileName &filename )
709 {
710  return fileExists(filename.c_str());
711 }
712 
715 void copyImage(const FileName & source,const FileName & target);
716 
719 void deleteFile(const FileName &fn);
720 
721 //Functions to make easy to change the magicWord used to identify the
722 //file format of metadata
723 extern String FileNameVersion;
724 void setMetadataVersion(String version);
726 
737 {
738 public:
739  inline bool operator ()(const FileName &fn1, const FileName &fn2)
740  {
741  return fn1<fn2;
742  }
743 };
744 
745 
749 class FileLock
750 {
751 protected:
752 
753 
754 #ifndef __MINGW32__
755  struct flock fl;
756 #endif
757 
758  bool islocked;
759  int filenum;
760 
761 public:
762 
765  {
766 #ifndef __MINGW32__
767  fl.l_type = F_WRLCK; /* F_RDLCK, F_WRLCK, F_UNLCK */
768  fl.l_whence = SEEK_SET; /* SEEK_SET, SEEK_CUR, SEEK_END */
769  fl.l_start = 0; /* Offset from l_whence */
770  fl.l_len = 0; /* length, 0 = to EOF */
771  fl.l_pid = getpid(); /* our PID */
772 
773  islocked = false;
774  filenum = 0;
775 #endif
776  }
777 
778  FileLock(int fileno)
779  {
780  FileLock();
781  this->filenum = fileno;
782  }
783 
785  void lock(int fileno = 0);
786  void lock(FILE* file);
787 
789  void unlock();
790 
791 };
792 
794 
795 #endif /* FILENAME_H_ */
FileName(const String &str, const String &ext)
FileName removeLastExtension() const
void waitUntilStableSize(size_t time_step=250000)
String getBlockName() const
FileName(const char *str, const String &ext)
FileName removeFileFormat() const
FileName replaceExtension(const String &newExt) const
void copyImage(const FileName &source, const FileName &target)
FileName(const FileName &fn)
FileName removePrefixNumber() const
FileName addExtension(const String &ext) const
void setMetadataVersion(String version)
FileName insertBeforeExtension(const String &str) const
void createEmptyFileWithGivenLength(size_t length=0) const
void compose(const String &str, const size_t no, const String &ext="")
FileName removeDirectories(int keep=0) const
FileName beforeLastOf(const String &str) const
void initUniqueName(const char *templateStr="xmippTemp_XXXXXX", const String &fnDir="")
bool hasImageExtension() const
FileName removeAllExtensions() const
void decompose(size_t &no, String &str) const
bool existsTrim() const
bool isDir() const
String FileNameVersion
FileName & operator=(const FileName &op)
FileLock(int fileno)
char * getXmippPath()
void getFiles(std::vector< FileName > &files) const
FileName afterLastOf(const String &str) const
String getExtension() const
FileName removeAllPrefixes() const
FileName afterFirstOf(const String &str) const
FileName replaceSubstring(const String &subOld, const String &subNew) const
FileName addPrefix(const String &prefix) const
FileName removeFilename() const
FileName beforeFirstOf(const String &str) const
size_t getPrefixNumber(size_t pos=0) const
void composeBlock(const String &blockName, size_t no, const String &str, const String &ext="")
FileName getRoot() const
bool isStar1(bool failIfNotExists) const
FileLock()
constructor
int makePath(mode_t mode=0755) const
__host__ __device__ float length(float2 v)
FileName(const String &str)
FileName replaceCharacter(char oldChar, char newChar) const
void mode
bool contains(const String &str) const
FileName withoutRoot() const
String getMetadataVersion(void)
bool exists() const
FileName removeUntilPrefix(const String &prefix) const
FileName toLowercase() const
void assertExists()
void deleteFile() const
void createEmptyFile(size_t size, size_t block_size=102400)
FileName(const char *str, int no, const String &ext="")
bool isMetaData(bool failIfNotExists=true) const
FileName removeBlockName() const
FileName withoutExtension() const
bool isEmpty() const
std::string String
Definition: xmipp_strings.h:34
String getString() const
bool fileExists(const char *filename)
String getFileFormat() const
int getNumber() const
FileName getDir() const
FileName(const char *str)
FileName removeSubstring(const String &sub) const
bool hasVolumeExtension() const
FileName toUppercase() const
struct stat Stat
void copyFile(const FileName &target) const
char * getXmippSrcPath()
bool hasStackExtension() const
FileName getBaseName() const
bool hasMetadataExtension() const
FileName removeExtension(const String &ext) const
size_t getFileSize() const
bool isInStack() const
FileName getDecomposedFileName() const
void initRandom(int length)