1 /*  Copyright (c) MediaArea.net SARL. All Rights Reserved.
2  *
3  *  Use of this source code is governed by a zlib-style license that can
4  *  be found in the License.txt file in the root of the source tree.
5  */
6 
7 //---------------------------------------------------------------------------
8 #include "ZenLib/PreComp.h"
9 #ifdef __BORLANDC__
10     #pragma hdrstop
11 #endif
12 //---------------------------------------------------------------------------
13 
14 //---------------------------------------------------------------------------
15 #include "ZenLib/Conf_Internal.h"
16 //---------------------------------------------------------------------------
17 
18 //---------------------------------------------------------------------------
19 #include "ZenLib/FileName.h"
20 #ifdef ZENLIB_USEWX
21     #include <wx/filename.h>
22 #else //ZENLIB_USEWX
23     #ifdef ZENLIB_STANDARD
24         #undef WINDOWS
25     #endif
26     #ifdef WINDOWS
27         #undef __TEXT
28         #include <windows.h>
29     #endif
30 #endif //ZENLIB_USEWX
31 //---------------------------------------------------------------------------
32 #undef ZENLIB_USEWX
33 
34 namespace ZenLib
35 {
36 
37 //***************************************************************************
38 // Read/Write
39 //***************************************************************************
40 
41 //---------------------------------------------------------------------------
Path_Get() const42 Ztring FileName::Path_Get() const
43 {
44     #ifdef ZENLIB_USEWX
45         wxFileName FN(c_str());
46         return FN.GetPath().c_str();
47     #else //ZENLIB_USEWX
48         //Path limit
49         size_t Pos_Path=rfind(FileName_PathSeparator);
50         if (Pos_Path==Ztring::npos)
51             return Ztring(); //Not found
52         else
53             return Ztring(*this, 0, Pos_Path);
54     #endif //ZENLIB_USEWX
55 }
56 
57 //---------------------------------------------------------------------------
Path_Set(const Ztring & Path)58 Ztring& FileName::Path_Set(const Ztring &Path)
59 {
60     #ifdef ZENLIB_USEWX
61         wxFileName FN(c_str());
62         FN.SetPath(Path.c_str());
63         assign (FN.GetFullPath().c_str());
64     #else //ZENLIB_USEWX
65         #ifdef WINDOWS
66             //Path limit
67             size_t Pos_Path=rfind(__T('\\'));
68             if (Pos_Path==Ztring::npos)
69             {
70                 insert(0, 1, __T('\\')); //Not found
71                 Pos_Path=0;
72             }
73             replace(0, Pos_Path, Path, 0, Ztring::npos);
74         #else
75             //Not supported
76         #endif
77     #endif //ZENLIB_USEWX
78     return *this;
79 }
80 
81 //---------------------------------------------------------------------------
Name_Get() const82 Ztring FileName::Name_Get() const
83 {
84     #ifdef ZENLIB_USEWX
85         wxFileName FN(c_str());
86         if (FN==FN.GetName()) //Bug of WxWidgets? if C:\\dir\\(no name), name is C:\\dir\\(no name)
87             return Ztring();
88         return FN.GetName().c_str();
89     #else //ZENLIB_USEWX
90         size_t Pos_Path=rfind(FileName_PathSeparator); //Path limit
91         if (Pos_Path==Ztring::npos)
92             Pos_Path=0; //Not found
93         else
94             Pos_Path+=Ztring(FileName_PathSeparator).size(); //Path separator size
95         //Extension limit
96         size_t Pos_Ext=rfind(__T('.'));
97         if (Pos_Ext==Ztring::npos || Pos_Ext<Pos_Path)
98             Pos_Ext=size(); //Not found
99         return Ztring(*this, Pos_Path, Pos_Ext-Pos_Path);
100     #endif //ZENLIB_USEWX
101 }
102 
103 //---------------------------------------------------------------------------
Name_Set(const Ztring & Name)104 Ztring& FileName::Name_Set(const Ztring &Name)
105 {
106     #ifdef ZENLIB_USEWX
107         wxFileName FN(c_str());
108         if (FN==FN.GetName()) //Bug of WxWidgets? if C:\\dir\\(no name), name is C:\\dir\\(no name)
109             FN.SetPath(c_str());
110         FN.SetName(Name.c_str());
111         assign ((FN.GetFullPath()+FN.GetPathSeparator()/*FileName_PathSeparator*/+FN.GetFullName()).c_str());
112     #else //ZENLIB_USEWX
113         #ifdef WINDOWS
114             //Path limit
115             size_t Pos_Path=rfind(__T('\\'));
116             if (Pos_Path==Ztring::npos)
117                 Pos_Path=0; //Not found
118             //Extension limit
119             size_t Pos_Ext=rfind(__T('.'));
120             if (Pos_Ext==Ztring::npos || Pos_Ext<Pos_Path)
121                 Pos_Ext=size(); //Not found
122             replace(Pos_Path+1, Pos_Ext-Pos_Path-1, Name, 0, Ztring::npos);
123         #else
124             //Not supported
125         #endif
126     #endif //ZENLIB_USEWX
127     return *this;
128 }
129 
130 //---------------------------------------------------------------------------
Extension_Get() const131 Ztring FileName::Extension_Get() const
132 {
133     #ifdef ZENLIB_USEWX
134         wxFileName FN(c_str());
135         return FN.GetExt().c_str();
136     #else //ZENLIB_USEWX
137         //Path limit
138         size_t Pos_Path=rfind(FileName_PathSeparator);
139         if (Pos_Path==Ztring::npos)
140             Pos_Path=0; //Not found
141         //Extension limit
142         size_t Pos_Ext=rfind(__T('.'));
143         if (Pos_Ext==Ztring::npos || Pos_Ext<Pos_Path)
144             return Ztring(); //Not found
145         else
146             return Ztring(*this, Pos_Ext+1, size()-Pos_Ext-1);
147     #endif //ZENLIB_USEWX
148 }
149 
150 //---------------------------------------------------------------------------
Extension_Set(const Ztring & Extension)151 Ztring& FileName::Extension_Set(const Ztring &Extension)
152 {
153     #ifdef ZENLIB_USEWX
154         wxFileName FN(c_str());
155         FN.SetExt(Extension.c_str());
156         assign (FN.GetFullPath().c_str());
157     #else //ZENLIB_USEWX
158         #ifdef WINDOWS
159             //Path limit
160             size_t Pos_Path=rfind(__T('\\'));
161             if (Pos_Path==Ztring::npos)
162                 Pos_Path=0; //Not found
163             //Extension limit
164             size_t Pos_Ext=rfind(__T('.'));
165             if (Pos_Ext==Ztring::npos || Pos_Ext<Pos_Path)
166             {
167                 append(1, __T('.')); //Not found
168                 Pos_Ext=size()-1;
169             }
170             replace(Pos_Ext+1, size()-Pos_Ext-1, Extension, 0, Ztring::npos);
171         #else
172             //Not supported
173         #endif
174     #endif //ZENLIB_USEWX
175     return *this;
176 }
177 
178 //***************************************************************************
179 // Helpers
180 //***************************************************************************
181 
182 //---------------------------------------------------------------------------
TempFileName_Create(const Ztring & Prefix)183 Ztring FileName::TempFileName_Create(const Ztring &Prefix)
184 {
185     #ifdef ZENLIB_USEWX
186         return wxFileName::CreateTempFileName(Prefix.c_str()).c_str();
187     #else //ZENLIB_USEWX
188         #ifdef WINDOWS
189             Char Path[MAX_PATH+1];
190             if (!GetTempPath(MAX_PATH, Path))
191                 return Ztring(); //Problem while getting a temp path
192 
193             Char FileName[MAX_PATH+1];
194             if (!GetTempFileName(Path, Prefix.c_str(), 0, FileName))
195                  return Ztring(); //Problem while getting a file name
196 
197             return Ztring(FileName);
198         #else
199             return __T("C:\\xxx.txt");
200         #endif
201     #endif //ZENLIB_USEWX
202 }
203 
204 //***************************************************************************
205 // Platform differences
206 //***************************************************************************
207 
208 //End of line
209 #ifdef __WINDOWS__
210     const Char* FileName_PathSeparator=__T("\\");
211 #endif
212 #if defined(UNIX) || defined(MACOS) || defined(MACOSX)
213     const Char* FileName_PathSeparator=__T("/");
214 #endif
215 
216 } //namespace
217