1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
|
// THIS FILE IS MODIFIED TO WORK WITH XBMC
#include "rar.hpp"
#include "utils/URIUtils.h"
char* PointToName(const char *Path)
{
//const char *Found=NULL;
for (const char *s=&Path[strlen(Path)-1];s>=Path;s--)
if (IsPathDiv(*s))
return (char*)(s+1);
// if (Found!=NULL)
// return((char*)Found);
return (char*)((*Path && IsDriveDiv(Path[1]) && charnext(Path)==Path+1) ? Path+2:Path);
}
/*
Dumbass broken routine!!
Not searching in reverse to strip off filename portion of path!
{
const char *Found=NULL;
for (const char *s=Path;*s!=0;s=charnext(s))
if (IsPathDiv(*s))
Found=(char*)(s+1);
if (Found!=NULL)
return((char*)Found);
return (char*)((*Path && IsDriveDiv(Path[1]) && charnext(Path)==Path+1) ? Path+2:Path);
}
*/
wchar* PointToName(const wchar *Path)
{
for (int I=strlenw(Path)-1;I>=0;I--)
if (IsPathDiv(Path[I]))
return (wchar*)&Path[I+1];
return (wchar*)((*Path && IsDriveDiv(Path[1])) ? Path+2:Path);
}
char* PointToLastChar(const char *Path)
{
for (const char *s=Path,*p=Path;;p=s,s=charnext(s))
if (*s==0)
return((char *)p);
}
char* ConvertPath(const char *SrcPath,char *DestPath)
{
const char *DestPtr=SrcPath;
for (const char *s=DestPtr;*s!=0;s++)
if (IsPathDiv(s[0]) && s[1]=='.' && s[2]=='.' && IsPathDiv(s[3]))
DestPtr=s+4;
while (*DestPtr)
{
const char *s=DestPtr;
if (s[0] && IsDriveDiv(s[1]))
s+=2;
if (s[0]=='\\' && s[1]=='\\')
{
const char *Slash=strchr(s+2,'\\');
if (Slash!=NULL && (Slash=strchr(Slash+1,'\\'))!=NULL)
s=Slash+1;
}
for (const char *t=s;*t!=0;t++)
if (IsPathDiv(*t))
s=t+1;
else
if (*t!='.')
break;
if (s==DestPtr)
break;
DestPtr=s;
}
if (DestPath!=NULL)
{
char TmpStr[NM];
strncpy(TmpStr,DestPtr,sizeof(TmpStr)-1);
strcpy(DestPath,TmpStr);
}
return((char *)DestPtr);
}
wchar* ConvertPath(const wchar *SrcPath,wchar *DestPath)
{
const wchar *DestPtr=SrcPath;
for (const wchar *s=DestPtr;*s!=0;s++)
if (IsPathDiv(s[0]) && s[1]=='.' && s[2]=='.' && IsPathDiv(s[3]))
DestPtr=s+4;
while (*DestPtr)
{
const wchar *s=DestPtr;
if (s[0] && IsDriveDiv(s[1]))
s+=2;
if (s[0]=='\\' && s[1]=='\\')
{
const wchar *Slash=strchrw(s+2,'\\');
if (Slash!=NULL && (Slash=strchrw(Slash+1,'\\'))!=NULL)
s=Slash+1;
}
for (const wchar *t=s;*t!=0;t++)
if (IsPathDiv(*t))
s=t+1;
else
if (*t!='.')
break;
if (s==DestPtr)
break;
DestPtr=s;
}
if (DestPath!=NULL)
{
wchar TmpStr[NM];
strncpyw(TmpStr,DestPtr,sizeof(TmpStr)/sizeof(TmpStr[0])-1);
strcpyw(DestPath,TmpStr);
}
return((wchar *)DestPtr);
}
void SetExt(char *Name,const char *NewExt)
{
char *Dot=GetExt(Name);
if (NewExt==NULL)
{
if (Dot!=NULL)
*Dot=0;
}
else
if (Dot==NULL)
{
strcat(Name,".");
strcat(Name,NewExt);
}
else
strcpy(Dot+1,NewExt);
}
#ifndef SFX_MODULE
void SetExt(wchar *Name,const wchar *NewExt)
{
if (Name==NULL || *Name==0)
return;
wchar *Dot=GetExt(Name);
if (NewExt==NULL)
{
if (Dot!=NULL)
*Dot=0;
}
else
if (Dot==NULL)
{
strcatw(Name,L".");
strcatw(Name,NewExt);
}
else
strcpyw(Dot+1,NewExt);
}
#endif
#ifndef SFX_MODULE
void SetSFXExt(char *SFXName)
{
#ifdef _UNIX
SetExt(SFXName,"sfx");
#endif
#if defined(_WIN_32) || defined(_EMX)
SetExt(SFXName,"exe");
#endif
}
#endif
#ifndef SFX_MODULE
void SetSFXExt(wchar *SFXName)
{
if (SFXName==NULL || *SFXName==0)
return;
#ifdef _UNIX
SetExt(SFXName,L"sfx");
#endif
#if defined(_WIN_32) || defined(_EMX)
SetExt(SFXName,L"exe");
#endif
}
#endif
char *GetExt(const char *Name)
{
CStdString strExtension = URIUtils::GetExtension(Name);
return((char *)strstr((char *)Name,strExtension.c_str()));
}
wchar *GetExt(const wchar *Name)
{
CStdString strExtension = URIUtils::GetExtension(Name);
return((wchar *)wcsstr((wchar_t *)Name,CStdStringW(strExtension).c_str()));
}
bool CmpExt(const char *Name,const char *Ext)
{
char *NameExt=GetExt(Name);
return(NameExt!=NULL && stricomp(NameExt+1,Ext)==0);
}
bool IsWildcard(const char *Str,const wchar *StrW)
{
if (StrW!=NULL && *StrW!=0)
return(strpbrkw(StrW,L"*?")!=NULL);
return(Str==NULL ? false:strpbrk(Str,"*?")!=NULL);
}
bool IsPathDiv(int Ch)
{
#if defined(_WIN_32) || defined(_EMX)
return(Ch=='\\' || Ch=='/');
#else
return(Ch==CPATHDIVIDER);
#endif
}
bool IsDriveDiv(int Ch)
{
#ifdef _UNIX
return(false);
#else
return(Ch==':');
#endif
}
int GetPathDisk(const char *Path)
{
if (IsDiskLetter(Path))
return(toupper(*Path)-'A');
else
return(-1);
}
void AddEndSlash(char *Path)
{
char *LastChar=PointToLastChar(Path);
if (*LastChar!=0 && *LastChar!=CPATHDIVIDER)
strcat(LastChar,PATHDIVIDER);
}
void AddEndSlash(wchar *Path)
{
int Length=strlenw(Path);
if (Length>0 && Path[Length-1]!=CPATHDIVIDER)
strcatw(Path,PATHDIVIDERW);
}
void GetFilePath(const char *FullName,char *Path)
{
int PathLength=PointToName(FullName)-FullName;
strncpy(Path,FullName,PathLength);
Path[PathLength]=0;
}
void GetFilePath(const wchar *FullName,wchar *Path)
{
const wchar *PathPtr=/*(*FullName && IsDriveDiv(FullName[1])) ? FullName+2:*/FullName;
int PathLength=PointToName(FullName)-FullName;
strncpyw(Path,PathPtr,PathLength);
Path[PathLength]=0;
}
void RemoveNameFromPath(char *Path)
{
char *Name=PointToName(Path);
if (Name>=Path+2 && (!IsDriveDiv(Path[1]) || Name>=Path+4))
Name--;
*Name=0;
}
#ifndef SFX_MODULE
void RemoveNameFromPath(wchar *Path)
{
wchar *Name=PointToName(Path);
if (Name>=Path+2 && (!IsDriveDiv(Path[1]) || Name>=Path+4))
Name--;
*Name=0;
}
#endif
#ifndef SFX_MODULE
bool EnumConfigPaths(char *Path,int Number)
{
#ifdef _EMX
static char RARFileName[NM];
if (Number==-1)
strcpy(RARFileName,Path);
if (Number!=0)
return(false);
if (_osmode==OS2_MODE)
{
PTIB ptib;
PPIB ppib;
DosGetInfoBlocks(&ptib, &ppib);
DosQueryModuleName(ppib->pib_hmte,NM,Path);
}
else
strcpy(Path,RARFileName);
RemoveNameFromPath(Path);
return(true);
#elif defined(_UNIX)
if (Number==0)
{
char *EnvStr=getenv("HOME");
if (EnvStr==NULL)
return(false);
strncpy(Path,EnvStr,NM);
Path[NM-1]=0;
return(true);
}
static const char *AltPath[]={
"/etc","/usr/lib","/usr/local/lib","/usr/local/etc"
};
Number--;
if (Number<0 || Number>=(int)(sizeof(AltPath)/sizeof(AltPath[0])))
return(false);
strcpy(Path,AltPath[Number]);
return(true);
#elif defined(_WIN_32)
if (Number!=0)
return(false);
#if !defined(TARGET_POSIX)
GetModuleFileName(NULL,Path,NM);
RemoveNameFromPath(Path);
#endif
return(true);
#else
return(false);
#endif
}
#endif
#ifndef SFX_MODULE
void GetConfigName(const char *Name,char *FullName, bool CheckExist)
{
for (int I=0;EnumConfigPaths(FullName,I);I++)
{
AddEndSlash(FullName);
strcat(FullName,Name);
if (!CheckExist || WildFileExist(FullName))
break;
}
}
#endif
char* GetVolNumPart(char *ArcName)
{
char *ChPtr=ArcName+strlen(ArcName)-1;
while (!isdigit(*ChPtr) && ChPtr>ArcName)
ChPtr--;
char *NumPtr=ChPtr;
while (isdigit(*NumPtr) && NumPtr>ArcName)
NumPtr--;
while (NumPtr>ArcName && *NumPtr!='.')
{
if (isdigit(*NumPtr))
{
char *Dot=strchrd(PointToName(ArcName),'.');
if (Dot!=NULL && Dot<NumPtr)
ChPtr=NumPtr;
break;
}
NumPtr--;
}
return(ChPtr);
}
void NextVolumeName(char *ArcName,bool OldNumbering)
{
char *ChPtr;
if ((ChPtr=GetExt(ArcName))==NULL)
{
strcat(ArcName,".rar");
ChPtr=GetExt(ArcName);
}
else
if (ChPtr[1]==0 || stricomp(ChPtr+1,"exe")==0 || stricomp(ChPtr+1,"sfx")==0)
strcpy(ChPtr+1,"rar");
if (!OldNumbering)
{
ChPtr=GetVolNumPart(ArcName);
while ((++(*ChPtr))=='9'+1)
{
*ChPtr='0';
ChPtr--;
if (ChPtr<ArcName || !isdigit(*ChPtr))
{
for (char *EndPtr=ArcName+strlen(ArcName);EndPtr!=ChPtr;EndPtr--)
*(EndPtr+1)=*EndPtr;
*(ChPtr+1)='1';
break;
}
}
}
else
if (!isdigit(*(ChPtr+2)) || !isdigit(*(ChPtr+3)))
strcpy(ChPtr+2,"00");
else
{
ChPtr+=3;
while ((++(*ChPtr))=='9'+1)
if (*(ChPtr-1)=='.')
{
*ChPtr='A';
break;
}
else
{
*ChPtr='0';
ChPtr--;
}
}
}
bool IsNameUsable(const char *Name)
{
// only for xbox
if ( Name == NULL) return false;
char cIllegalChars[] = "<>=?;\"*+,/|";
unsigned int iIllegalCharSize = strlen(cIllegalChars);
bool isIllegalChar;
unsigned int iSize = strlen(Name);
if(iSize > 42) return false;
for (unsigned int i = 0; i < iSize; i++)
{
isIllegalChar = false;
// check for illegal chars
for (unsigned j = 0; j < iIllegalCharSize; j++)
if (Name[i] == cIllegalChars[j]) isIllegalChar = true;
// FATX only allows chars from 32 till 127
if (isIllegalChar == true || Name[i] < 32 || Name[i] > 126) return false;
}
return true;
/*
#ifndef _UNIX
if (Name[0] && Name[1] && strchr(Name+2,':')!=NULL)
return(false);
#endif
if (strlen(Name) < 43) return 1;
else return 0;
return(*Name!=0 && strpbrk(Name,"?*<>|")==NULL);
*/
}
void MakeNameUsable(char *Name, bool bKeepExtension, bool IsFATX)
{
// Changed to be compatible with xbmc's MakeLegalFileName function
// (xbox only)
if ( Name == NULL) return;
char cIllegalChars[] = "<>=?;\"*+,/|";
unsigned int iIllegalCharSize = strlen(cIllegalChars);
bool isIllegalChar;
unsigned int iSize = strlen(Name);
unsigned int iNewStringSize = 0;
char* strNewString = new char[iSize + 1];
// only copy the legal characters to the new filename
for (unsigned int i = 0; i < iSize; i++)
{
isIllegalChar = false;
// check for illigal chars
for (unsigned j = 0; j < iIllegalCharSize; j++)
if (Name[i] == cIllegalChars[j]) isIllegalChar = true;
// FATX only allows chars from 32 till 127
if (isIllegalChar == false &&
Name[i] > 31 && Name[i] < 127) strNewString[iNewStringSize++] = Name[i];
}
strNewString[iNewStringSize] = '\0';
if (IsFATX)
{
// since we can only write to samba shares and hd, we assume this has to be a fatx filename
// thus we have to strip it down to 42 chars (samba doesn't have this limitation)
char* FileName = PointToName(strNewString);
int iFileNameSize = strlen(FileName);
// no need to keep the extension, just strip it down to 42 characters
if (iFileNameSize > 42 && bKeepExtension == false) FileName[42] = '\0';
// we want to keep the extension
else if (iFileNameSize > 42 && bKeepExtension == true)
{
char strExtension[42];
unsigned int iExtensionLength = iFileNameSize - (strrchr(FileName, '.') - FileName);
strcpy(strExtension, (FileName + iFileNameSize - iExtensionLength));
strcpy(FileName + (42 - iExtensionLength), strExtension);
}
}
strcpy(Name, strNewString);
delete[] strNewString;
}
char* UnixSlashToDos(char *SrcName,char *DestName,uint MaxLength)
{
if (DestName!=NULL && DestName!=SrcName)
strcpy(DestName,SrcName);
for (char *s=SrcName;*s!=0;s=charnext(s))
{
if (*s=='/')
{
if (DestName==NULL)
*s='\\';
else
DestName[s-SrcName]='\\';
}
}
return(DestName==NULL ? SrcName:DestName);
}
char* DosSlashToUnix(char *SrcName,char *DestName,uint MaxLength)
{
if (DestName!=NULL && DestName!=SrcName)
{
if (strlen(SrcName)>=MaxLength)
{
*DestName=0;
return(DestName);
}
else
strcpy(DestName,SrcName);
}
for (char *s=SrcName;*s!=0;s=charnext(s))
{
if (*s=='\\')
{
if (DestName==NULL)
*s='/';
else
DestName[s-SrcName]='/';
}
}
return(DestName==NULL ? SrcName:DestName);
}
bool IsFullPath(const char *Path)
{
char PathOnly[NM];
GetFilePath(Path,PathOnly);
if (IsWildcard(PathOnly))
return(true);
#if defined(_WIN_32) || defined(_EMX)
return(Path[0]=='\\' && Path[1]=='\\' ||
IsDiskLetter(Path) && IsPathDiv(Path[2]));
#else
return(IsPathDiv(Path[0]));
#endif
}
bool IsDiskLetter(const char *Path)
{
char Letter=toupper(Path[0]);
return(Letter>='A' && Letter<='Z' && IsDriveDiv(Path[1]));
}
void GetPathRoot(const char *Path,char *Root)
{
*Root=0;
if (IsDiskLetter(Path))
sprintf(Root,"%c:\\",*Path);
else
if (Path[0]=='\\' && Path[1]=='\\')
{
const char *Slash=strchr(Path+2,'\\');
if (Slash!=NULL)
{
int Length;
if ((Slash=strchr(Slash+1,'\\'))!=NULL)
Length=Slash-Path+1;
else
Length=strlen(Path);
strncpy(Root,Path,Length);
Root[Length]=0;
}
}
}
int ParseVersionFileName(char *Name,wchar *NameW,bool Truncate)
{
int Version=0;
char *VerText=strrchrd(Name,';');
if (VerText!=NULL)
{
Version=atoi(VerText+1);
if (Truncate)
*VerText=0;
}
if (NameW!=NULL)
{
wchar *VerTextW=strrchrw(NameW,';');
if (VerTextW!=NULL)
{
if (Version==0)
Version=atoiw(VerTextW+1);
if (Truncate)
*VerTextW=0;
}
}
return(Version);
}
#ifndef SFX_MODULE
char* VolNameToFirstName(const char *VolName,char *FirstName,bool NewNumbering)
{
if (FirstName!=VolName)
strcpy(FirstName,VolName);
char *VolNumStart=FirstName;
if (NewNumbering)
{
int N='1';
for (char *ChPtr=GetVolNumPart(FirstName);ChPtr>FirstName;ChPtr--)
if (isdigit(*ChPtr))
{
*ChPtr=N;
N='0';
}
else
if (N=='0')
{
VolNumStart=ChPtr+1;
break;
}
}
else
{
SetExt(FirstName,"rar");
VolNumStart=GetExt(FirstName);
}
if (!FileExist(FirstName))
{
char Mask[NM];
strcpy(Mask,FirstName);
SetExt(Mask,"*");
FindFile Find;
Find.SetMask(Mask);
struct FindData FD;
while (Find.Next(&FD))
{
Archive Arc;
if (Arc.Open(FD.Name,FD.NameW) && Arc.IsArchive(true) && !Arc.NotFirstVolume)
{
strcpy(FirstName,FD.Name);
break;
}
}
}
return(VolNumStart);
}
#endif
wchar* GetWideName(const char *Name,const wchar *NameW,wchar *DestW)
{
if (NameW!=NULL && *NameW!=0)
{
if (DestW!=NameW)
strcpyw(DestW,NameW);
}
else
CharToWide(Name,DestW);
return(DestW);
}
|