~ubuntu-branches/ubuntu/vivid/unrar-nonfree/vivid

« back to all changes in this revision

Viewing changes to pathfn.cpp

  • Committer: Package Import Robot
  • Author(s): Martin Meredith
  • Date: 2015-02-03 12:58:01 UTC
  • mfrom: (1.1.18) (5.1.18 sid)
  • Revision ID: package-import@ubuntu.com-20150203125801-od6ev8cqy1er51vz
Tags: 1:5.2.5-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
  while (*DestPtr!=0)
30
30
  {
31
31
    const wchar *s=DestPtr;
32
 
    if (s[0] && IsDriveDiv(s[1]))
 
32
    if (s[0]!=0 && IsDriveDiv(s[1]))
33
33
      s+=2;
34
34
    if (s[0]=='\\' && s[1]=='\\')
35
35
    {
71
71
}
72
72
 
73
73
 
74
 
void SetExt(wchar *Name,const wchar *NewExt)
 
74
void SetExt(wchar *Name,const wchar *NewExt,size_t MaxSize)
75
75
{
76
76
  if (Name==NULL || *Name==0)
77
77
    return;
78
78
  wchar *Dot=GetExt(Name);
79
 
  if (NewExt==NULL)
 
79
  if (Dot!=NULL)
 
80
    *Dot=0;
 
81
  if (NewExt!=NULL)
80
82
  {
81
 
    if (Dot!=NULL)
82
 
      *Dot=0;
 
83
    wcsncatz(Name,L".",MaxSize);
 
84
    wcsncatz(Name,NewExt,MaxSize);
83
85
  }
84
 
  else
85
 
    if (Dot==NULL)
86
 
    {
87
 
      wcscat(Name,L".");
88
 
      wcscat(Name,NewExt);
89
 
    }
90
 
    else
91
 
      wcscpy(Dot+1,NewExt);
92
86
}
93
87
 
94
88
 
95
89
#ifndef SFX_MODULE
96
 
void SetSFXExt(wchar *SFXName)
 
90
void SetSFXExt(wchar *SFXName,size_t MaxSize)
97
91
{
98
92
  if (SFXName==NULL || *SFXName==0)
99
93
    return;
100
94
 
101
95
#ifdef _UNIX
102
 
  SetExt(SFXName,L"sfx");
 
96
  SetExt(SFXName,L"sfx",MaxSize);
103
97
#endif
104
98
 
105
99
#if defined(_WIN_ALL) || defined(_EMX)
106
 
  SetExt(SFXName,L"exe");
 
100
  SetExt(SFXName,L"exe",MaxSize);
107
101
#endif
108
102
}
109
103
#endif
110
104
 
111
105
 
 
106
// 'Ext' is an extension with the leading dot, like L".rar".
112
107
wchar *GetExt(const wchar *Name)
113
108
{
114
109
  return Name==NULL ? NULL:wcsrchr(PointToName(Name),'.');
162
157
{
163
158
  size_t Length=wcslen(Path);
164
159
  if (Length>0 && Path[Length-1]!=CPATHDIVIDER && Length+1<MaxLength)
165
 
    wcscat(Path,PATHDIVIDERW);
 
160
    wcscat(Path,SPATHDIVIDER);
166
161
}
167
162
 
168
163
 
201
196
 
202
197
 
203
198
#if defined(_WIN_ALL) && !defined(SFX_MODULE)
204
 
static void GetAppDataPath(wchar *Path,size_t MaxSize,bool Create)
 
199
bool GetAppDataPath(wchar *Path,size_t MaxSize,bool Create)
205
200
{
206
201
  LPMALLOC g_pMalloc;
207
202
  SHGetMalloc(&g_pMalloc);
217
212
    if (!Success && Create)
218
213
      Success=MakeDir(Path,false,0)==MKDIR_SUCCESS;
219
214
  }
220
 
  if (!Success)
221
 
  {
222
 
    GetModuleFileName(NULL,Path,(DWORD)MaxSize);
223
 
    RemoveNameFromPath(Path);
224
 
  }
225
215
  g_pMalloc->Free(ppidl);
 
216
  return Success;
226
217
}
227
218
#endif
228
219
 
242
233
  }
243
234
 
244
235
  if (*Path==0 || !FileExist(Path))
245
 
    GetAppDataPath(Path,MaxSize,Create);
 
236
    if (!GetAppDataPath(Path,MaxSize,Create))
 
237
    {
 
238
      GetModuleFileName(NULL,Path,(DWORD)MaxSize);
 
239
      RemoveNameFromPath(Path);
 
240
    }
246
241
}
247
242
#endif
248
243
 
424
419
#ifndef _UNIX
425
420
    if (s-Name>1 && *s==':')
426
421
      *s='_';
427
 
    if ((*s==' ' || *s=='.') && IsPathDiv(s[1]))
 
422
    // Remove ' ' and '.' before path separator, but allow .\ and ..\.
 
423
    if ((*s==' ' || *s=='.' && s>Name && !IsPathDiv(s[-1]) && s[-1]!='.') && IsPathDiv(s[1]))
