Statistics
| Branch: | Revision:

root / src / common / fileutil.h @ master

History | View | Annotate | Download (2.48 KB)

1
//==========================================================================
2
//  FILEUTIL.H - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//  Author: Andras Varga
7
//
8
//==========================================================================
9

    
10
/*--------------------------------------------------------------*
11
  Copyright (C) 2006-2008 OpenSim Ltd.
12

13
  This file is distributed WITHOUT ANY WARRANTY. See the file
14
  `license' for details on this and other legal matters.
15
*--------------------------------------------------------------*/
16

    
17
#ifndef __FILEUTIL_H
18
#define __FILEUTIL_H
19

    
20
#include <string>
21
#include "commondefs.h"
22

    
23
NAMESPACE_BEGIN
24

    
25
#ifdef _WIN32
26
#define PATH_SEPARATOR   ";"
27
#else
28
#define PATH_SEPARATOR   ":;"
29
#endif
30

    
31
/**
32
 * Convert backslashes to slashes.
33
 */
34
COMMON_API std::string fileNameToSlash(const char *fileName);
35

    
36
/**
37
 * Splits the last segment of the given path. Use only with canonical absolute paths!
38
 * (In extreme cases and irregularities in the path the result is unpredictable)
39
 */
40
COMMON_API void splitFileName(const char *pathname, std::string& dir, std::string& fnameonly);
41

    
42
/**
43
 * Calls splitFileName() and returns the dir part of the result
44
 */
45
COMMON_API std::string directoryOf(const char *pathname);
46

    
47
/**
48
 * Canonicalizes the given path. For example, changes backslashes to slashes
49
 * (or the other way round, depending on the slashes arg), kills off multiple slashes,
50
 * dot and dot-dot components ((foo//bar --> foo/bar, foo/./bar --> foo/bar,
51
 * foo/../bar --> bar), etc. You may want to convert the path to absolute
52
 * (toAbsolutePath()) before calling this function.
53
 */
54
COMMON_API std::string tidyFilename(const char *pathname, bool slashes=false);
55
COMMON_API std::string toAbsolutePath(const char *pathname);
56
COMMON_API std::string concatDirAndFile(const char *basedir, const char *pathname);
57

    
58
/** Returns true if given file or directory exists */
59
COMMON_API bool fileExists(const char *pathname);
60
COMMON_API bool isDirectory(const char *pathname);
61
COMMON_API void removeFile(const char *fname, const char *descr);
62

    
63
/** Recursively create all directories in the specified path */
64
COMMON_API void mkPath(const char *pathname);
65

    
66
/**
67
 * Utility class for temporary change of directory. Changes back to
68
 * original dir when goes out of scope. Does nothing if NULL is passed
69
 * to the constructor.
70
 */
71
class COMMON_API PushDir
72
{
73
  private:
74
    std::string olddir;
75
  public:
76
    PushDir(const char *changetodir);
77
    ~PushDir();
78
};
79

    
80
NAMESPACE_END
81

    
82

    
83
#endif
84