diff options
Diffstat (limited to 'src/lib/hd24devicenamegenerator.cpp')
-rw-r--r-- | src/lib/hd24devicenamegenerator.cpp | 286 |
1 files changed, 286 insertions, 0 deletions
diff --git a/src/lib/hd24devicenamegenerator.cpp b/src/lib/hd24devicenamegenerator.cpp new file mode 100644 index 0000000..99ad4a7 --- /dev/null +++ b/src/lib/hd24devicenamegenerator.cpp | |||
@@ -0,0 +1,286 @@ | |||
1 | #include "hd24devicenamegenerator.h" | ||
2 | #define DEVGENDEBUG 0 | ||
3 | #include <stdio.h> | ||
4 | #include <string.h> | ||
5 | #include <stdlib.h> | ||
6 | #ifdef DARWIN | ||
7 | #include <sys/malloc.h> | ||
8 | #else | ||
9 | #include <malloc.h> | ||
10 | #endif | ||
11 | #include <sys/types.h> | ||
12 | #include <dirent.h> | ||
13 | int endswith(const char* str, const char* end) | ||
14 | { | ||
15 | #if (DEVGENDEBUG==1) | ||
16 | cout << "endswith("<<str<<","<<end<<")"<<endl; | ||
17 | #endif | ||
18 | if (str==NULL) return false; | ||
19 | if (end==NULL) return false; | ||
20 | size_t str_len = strlen(str); | ||
21 | size_t end_len = strlen(end); | ||
22 | if (str_len<end_len) return false; | ||
23 | |||
24 | const char *str_end = str + str_len - end_len; | ||
25 | return strcmp(str_end, end) == 0; | ||
26 | } | ||
27 | |||
28 | __uint32 hd24devicenamegenerator::hd24filecount(const char* dirname) | ||
29 | { | ||
30 | #if (DEVGENDEBUG==1) | ||
31 | cout << "hd24devicenamegenerator::hd24filecount("<<dirname<<")"<< endl; | ||
32 | #endif | ||
33 | unsigned char isFile =0x8; | ||
34 | DIR *Dir; | ||
35 | struct dirent *DirEntry; | ||
36 | Dir = opendir(dirname); | ||
37 | |||
38 | int foundcount=0; | ||
39 | do | ||
40 | { | ||
41 | DirEntry=readdir(Dir); | ||
42 | if (!DirEntry) break; | ||
43 | if ( DirEntry->d_type != isFile) continue; | ||
44 | |||
45 | if (!( endswith(DirEntry->d_name,".h24") | ||
46 | ||endswith(DirEntry->d_name,".H24"))) continue; | ||
47 | |||
48 | foundcount++; | ||
49 | |||
50 | if (filelist==NULL) | ||
51 | { | ||
52 | filelist=new vector<string>(); | ||
53 | } | ||
54 | string* fname=new string(dirname); | ||
55 | *fname+=DirEntry->d_name; | ||
56 | filelist->push_back(*fname); | ||
57 | #if (DEVGENDEBUG==1) | ||
58 | cout <<"Found a File : " << *fname << endl; | ||
59 | #endif | ||
60 | delete fname; | ||
61 | |||
62 | // validity of file image will be extablished by hd24fs device scan | ||
63 | } while(1); | ||
64 | |||
65 | return foundcount; | ||
66 | } | ||
67 | |||
68 | void hd24devicenamegenerator::initvars() | ||
69 | { | ||
70 | imagespath=NULL; | ||
71 | filelist=NULL; | ||
72 | filecount=0; // cache for getnumberoffiles | ||
73 | } | ||
74 | |||
75 | void hd24devicenamegenerator::clearfilelist() | ||
76 | { | ||
77 | if (filelist==NULL) | ||
78 | { | ||
79 | return; | ||
80 | } | ||
81 | while (filelist->size()!=0) | ||
82 | { | ||
83 | filelist->pop_back(); | ||
84 | } | ||
85 | delete filelist; | ||
86 | filelist=NULL; | ||
87 | } | ||
88 | |||
89 | hd24devicenamegenerator::~hd24devicenamegenerator() | ||
90 | { | ||
91 | clearfilelist(); | ||
92 | } | ||
93 | hd24devicenamegenerator::hd24devicenamegenerator() | ||
94 | { | ||
95 | initvars(); | ||
96 | } | ||
97 | |||
98 | const char* hd24devicenamegenerator::imagedir() | ||
99 | { | ||
100 | #if (DEVGENDEBUG==1) | ||
101 | cout << "hd24devicenamegenerator::imagedir()"<<endl; | ||
102 | #endif | ||
103 | return imagespath; | ||
104 | } | ||
105 | |||
106 | const char* hd24devicenamegenerator::imagedir(const char* newdir) | ||
107 | { | ||
108 | #if (DEVGENDEBUG==1) | ||
109 | if (newdir==NULL) | ||
110 | { | ||
111 | cout << "hd24devicenamegenerator::imagedir(NULL)"<< endl; | ||
112 | } | ||
113 | else | ||
114 | { | ||
115 | cout << "hd24devicenamegenerator::imagedir("<<newdir<<")"<< endl; | ||
116 | } | ||
117 | #endif | ||
118 | |||
119 | filecount=0; | ||
120 | clearfilelist(); | ||
121 | |||
122 | if (imagespath!=NULL) | ||
123 | { | ||
124 | free (imagespath); | ||
125 | imagespath=NULL; | ||
126 | } | ||
127 | |||
128 | if (newdir!=NULL) | ||
129 | { | ||
130 | imagespath=(char*)malloc(strlen(newdir)+1); | ||
131 | if (imagespath!=NULL) | ||
132 | { | ||
133 | strncpy(imagespath,newdir,strlen(newdir)+1); | ||
134 | } | ||
135 | #if (DEVGENDEBUG==1) | ||
136 | cout << "new images path is " ; | ||
137 | if (imagespath==NULL) | ||
138 | { | ||
139 | cout << "NULL" << endl; | ||
140 | } else { | ||
141 | cout << imagespath << endl; | ||
142 | } | ||
143 | #endif | ||
144 | } | ||
145 | |||
146 | return (const char*)imagespath; | ||
147 | } | ||
148 | |||
149 | __uint32 hd24devicenamegenerator::getnumberoffiles() | ||
150 | { | ||
151 | #if (DEVGENDEBUG==1) | ||
152 | cout << "hd24devicenamegenerator::getnumberoffiles()"<< endl; | ||
153 | #endif | ||
154 | // Files in the image directory will also be considered devices. | ||
155 | if (filecount==0) | ||
156 | { | ||
157 | if (imagedir()==NULL) | ||
158 | { | ||
159 | return 0; | ||
160 | } | ||
161 | // retrieve a list of .h24 image files. | ||
162 | filecount=hd24filecount(imagedir()); | ||
163 | } | ||
164 | return filecount; | ||
165 | } | ||
166 | |||
167 | __uint32 hd24devicenamegenerator::getnumberofsysdevs() | ||
168 | { | ||
169 | #if (DEVGENDEBUG==1) | ||
170 | cout << "hd24devicenamegenerator::getnumberofsysdevs()"<< endl; | ||
171 | #endif | ||
172 | #ifdef LINUX | ||
173 | // For linux the total number of names is 26x /dev/hd* + 26x /dev/sd* | ||
174 | return 52; | ||
175 | #endif | ||
176 | #ifdef WINDOWS | ||
177 | return 128; | ||
178 | #endif | ||
179 | #ifdef DARWIN | ||
180 | return 100; | ||
181 | #endif | ||
182 | } | ||
183 | |||
184 | __uint32 hd24devicenamegenerator::getnumberofnames() | ||
185 | { | ||
186 | #if (DEVGENDEBUG==1) | ||
187 | cout << "hd24devicenamegenerator::getnumberofnames()"<< endl; | ||
188 | #endif | ||
189 | __uint32 filecount=this->getnumberoffiles(); | ||
190 | __uint32 devcount=this->getnumberofsysdevs(); | ||
191 | #if (DEVGENDEBUG==1) | ||
192 | cout << "return: filecount="<<filecount<<", devcount="<<devcount<<endl; | ||
193 | #endif | ||
194 | return filecount+devcount; | ||
195 | } | ||
196 | |||
197 | const char* hd24devicenamegenerator::getfilename(__uint32 filenum) | ||
198 | { | ||
199 | #if (DEVGENDEBUG==1) | ||
200 | cout << "hd24devicenamegenerator::getfilename("<<filenum<<")"<< endl; | ||
201 | #endif | ||
202 | // Given the list of files, this will return the Nth name (base 0). | ||
203 | if (filelist==NULL) | ||
204 | { | ||
205 | return ""; | ||
206 | } | ||
207 | if (filelist->size()==0) | ||
208 | { | ||
209 | return ""; | ||
210 | } | ||
211 | return filelist->at(filenum).c_str(); | ||
212 | } | ||
213 | |||
214 | #ifdef LINUX | ||
215 | string* hd24devicenamegenerator::getdevicename(__uint32 devicenumber) | ||
216 | { | ||
217 | #if (DEVGENDEBUG==1) | ||
218 | cout << "hd24devicenamegenerator::getdevicename("<<devicenumber<<")"<< endl; | ||
219 | #endif | ||
220 | if (devicenumber>=this->getnumberofsysdevs()) | ||
221 | { | ||
222 | return new string(getfilename(devicenumber-this->getnumberofsysdevs())); | ||
223 | } | ||
224 | string* devname; | ||
225 | __uint32 devgroup = devicenumber; | ||
226 | devicenumber = (devicenumber%26); | ||
227 | devgroup -= devicenumber; | ||
228 | devgroup /= 26; | ||
229 | |||
230 | switch (devgroup) | ||
231 | { | ||
232 | case 0: | ||
233 | devname = new string("/dev/hd"); | ||
234 | break; | ||
235 | case 1: | ||
236 | devname = new string("/dev/sd"); | ||
237 | break; | ||
238 | default: | ||
239 | return new string(""); | ||
240 | } | ||
241 | |||
242 | char devletter = (char) ('a' + devicenumber); | ||
243 | *devname += devletter; | ||
244 | |||
245 | return devname; | ||
246 | } | ||
247 | #endif | ||
248 | |||
249 | #ifdef WINDOWS | ||
250 | |||
251 | string* hd24devicenamegenerator::getdevicename(__uint32 devicenumber) | ||
252 | { | ||
253 | #if (DEVGENDEBUG==1) | ||
254 | cout << "hd24devicenamegenerator::getdevicename("<<devicenumber<<")"<< endl; | ||
255 | #endif | ||
256 | if (devicenumber>=this->getnumberofsysdevs()) | ||
257 | { | ||
258 | return new string(getfilename(devicenumber-this->getnumberofsysdevs())); | ||
259 | } | ||
260 | |||
261 | string* devname = new string("//./PHYSICALDRIVE"); | ||
262 | string* devnum = Convert::int2str(devicenumber); | ||
263 | *devname += *devnum; | ||
264 | delete devnum; | ||
265 | return devname; | ||
266 | } | ||
267 | #endif | ||
268 | |||
269 | #ifdef DARWIN | ||
270 | string* hd24devicenamegenerator::getdevicename(__uint32 devicenumber) | ||
271 | { | ||
272 | #if (DEVGENDEBUG==1) | ||
273 | cout << "hd24devicenamegenerator::getdevicename("<<devicenumber<<")"<< endl; | ||
274 | #endif | ||
275 | if (devicenumber>=this->getnumberofsysdevs()) | ||
276 | { | ||
277 | return new string(getfilename(devicenumber-this->getnumberofsysdevs())); | ||
278 | } | ||
279 | |||
280 | string* devname = new string("/dev/disk"); | ||
281 | string* devnum = Convert::int2str(devicenumber); | ||
282 | *devname += *devnum; | ||
283 | delete devnum; | ||
284 | return devname; | ||
285 | } | ||
286 | #endif | ||