428
424
      *s='_';
429
425
#endif
430
426
  }
440
436
#ifndef _UNIX
441
437
    if (s-Name>1 && *s==':')
442
438
      *s='_';
443
 
    if ((*s==' ' || *s=='.') && IsPathDiv(s[1]))
 
439
#if 0  // We already can create such files.
 
440
    // Remove ' ' and '.' before path separator, but allow .\ and ..\.
 
441
    if (IsPathDiv(s[1]) && (*s==' ' || *s=='.' && s>Name &&
 
442
        !IsPathDiv(s[-1]) && (s[-1]!='.' || s>Name+1 && !IsPathDiv(s[-2]))))
444
443
      *s='_';
445
444
#endif
446
 
  }
447
 
}
448
 
 
449
 
 
450
 
char* UnixSlashToDos(char *SrcName,char *DestName,size_t MaxLength)
451
 
{
452
 
  if (DestName!=NULL && DestName!=SrcName)
453
 
    if (strlen(SrcName)>=MaxLength)
454
 
    {
455
 
      *DestName=0;
456
 
      return DestName;
457
 
    }
458
 
    else
459
 
      strcpy(DestName,SrcName);
460
 
  for (char *s=SrcName;*s!=0;s=charnext(s))
461
 
  {
462
 
    if (*s=='/')
463
 
      if (DestName==NULL)
464
 
        *s='\\';
465
 
      else
466
 
        DestName[s-SrcName]='\\';
467
 
  }
468
 
  return DestName==NULL ? SrcName:DestName;
469
 
}
470
 
 
471
 
 
472
 
char* DosSlashToUnix(char *SrcName,char *DestName,size_t MaxLength)
473
 
{
474
 
  if (DestName!=NULL && DestName!=SrcName)
475
 
    if (strlen(SrcName)>=MaxLength)
476
 
    {
477
 
      *DestName=0;
478
 
      return DestName;
479
 
    }
480
 
    else
481
 
      strcpy(DestName,SrcName);
482
 
  for (char *s=SrcName;*s!=0;s=charnext(s))
483
 
  {
484
 
    if (*s=='\\')
485
 
      if (DestName==NULL)
486
 
        *s='/';
487
 
      else
488
 
        DestName[s-SrcName]='/';
489
 
  }
490
 
  return DestName==NULL ? SrcName:DestName;
491
 
}
492
 
 
493
 
 
494
 
wchar* UnixSlashToDos(wchar *SrcName,wchar *DestName,size_t MaxLength)
495
 
{
496
 
  if (DestName!=NULL && DestName!=SrcName)
497
 
    if (wcslen(SrcName)>=MaxLength)
498
 
    {
499
 
      *DestName=0;
500
 
      return DestName;
501
 
    }
502
 
    else
503
 
      wcscpy(DestName,SrcName);
504
 
  for (wchar *s=SrcName;*s!=0;s++)
505
 
  {
506
 
    if (*s=='/')
507
 
      if (DestName==NULL)
508
 
        *s='\\';
509
 
      else
510
 
        DestName[s-SrcName]='\\';
511
 
  }
512
 
  return DestName==NULL ? SrcName:DestName;
513
 
}
514
 
 
515
 
 
516
 
wchar* DosSlashToUnix(wchar *SrcName,wchar *DestName,size_t MaxLength)
517
 
{
518
 
  if (DestName!=NULL && DestName!=SrcName)
519
 
    if (wcslen(SrcName)>=MaxLength)
520
 
    {
521
 
      *DestName=0;
522
 
      return DestName;
523
 
    }
524
 
    else
525
 
      wcscpy(DestName,SrcName);
526
 
  for (wchar *s=SrcName;*s!=0;s++)
527
 
  {
528
 
    if (*s=='\\')
529
 
      if (DestName==NULL)
530
 
        *s='/';
531
 
      else
532
 
        DestName[s-SrcName]='/';
533
 
  }
534
 
  return DestName==NULL ? SrcName:DestName;
 
445
#endif
 
446
  }
 
447
}
 
448
 
 
449
 
 
450
void UnixSlashToDos(const char *SrcName,char *DestName,size_t MaxLength)
 
451
{
 
452
  size_t Copied=0;
 
453
  for (;Copied<MaxLength-1 && SrcName[Copied]!=0;Copied++)
 
454
    DestName[Copied]=SrcName[Copied]=='/' ? '\\':SrcName[Copied];
 
455
  DestName[Copied]=0;
 
456
}
 
457
 
 
458
 
 
459
void DosSlashToUnix(const char *SrcName,char *DestName,size_t MaxLength)
 
460
{
 
461
  size_t Copied=0;
 
462
  for (;Copied<MaxLength-1 && SrcName[Copied]!=0;Copied++)
 
463
    DestName[Copied]=SrcName[Copied]=='\\' ? '/':SrcName[Copied];
 
464
  DestName[Copied]=0;
 
465
}
 
466
 
 
467
 
 
468
void UnixSlashToDos(const wchar *SrcName,wchar *DestName,size_t MaxLength)
 
