File: Filenames.Mod

package info (click to toggle)
oo2c32 1.5.0-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 8,748 kB
  • ctags: 5,415
  • sloc: ansic: 95,007; sh: 473; makefile: 344; perl: 57; lisp: 21
file content (188 lines) | stat: -rw-r--r-- 5,825 bytes parent folder | download | duplicates (6)
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
(* This module is obsolete.  Don't use it. *)
MODULE Filenames;
(* Note: It is not checked whether the concatenated strings fit into the
   variables given for them or not *)

IMPORT 
  Strings, Strings2, Rts;


PROCEDURE LocateCharLast(str: ARRAY OF CHAR; ch: CHAR): INTEGER;
(* Result is the position of the last occurence of 'ch' in the string 'str'.
   If 'ch' does not occur in 'str', then -1 is returned *)
  VAR 
    pos: INTEGER;
  BEGIN
    pos:=Strings.Length(str);
    WHILE (pos >= 0) DO
      IF (str[pos] = ch) THEN
        RETURN(pos);
      ELSE
        DEC(pos);
      END; (* IF *)
    END; (* WHILE *)
    RETURN -1
  END LocateCharLast;

PROCEDURE SplitRChar(str: ARRAY OF CHAR; VAR str1, str2: ARRAY OF CHAR; ch: CHAR);
(* pre : 'str' contains the string to be splited after the rightmost 'ch' *)
(* post: 'str1' contains the left part (including 'ch') of 'str',
         iff occurs(ch,str), otherwise "",
         'str2' contains the right part of 'str'.
*)
(*
  example:
            str = "/aksdf/asdf/gasdfg/esscgd.asdfg"
    result: str2 = "esscgd.asdfg"
            str1 = "/aksdf/asdf/gasdfg/"
*)

  VAR 
    len,pos: INTEGER;
  BEGIN
    len:=Strings.Length(str);
 
    (* search for the rightmost occurence of 'ch' and
       store it's position in 'pos' *)
    pos:=LocateCharLast(str,ch);
    
    COPY(str,str2); (* that has to be done all time *)
    IF (pos >= 0) THEN 
      (* 'ch' occurs in 'str', (str[pos]=ch)=TRUE *)
      COPY(str,str1); (* copy the whole string 'str' to 'str1' *)
      INC(pos); (* we want to split _after_ 'ch' *)
      Strings.Delete(str2,0,pos); (* remove left part from 'str2' *)
      Strings.Delete(str1,pos,(len-pos)); (* remove right part from 'str1' *)
    ELSE (* there is no pathinfo in 'file' *)
      COPY("",str1); (* make 'str1' the empty string *)
    END; (* IF *)
  END SplitRChar;

(******************************)
(* decomposition of filenames *)
(******************************)

PROCEDURE GetPath*(full: ARRAY OF CHAR; VAR path, file: ARRAY OF CHAR);
(*
  pre : "full" contains the (maybe) absolute path to a file.
  post: "file" contains only the filename, "path" the path for it.
  
  example:
    pre : full = "/aksdf/asdf/gasdfg/esscgd.asdfg"
    post: file = "esscgd.asdfg"
          path = "/aksdf/asdf/gasdfg/"
*)

  BEGIN
    SplitRChar(full,path,file,Rts.pathSeperator);
  END GetPath;

PROCEDURE GetExt*(full: ARRAY OF CHAR; VAR file, ext: ARRAY OF CHAR);
  BEGIN
    IF (LocateCharLast(full,Rts.pathSeperator) < LocateCharLast(full,".")) THEN
      (* there is a "real" extension *)
      SplitRChar(full,file,ext,".");
      Strings.Delete(file,Strings.Length(file)-1,1); (* delete "." at the end of 'file' *)
    ELSE
      COPY(full,file);
      COPY("",ext);
    END; (* IF *)
  END GetExt;

PROCEDURE GetFile*(full: ARRAY OF CHAR; VAR file: ARRAY OF CHAR);
(* removes both path & extension from 'full' and stores the result in 'file' *)
(* example:
   GetFile("/tools/public/o2c-1.2/lib/Filenames.Mod",myname)
     results in 
   myname="Filenames"
*)
  VAR 
    dummy: ARRAY 256 OF CHAR; (* that should be enough... *)
  BEGIN
    GetPath(full,dummy,file);
    GetExt(file,file,dummy);
  END GetFile;


(****************************)
(* composition of filenames *)
(****************************)

PROCEDURE AddExt*(VAR full: ARRAY OF CHAR; file, ext: ARRAY OF CHAR);
(* pre : 'file' is a filename
         'ext' is some extension
*)
(* post: 'full' contains 'file'"."'ext', iff 'ext'#"",
         otherwise 'file'
*)
  BEGIN
    COPY(file,full);    
    IF (ext[0] # 0X) THEN 
      (* we only append 'real', i.e. nonempty extensions *)
      Strings2.AppendChar(".", full);
      Strings.Append(ext, full);
    END; (* IF *)
  END AddExt;

PROCEDURE AddPath*(VAR full: ARRAY OF CHAR; path, file: ARRAY OF CHAR);
(* pre : 'file' is a filename
         'path' is a path (will not be interpreted) or ""
*)
(* post: 'full' will contain the contents of 'file' with
         addition of 'path' at the beginning.
*)
  BEGIN
    COPY(file,full);
    IF (path[0] # 0X) THEN
      (* we only add something if there is something... *)
      IF (path[Strings.Length(path) - 1] # Rts.pathSeperator) THEN 
        (* add a seperator, if none is at the end of 'path' *)
        Strings.Insert(Rts.pathSeperator, 0, full);
      END; (* IF *)
      Strings.Insert(path, 0, full)
    END; (* IF *)
  END AddPath;

PROCEDURE BuildFilename*(VAR full: ARRAY OF CHAR; path, file, ext: ARRAY OF CHAR);
(* pre : 'file' is the name of a file, 
         'path' is its path and
         'ext' is the extension to be added 
*)
(* post: 'full' contains concatenation of 'path' with ('file' with 'ext')
*)
 BEGIN
   AddExt(full,file,ext);
   AddPath(full,path,full);
  END BuildFilename;

PROCEDURE ExpandPath*(VAR full: ARRAY OF CHAR; path: ARRAY OF CHAR);
(* Expands "~/" and "~user/" at the beginning of 'path' to it's
   intended strings.
   "~/" will result in the path to the current user's home,
   "~user" will result in the path of "user"'s home. *)
  VAR
    len, posSep, posSuffix: INTEGER;
    suffix, userpath: ARRAY 256 OF CHAR;
    username: ARRAY 32 OF CHAR;
  BEGIN
    COPY (path, full);
    IF (path[0] = "~") THEN (* we have to expand something *)
      posSep := Strings2.PosChar (Rts.pathSeperator, path);
      len := Strings.Length (path);
      IF (posSep < 0) THEN   (* no '/' in file name, just the path *)
        posSep := len;
        posSuffix := len
      ELSE
        posSuffix := posSep+1
      END;
      Strings.Extract (path, posSuffix, len-posSuffix, suffix);
      Strings.Extract (path, 1, posSep-1, username);
      Rts.GetUserHome (userpath, username);
      IF (userpath[0] # 0X) THEN         (* sucessfull search *)
        AddPath (full, userpath, suffix)
      END
    END
  END ExpandPath;

END Filenames.