root/swishctl/src/setup/paths.cpp

Revision 1232, 6.3 kB (checked in by augur, 5 years ago)

Initial revision

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1 // paths.cpp: implementation of the CDirectory class.
2 //
3 //////////////////////////////////////////////////////////////////////
4
5 #include "stdafx.h"
6 #include "paths.h"
7 #include <string>
8
9 using namespace std;
10
11 void CBuffer::init(char *initialvalue, unsigned int initialsize)
12 {
13         delete[] buffer; //
14         buffersize = initialsize;
15         if ( !buffersize ) {
16                 buffer = NULL;
17                 return;
18         }
19        
20         if ( !initialvalue ) {
21                 buffer = new char[buffersize];
22                 buffer[0] = '\0';
23         } else {
24                 unsigned int namelength = strlen( initialvalue );
25                 // use a larger buffer if necessary
26                 if ( namelength > buffersize ) {
27                         buffersize = namelength + 1;
28                 }
29                 buffer = new char[buffersize];
30                 strcpy( buffer, initialvalue );
31         }
32 }
33
34 CBuffer::CBuffer(unsigned int maxsize)
35 {
36         buffer = NULL;
37         buffmax = maxsize;
38         init( NULL, buffmax );
39 }
40
41 CBuffer::CBuffer(unsigned int maxsize, CBuffer &source)
42 {
43         buffmax = maxsize;
44         buffer = NULL;
45         init( source.getBuffer(), source.getSize());
46 }
47
48 CBuffer::CBuffer (unsigned int maxsize, char *initialvalue)
49 {
50         buffmax = maxsize;
51         buffer = NULL;
52         init( initialvalue, buffmax);
53 }
54
55 void CBuffer::operator =(CBuffer &source)
56 {
57         init( source.getBuffer(), source.getSize());
58 }
59
60 void CBuffer::operator =(char *initialvalue)
61 {
62         init( initialvalue, buffmax);
63 }
64
65 CBuffer::~CBuffer()
66 {
67         delete[] buffer;
68 }
69
70
71 //////////////////////////////////////////////////////////////////////
72 // Construction/Destruction
73 //////////////////////////////////////////////////////////////////////
74
75 CDirectory& CDirectory::operator =(CDirectory &source)
76 {
77         // should I delete "this" first?
78         if (this == &source) return *this;     
79         return *(new CDirectory(source));
80 }
81
82 CDirectory& CDirectory::operator =(char *dirname)
83 {
84         // should I delete "this" first?
85         return *(new CDirectory(dirname));
86 }
87
88 CPath& CDirectory::Append(char *extra)
89 {
90         if ( buffer ) {
91                 CPath *temp = new CPath(buffer);
92
93                 int thislen = strlen (buffer);
94                 char finalchar = buffer[thislen-1];
95
96                 if ( finalchar != '\\' &&  finalchar != '/' ) {
97                         strcat( temp->getBuffer(), "\\");
98                 }
99                
100                 strcat(  temp->getBuffer(), extra);
101                 return *temp;           
102         } else {                 // this.m_str is null
103                 // return a copy of the filename...
104                 return *(new CPath(extra));
105         }
106
107 }
108
109 CPath& CDirectory::operator +(char *extra)
110 {
111         return Append(extra);
112 }
113
114
115 CPath& CDirectory::operator +(CFilename &filename)
116 {
117         return Append( filename.getBuffer());
118 }
119
120 void CPath::operator =(CPath &source)
121 {
122         init( source.getBuffer(), source.getSize());
123 }
124
125 void CPath::operator =(char *pathname)
126 {
127         init( pathname, _MAX_PATH);
128 }
129
130
131
132 CFilename& CPath::getFilename()
133 {
134         static char fname[_MAX_FNAME];
135         static char extname[_MAX_EXT];
136         CFilename *temp = new CFilename();
137
138         // split the FileNameAndExtension parameter
139    _splitpath (buffer, NULL, NULL, fname, extname);
140         // assemble full path
141    _makepath (temp->getBuffer(), NULL, NULL, fname, extname);
142
143    return *temp;
144 }
145
146
147 CDirectory& CPath::getDirectory()
148 {
149         static char drvname[_MAX_FNAME];
150         static char dirname[_MAX_EXT];
151         CDirectory *temp = new CDirectory();
152
153         // split the FileNameAndExtension parameter
154    _splitpath (buffer, drvname, dirname, NULL, NULL);
155         // assemble full path
156    _makepath (temp->getBuffer(), drvname, dirname, NULL, NULL);
157
158    return *temp;
159 }
160
161
162
163 //////////////////////////////////////////////////////////////////////
164 // CFilename Class
165 //////////////////////////////////////////////////////////////////////
166
167 //////////////////////////////////////////////////////////////////////
168 // Construction/Destruction
169 //////////////////////////////////////////////////////////////////////
170
171 void CFilename::operator =(CFilename &source)
172 {
173         init( source.getBuffer(), source.getSize());
174 }
175
176 void CFilename::operator =(char *filename)
177 {
178         init( filename, buffmax);
179 }
180
181 //////////////////////////////////////////////////////////////////////
182 // CAppDirectory Class
183 //////////////////////////////////////////////////////////////////////
184
185 //////////////////////////////////////////////////////////////////////
186 // Construction/Destruction
187 //////////////////////////////////////////////////////////////////////
188
189 CAppDirectory::CAppDirectory(HINSTANCE apphandle)
190 {
191         CPath temppath;
192
193         DWORD copied = GetModuleFileName( apphandle,
194                 temppath.getBuffer(), temppath.getSize());
195         CDirectory tempdir = temppath.getDirectory();
196         strcpy(getBuffer(), tempdir.getBuffer());
197        
198 }
199
200 CWinDirectory::CWinDirectory()
201 {
202         UINT buffsize = GetWindowsDirectory (getBuffer(), 0);
203         if ( buffsize > getSize() ) {
204                 init( NULL, buffsize );
205         }
206         GetWindowsDirectory (getBuffer(), buffsize); // result should be < buffsize
207 }
208
209 CStartProgramsDirectory::CStartProgramsDirectory()
210 {
211         LPITEMIDLIST   pidlPrograms;
212
213         //get the pidl for Start|Programs
214         SHGetSpecialFolderLocation(NULL, CSIDL_PROGRAMS, &pidlPrograms);
215
216         BOOL result = SHGetPathFromIDList( pidlPrograms, getBuffer() );
217
218 }
219
220 void CStartProgramsDirectory::NotifyChanged()
221 {
222         SHChangeNotify(SHCNE_MKDIR, SHCNF_PATH, getBuffer(), 0);
223 }
224
225 CDirectory & CStartProgramsDirectory::operator+(char *childname)
226 {
227         CDirectory *fullpath = new CDirectory(*this);
228        
229         unsigned int pathlength = strlen(fullpath->getBuffer());
230
231         char finalchar = (fullpath->getBuffer())[pathlength-1];
232
233         if ( finalchar != '\\' &&  finalchar != '/' ) {
234                 strcat( fullpath->getBuffer(), "\\");
235         }
236         strcat( fullpath->getBuffer(), childname);     
237
238         return *fullpath;
239 }
240
241 CDirectory &CStartProgramsDirectory::CreateChild(char *childname)
242 {
243         CDirectory *fullpath = new CDirectory(*this);
244        
245         unsigned int pathlength = strlen(fullpath->getBuffer());
246
247         char finalchar = (fullpath->getBuffer())[pathlength-1];
248
249         if ( finalchar != '\\' &&  finalchar != '/' ) {
250                 strcat( fullpath->getBuffer(), "\\");
251         }
252         strcat( fullpath->getBuffer(), childname);     
253
254         if (! CreateDirectory(fullpath->getBuffer(), NULL) ) {
255                 // what todo: ???
256         }
257
258         return *fullpath;
259 }
260
261 bool CStartProgramsDirectory::DeleteChild(char *childname)
262 {
263         CDirectory fullpath = *this + childname;
264         return RemoveDirectory(fullpath.getBuffer()) != 0;
265 }
266
267
268 void TestPathsModule(HINSTANCE apphandle)
269 {
270         CDirectory dir1( "C:\\Program Files\\" );
271         CDirectory dircopy( dir1 );
272         CFilename fn("yaah.txt" );
273         CPath &pathname = dir1 + fn;
274         CAppDirectory app(apphandle);
275
276         CWinDirectory dir2;
277         CPath path3 = dir2 + fn;
278         CFilename fn2 = pathname.getFilename();
279
280         try {
281                 char *dir1buff = dir1.getBuffer();
282                 unsigned int dir1size = dir1.getSize();
283
284
285                 unsigned int dir2size = dircopy.getSize(); // should throw an exception
286                 char *dir2buff = dircopy.getBuffer();
287
288         } catch (...) {
289                 int i = 1;
290         }
291 }
292
293
Note: See TracBrowser for help on using the browser.