469
{
 
470
  size_t Copied=0;
 
471
  for (;Copied<MaxLength-1 && SrcName[Copied]!=0;Copied++)
 
472
    DestName[Copied]=SrcName[Copied]=='/' ? '\\':SrcName[Copied];
 
473
  DestName[Copied]=0;
 
474
}
 
475
 
 
476
 
 
477
void DosSlashToUnix(const wchar *SrcName,wchar *DestName,size_t MaxLength)
 
478
{
 
479
  size_t Copied=0;
 
480
  for (;Copied<MaxLength-1 && SrcName[Copied]!=0;Copied++)
 
481
    DestName[Copied]=SrcName[Copied]=='\\' ? '/':SrcName[Copied];
 
482
  DestName[Copied]=0;
535
483
}
536
484
 
537
485
 
593
541
}
594
542
 
595
543
 
 
544
bool IsFullRootPath(const wchar *Path)
 
545
{
 
546
  return IsFullPath(Path) || IsPathDiv(Path[0]);
 
547
}
 
548
 
 
549
 
596
550
bool IsDiskLetter(const wchar *Path)
597
551
{
598
552
  wchar Letter=etoupperw(Path[0]);
642
596
 
643
597
#if !defined(SFX_MODULE) && !defined(SETUP)
644
598
// Get the name of first volume. Return the leftmost digit of volume number.
645
 
wchar* VolNameToFirstName(const wchar *VolName,wchar *FirstName,bool NewNumbering)
 
599
wchar* VolNameToFirstName(const wchar *VolName,wchar *FirstName,size_t MaxSize,bool NewNumbering)
646
600
{
647
601
  if (FirstName!=VolName)
648
 
    wcscpy(FirstName,VolName);
 
602
    wcsncpyz(FirstName,VolName,MaxSize);
649
603
  wchar *VolNumStart=FirstName;
650
604
  if (NewNumbering)
651
605
  {
668
622
  else
669
623
  {
670
624
    // Old volume numbering scheme. Just set the extension to ".rar".
671
 
    SetExt(FirstName,L"rar");
 
625
    SetExt(FirstName,L"rar",MaxSize);
672
626
    VolNumStart=GetExt(FirstName);
673
627
  }
674
628
  if (!FileExist(FirstName))
677
631
    // check if volume with same name and any other extension is available.
678
632
    // It can help in case of *.exe or *.sfx first volume.
679
633
    wchar Mask[NM];
680
 
    wcscpy(Mask,FirstName);
681
 
    SetExt(Mask,L"*");
 
634
    wcsncpyz(Mask,FirstName,ASIZE(Mask));
 
635
    SetExt(Mask,L"*",ASIZE(Mask));
682
636
    FindFile Find;
683
637
    Find.SetMask(Mask);
684
638
    FindData FD;
687
641
      Archive Arc;
688
642
      if (Arc.Open(FD.Name,0) && Arc.IsArchive(true) && Arc.FirstVolume)
689
643
      {
690
 
        wcscpy(FirstName,FD.Name);
 
644
        wcsncpyz(FirstName,FD.Name,MaxSize);
691
645
        break;
692
646
      }
693
647
    }
698
652
 
699
653
 
700
654
#ifndef SFX_MODULE
701
 
static void GenArcName(wchar *ArcName,wchar *GenerateMask,uint ArcNumber,bool &ArcNumPresent)
 
655
static void GenArcName(wchar *ArcName,const wchar *GenerateMask,uint ArcNumber,bool &ArcNumPresent)
702
656
{
703
657
  bool Prefix=false;
704
658
  if (*GenerateMask=='+')
821
775
    }
822
776
    const wchar *ChPtr=wcschr(MaskChars,toupperw(Mask[I]));
823
777
    if (ChPtr==NULL || QuoteMode)
 
778
    {
824
779
      DateText[J]=Mask[I];
 
780
#ifdef _WIN_ALL
 
781
      // We do not allow ':' in Windows because of NTFS streams.
 
782
      // Users had problems after specifying hh:mm mask.
 
783
      if (DateText[J]==':')
 
784
        DateText[J]='_';
 
785
#endif
 
786
    }
825
787
    else
826
788
    {
827
789
      size_t FieldPos=ChPtr-MaskChars;
856
818
}
857
819
 
858
820
 
859
 
void GenerateArchiveName(wchar *ArcName,size_t MaxSize,wchar *GenerateMask,bool Archiving)
 
821
void GenerateArchiveName(wchar *ArcName,size_t MaxSize,const wchar *GenerateMask,bool Archiving)
860
822
{
861
823
  // Must be enough space for archive name plus all stuff in mask plus
862
824
  // extra overhead produced by mask 'N' (archive number) characters.
973
935
        return false;
974
936
      wcsncpy(Dest,Prefix,PrefixLength);
975
937
      wcscpy(Dest+PrefixLength,CurDir);
 
938
 
 
939
      if (Src[0]=='.' && IsPathDiv(Src[1])) // Remove leading .\ in pathname.
 
940
        Src+=2;
 
941
 
976
942
      wcsncatz(Dest,Src,MaxSize);
977
943
      return true;
978
944
    }