1 /* 3dScreamers */
2 /* bzflag
3  * Copyright (c) 1993-2021 Tim Riker
4  *
5  * This package is free software;  you can redistribute it and/or
6  * modify it under the terms of the license found in the file
7  * named COPYING that should have accompanied this file.
8  *
9  * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
10  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
12  */
13 
14 #ifndef _OSFILE_H_
15 #define _OSFILE_H_
16 
17 /* common header */
18 #include "common.h"
19 
20 /* system headers */
21 #ifdef _WIN32
22 #define WIN32_LEAN_AND_MEAN    // Exclude rarely-used stuff from Windows headers
23 #include <windows.h>
24 #include <io.h>
25 #include <direct.h>
26 #else
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <dirent.h>
31 #include <ctype.h>
32 #endif
33 
34 #include <string>
35 #include <vector>
36 
37 #include <stdio.h>
38 
39 typedef enum
40 {
41     eFileStart,
42     eCurentPos,
43     eFileEnd
44 } teFilePos;
45 
46 void setOSFileBaseDir(const std::string &dir);
47 void OSFileOSToStdDir(std::string &dir);
48 
49 class OSFile
50 {
51 public:
52     OSFile();
53     OSFile(const OSFile &r);
54     OSFile& operator=(const OSFile &r);
55 
56     OSFile(const std::string &szName);
57     OSFile(const char *szName);
58 
59     OSFile(const std::string &szName, const char *szMode);
60     ~OSFile();
61 
62     bool open(const std::string &szName, const char *szMode);
63     bool open(const char *szMode);
64     bool close();
65 
66     void stdName(const std::string &szName);
67     void osName(const std::string &szName);
68 
69     FILE* getFile();
70 
71     std::string getStdName();
72     std::string getOSName();
73 
74     std::string getFileName();
75 
76     std::string getExtension();
77 
78     std::string getFullOSPath();
79 
80     std::string getOSFileDir();
81 
82     bool isOpen();
83 
84     int read(void* data, int size, int count = 1);
85     unsigned char readChar();
86     int scanChar(unsigned char *pChar);
87     const char* scanStr();
88     std::string readLine();
89     int write(const void* data, int size);
90     void flush();
91 
92     int seek(teFilePos ePos, int iOffset);
93     unsigned int size();
94     unsigned int tell();
95 
96     void setUseGlobalPath(bool use = false);
97 protected:
98     class OSFileInfo;
99     OSFileInfo    *info;
100 };
101 
102 
103 class OSDir
104 {
105 public:
106     OSDir();
107     OSDir(const OSDir &r);
108     OSDir& operator=(const OSDir &r);
109     OSDir(const std::string &DirName);
110     ~OSDir();
111 
112     void setStdDir(const std::string &DirName);
113     void setOSDir(const std::string &DirName);
114 
115     void makeStdDir(const std::string &DirName);
116     void makeOSDir(const std::string &DirName);
117 
118     bool getNextFile(OSFile &oFile, bool bRecursive);
119     bool getNextFile(OSFile &oFile, const char* fileMask, bool bRecursive);
120 
121     int getFileScanCount();
122 
123     std::string getStdName();
124     std::string getOSName();
125     std::string getFullOSPath();
126 
127     std::string getOSFileDir();
128 
129 protected:
130     class OSDirInfo;
131     OSDirInfo    *info;
132 
133     bool windowsAddFileStack(std::string pathName, std::string fileMask, bool bRecursive);
134     bool linuxAddFileStack(std::string pathName, std::string fileMask, bool bRecursive);
135 };
136 
137 
138 #endif//_OSFILE_H_
139 
140 // Local Variables: ***
141 // mode: C++ ***
142 // tab-width: 4 ***
143 // c-basic-offset: 4 ***
144 // indent-tabs-mode: nil ***
145 // End: ***
146 // ex: shiftwidth=4 tabstop=4
147