LightWrap++
AC++wrapperfortheLightWave3DSDK
file_request.h
Go to the documentation of this file.
1 #ifndef FILE_REQUEST_H
2 #define FILE_REQUEST_H
3 
4 #include <lwpp/utility.h>
5 #include <lwpp/storeable.h>
6 #include <lwpp/message.h>
7 #include <deque>
8 
9 #ifndef LWFTYPE_INSTALL
10 #define LWFTYPE_INSTALL "Install" // installation directory, not defined by LW
11 #endif
12 
13 namespace lwpp
14 {
15 #ifdef LWPP_PLATFORM_WIN
16  const char fSep = '\\';
17 #elif defined(LWPP_PLATFORM_OSX_CFM)
18  const char fSep = ':';
19  //extern const char *fSeps = ":";
20 #elif defined(LWPP_PLATFORM_OSX_UB)
21  const char fSep = '/';
22 #else
23 #error no valid Platform
24 #endif
25  extern const char *fSeps;
29 class DirInfo : protected GlobalBase<LWDirInfoFunc>
30  {
31  private:
32  const char *dirType;
33  std::string return_dir;
34  public:
38  DirInfo (const char *dirtype = 0) : dirType(dirtype)
39  {
40  if ((dirtype == 0) || (*dirtype == 0)) dirType = LWFTYPE_CONTENT;
41  }
43 
47  const char *GetDirectory(const char* locType = 0);
48 
49  const std::string GetDirectoryStr(const char* locType = 0)
50  {
51  return std::string(GetDirectory(locType));
52  }
54 
58  const char *stripPath(const char *fileName);
59  };
60 
61  // Directory scanning
62  class Directory
63  {
64  DIR *directory;
65  struct dirent *entry;
66  public:
67  Directory(const std::string name);
68  ~Directory();
69  bool exists();
70  const char *getEntry(void);
71  };
72 
73  bool isDirectory(const std::string name);
74 
75  void makeAllDirectories(const char *path);
76 
77  class FileType : protected GlobalBase<LWFileTypeFunc>
78  {
79  public:
80  FileType (void){;}
81  const char *GetType(const char *filetype) const
82  {
83  return globPtr(filetype);
84  }
85  };
86 
88  std::string getTempPath();
89 
92  {
93  public:
94  virtual bool Post(void) = 0;
95  virtual const char *getFullName(void) = 0;
96  };
97 
100  {
101  struct Data;
102  Data *m_data;
103  public:
104  OSFileRequest (const int reqType = FREQ_LOAD, char *title = "",
105  const char *fileType = "", const char *ext = "", const char *baseName = "",
106  const char *path = "", const char *_baseDirType = 0, int bufLen = 1024);
107 
108  virtual ~OSFileRequest();
109 
110  virtual bool Post(void);
111 
112  virtual const char *getFullName(void);
113  private:
114  int type;
115 #ifdef LWPP_PLATFORM_WIN
116 #elif defined(LWPP_PLATFORM_OSX_CFM)
117  NavDialogCreationOptions dialogOptions;
118  NavDialogRef dialog;
119  OSStatus status;
120  NavReplyRecord replyRecord;
121  std::string temp_path;
122 #elif defined(LWPP_PLATFORM_OSX_UB)
123 #else
124  #error No OS specifice file requester!
125 #endif
126  };
127 
130 
134  class FileRequest2 : public BaseFileRequest, protected GlobalBase<LWFileActivateFunc>, protected DirInfo
135  {
136  private:
137  LWFileReqLocal frloc;
138  public:
149  FileRequest2 (const int reqType = FREQ_LOAD, const char *title = "",
150  const char *fileType = "", const char *baseName = "",
151  const char *path = "", const char *_baseDirType = 0,
152  int (*pickName) (void) = 0, int bufLen = 1024)
153  : DirInfo(_baseDirType)
154  {
155  frloc.reqType = reqType;
156  frloc.bufLen = bufLen;
157  frloc.result = 0;
158  frloc.title = title;
159  frloc.fileType = fileType;
160  frloc.path = new char[bufLen];
161  frloc.baseName = new char[bufLen];
162  frloc.fullName = new char[bufLen];
163  strcpy (frloc.path, path);
164  strcpy (frloc.baseName, baseName);
165  frloc.pickName = pickName;
166  }
168  {
169  delete[] frloc.path;
170  delete[] frloc.baseName;
171  delete[] frloc.fullName;
172  }
174 
177  bool Post(void)
178  {
179  return ((GlobalBase<LWFileActivateFunc>::globPtr( LWFILEREQ_VERSION, &frloc ) == AFUNC_OK) && (frloc.result == 1));
180  }
182  const char *getFullName()
183  {
184  return DirInfo::stripPath(frloc.fullName);
185  }
186  const char *getFullPath() const
187  {
188  return frloc.fullName;
189  }
190  const char *getPath() const
191  {
192  return frloc.path;
193  }
194  LWFileReqLocal *getFileReqLocal()
195  {
196  return &frloc;
197  }
198  void setBaseName(const std::string name)
199  {
200  strcpy (frloc.baseName, name.c_str());
201  }
202  };
203 
204  // remove trailing file separator
205  void trimTrail(std::string &path);
206 
208 
223  class FileName : public Storeable
224  {
225  private:
226  const char *baseDir;
227  std::string fileName;
228  std::string relativePath;
229  std::string fullName;
230  std::string relName;
231  bool valid;
232  bool useBase;
233 
234  void buildFullName();
236  void splitFileName();
238  bool setRelativeName(const char *relName);
239  void setExtension(const char *ext);
240  static std::string extractExtension(const char *source);
241  public:
243 
247  FileName (const char *_baseDir = LWFTYPE_CONTENT, bool base = false) : baseDir(_baseDir), valid(false), useBase(base)
248  {
249  ;
250  }
251  FileName (const std::string name, bool base = false) : baseDir(LWFTYPE_CONTENT), valid(false), useBase(base)
252  {
253  setFullName(name);
254  }
256  std::string getRelativeName();
257  const char *getRelativeNameC();
258  //std::string getRelativeName(const char *_baseDir);
260  std::string getFileName(void);
262  std::string getPath(void);
264  std::string getFullPath(void);
266  //void makePath(void);
267 
268  const char *getFullName()
269  {
270  if (!valid) buildFullName();
271 #ifdef _DEBUG
272  lwpp::dostream dout;
273  dout << "FileName::getFullName() - " << fullName << std::endl;
274 #endif
275  return fullName.c_str();
276  }
277  std::string getAbsoluteName()
278  {
279  if (!valid) buildFullName();
280  if (!useBase)
281  {
282  DirInfo di(baseDir);
283  return std::string(di.GetDirectory()) + fullName;
284  }
285  return fullName;
286  }
287  FileName &operator=(const char *name)
288  {
289  setFullName(name);
290  return *this;
291  }
292  FileName &operator=(const std::string name)
293  {
294  setFullName(name);
295  return *this;
296  }
298  void setFullName(const std::string name)
299  {
300  fullName = name;
301  splitFileName();
302  }
304  void setFileName(const std::string name)
305  {
306  fileName = name;
307  valid = false;
308  }
310  void setFileExtension(const char *extension = "")
311  {
312  setExtension(extension);
313  }
314  std::string getFileExtension()
315  {
316  return std::string(extractExtension(fileName.c_str()));
317  }
318  // remove trailing numbers
319  void stripNumberTrail(void);
321  void setExtractedExtension(const char *source)
322  {
323  std::string ext(extractImageExtension(source));
324  setExtension(ext.c_str());
325  }
326  static std::string extractImageExtension(const char *source);
327 
329  //static bool Exists(const char *name);
330  static bool Exists(const std::string &name); //{return Exists(name.c_str());}
331  bool Exists() {return Exists(getFullName());}
332 
333  LWError Load(const LoadState &ls );
334  LWError Save(const SaveState &ss );
335  };
336 
337  std::string stripExtension(const char *source);
338 
339  std::string trim(const std::string& s, const char *ws=" \t\r");
340 
342  std::string makeRelativeName(const std::string basePath, const std::string absFileName);
343 
344  std::string makeAbsoluteName(const std::string basePath, const std::string relFileName);
345 
346  typedef std::deque<std::string> pathList;
347  std::string makeRelativeName(pathList basePath, const std::string absFileName);
348  pathList splitPath(std::string sourcePath);
349  pathList splitFullPath(std::string sourcePath, std::string &fileName, std::string &ext);
350  std::string makeFullFileName(const pathList &path, const std::string &file, const std::string &ext);
351  std::string makeFullPath(const pathList &path);
352 
353  std::string makeAbsoluteName(pathList basePath, const std::string relFileName);
354  std::string makeAbsoluteName(const std::string basePath, const std::string relFileName);
355 
357  class Stat
358  {
359  struct statData;
360  statData *d;
361  public:
363  Stat(const std::string fName);
364  ~Stat();
366  bool isDirectory() const;
368  bool exists() const;
370  time_t getCreateTimeStamp() const;
372  time_t getModifyTimeStamp() const;
374  size_t getFileSize() const;
375  };
376 } // end namespace lwpp
377 
378 #endif // FILE_REQUEST_H