summaryrefslogtreecommitdiff
path: root/src/FWOSDriver/FWOSDriver_Driver.cxx
blob: b7b78ec039489b5b2d9315d62c16f02764791e5a (plain)
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
#include <FWOSDriver_Driver.ixx>
#include <TCollection_ExtendedString.hxx>
#include <OSD_Path.hxx>
#include <OSD_Directory.hxx>
#include <OSD_Protection.hxx>
#include <OSD_SingleProtection.hxx>
#include <OSD_File.hxx>
#include <OSD_FileNode.hxx>
#include <UTL.hxx>
#include <TCollection_ExtendedString.hxx>
#ifdef WNT
#include <tchar.h>
#endif  // WNT


static void PutSlash (TCollection_ExtendedString& anXSTRING) {
#ifdef WNT
  anXSTRING+="\\";
#else
  anXSTRING+="/";
#endif  // WNT
}

FWOSDriver_Driver::FWOSDriver_Driver() {}


Standard_Boolean FWOSDriver_Driver::Find(const TCollection_ExtendedString& aFolder, const TCollection_ExtendedString& aName, const TCollection_ExtendedString& aVersion) {

  OSD_Path thePath=UTL::Path(aFolder);
  OSD_Directory theDirectory(thePath);
  if(theDirectory.Exists()) {
    TCollection_ExtendedString f(aFolder);
    PutSlash(f);
    f+=aName;
    OSD_Path p2 = UTL::Path(f);
    OSD_File theFile(p2);
    return theFile.Exists();
  }
  return Standard_False;
}

Standard_Boolean FWOSDriver_Driver::HasReadPermission(const TCollection_ExtendedString& aFolder, const TCollection_ExtendedString& aName, const TCollection_ExtendedString& aVersion) {


  OSD_SingleProtection theProtection=OSD_File(UTL::Path(Concatenate(aFolder,aName))).Protection().User();
  switch (theProtection) {
    case OSD_None:
    case OSD_R:
    case OSD_RW:
    case OSD_RX:
    case OSD_WX:
    case OSD_RWX:
    case OSD_RD:
    case OSD_RWD:
    case OSD_RXD:
    case OSD_RWXD:
      return Standard_True;
    default:
      break;
    }
  return Standard_False;
}

Handle(CDM_MetaData) FWOSDriver_Driver::MetaData(const TCollection_ExtendedString& aFolder, const TCollection_ExtendedString& aName, const TCollection_ExtendedString& aVersion) {
  TCollection_ExtendedString p = Concatenate(aFolder,aName);
  return CDM_MetaData::LookUp(aFolder,aName,p,p,UTL::IsReadOnly(p));
}

Handle(CDM_MetaData) FWOSDriver_Driver::CreateMetaData(const Handle(CDM_Document)& aDocument,const TCollection_ExtendedString& aFileName) {
  return CDM_MetaData::LookUp(aDocument->RequestedFolder(),aDocument->RequestedName(),Concatenate(aDocument->RequestedFolder(),aDocument->RequestedName()),aFileName,UTL::IsReadOnly(aFileName));
}
TCollection_ExtendedString FWOSDriver_Driver::BuildFileName(const Handle(CDM_Document)& aDocument) {

  TCollection_ExtendedString retstr = TCollection_ExtendedString(aDocument->RequestedFolder());
  PutSlash(retstr);
  retstr += aDocument->RequestedName();
  return retstr;
}
Standard_Boolean FWOSDriver_Driver::FindFolder(const TCollection_ExtendedString& aFolder) {
  
  OSD_Path thePath=UTL::Path(aFolder);
  OSD_Directory theDirectory(thePath);
  return theDirectory.Exists();
}



TCollection_ExtendedString FWOSDriver_Driver::Concatenate(const TCollection_ExtendedString& aFolder, const TCollection_ExtendedString& aName) {
  TCollection_ExtendedString ff(aFolder);
  ff = "";
  ff += aFolder;
  PutSlash(ff);
  ff+=aName;
  return ff;
}

TCollection_ExtendedString FWOSDriver_Driver::DefaultFolder() {
  TCollection_ExtendedString theDefaultFolder;
  if (theDefaultFolder.Length() == 0) {
    
#ifdef WNT
    TCollection_ExtendedString hd=UTL::xgetenv("HOMEDRIVE");
    if(hd.Length() != NULL) {
      theDefaultFolder=hd;
      theDefaultFolder+=UTL::xgetenv("HOMEPATH");
    }
    else {
      theDefaultFolder=UTL::xgetenv("TEMP");
      if(theDefaultFolder.Length()==0) Standard_Failure::Raise("cannot determine default folder; HOMEDRIVE and TEMP are undefined");
    }
#else
    TCollection_ExtendedString home=UTL::xgetenv("HOME");
    if(home.Length() !=0)
      theDefaultFolder =  home;
    else
      theDefaultFolder= TCollection_ExtendedString("/tmp");
#endif
  }
  return theDefaultFolder;
}

Handle(CDM_MetaData) FWOSDriver_Driver::BuildMetaData(const TCollection_ExtendedString& aFileName) {

  OSD_Path p = UTL::Path(aFileName);
  
  TCollection_ExtendedString f = UTL::Trek(p);
  TCollection_ExtendedString n = UTL::Name(p);
  n +=".";
  n += UTL::Extension(p);

  return CDM_MetaData::LookUp(f,n,aFileName,aFileName,UTL::IsReadOnly(aFileName));
}

TCollection_ExtendedString FWOSDriver_Driver::SetName(const Handle(CDM_Document)& aDocument, const TCollection_ExtendedString& aName)  {
  
  TCollection_ExtendedString xn(aName);
  
  TCollection_ExtendedString n(xn);
  // file name may have spaces
  //n.RemoveAll(' ');
  
  TCollection_ExtendedString e (aDocument->FileExtension());
  if (e.Length() > 0) {
    e.Insert(1, '.');
    Standard_Integer ln = n.Length();
    Standard_Integer le = e.Length();
    Standard_Boolean ExtensionIsAlreadyThere = Standard_False;
    if(ln>=le) {
      Standard_Integer ind=n.SearchFromEnd(e);
      ExtensionIsAlreadyThere = ind+le-1==ln;
    }
    if(!ExtensionIsAlreadyThere) n+=e;
  }
  return n;
}