This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
/
s_util.cpp
249 lines (228 loc) · 6.83 KB
/
s_util.cpp
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
/* s_util.cpp
*
* Copyright (c) 1994-1996, Marko Macek
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
*/
#include "fte.h"
#define BUF_SZ (sizeof(FileBuffer))
extern RxNode *CompletionFilter;
// should use DosCopy under OS/2...
int copyfile(char *f1, char *f2) { // from F1 to F2
void *buffer;
int fd1, fd2;
int rd;
buffer = FileBuffer;
if ((fd1 = open(f1, O_RDONLY | O_BINARY)) == -1)
return -1;
if ((fd2 = open(f2, O_WRONLY | O_BINARY | O_CREAT | O_TRUNC, 0666)) == -1) {
close(fd1);
return -1;
}
while ((rd = read(fd1, buffer, BUF_SZ)) > 0) {
if (write(fd2, buffer, rd) != rd) {
close(fd1);
close(fd2);
unlink(f2);
return -1;
}
}
close(fd2);
close(fd1);
return 0;
}
char *MakeBackup(char *FileName, char *NewName) {
// static char NewName[260];
int l = strlen(FileName);
if (l <= 0)
return NULL;
// disabling this! it works great on any filesystem not restricted
// to 8.3 filenames, but on DOS, it results in a backup filename being
// creating that is not legal if the file extension is already 3 characters
// long. this causes the checks here to work incorrectly. the end result of
// all of that is that file saving with the 'KeepBackups' option DISABLED
// results in your ORIGINAL file being deleted every time you save! ouch!
// since i am building this fork specifically for use on DOS, it only makes
// sense for me to keep the 8.3 DOS compatibility enabled anyway.
#if 0
/* try 1 */
strcpy(NewName, FileName);
strcat(NewName, "~");
if (!IsSameFile(FileName,NewName)) {
if (access(NewName, 0) == 0) // Backup already exists?
unlink(NewName); // Then delete the file..
if (access(FileName, 0) != 0) // Original found?
return NewName;
if (copyfile(FileName, NewName) == 0)
return NewName;
//#if 0
if (errno == 2)
return NewName; /* file not found */
//#endif
}
#endif
/* try 2: 8.3 */
strcpy(NewName, FileName);
NewName[l-1] = '~';
if (!IsSameFile(FileName,NewName)) {
if (access(NewName, 0) == 0) // Backup exists?
unlink(NewName); // Then delete;
if (access(FileName, 0) != 0) // Original exists?
return NewName; // If not-> return base..
if (copyfile(FileName, NewName) == 0)
return NewName;
#if 0
if (errno == 2)
return NewName;
#endif
}
return NULL;
}
int GetCharFromEvent(TEvent &E, char *Ch) {
*Ch = 0;
if (E.Key.Code & kfModifier)
return 0;
if (kbCode(E.Key.Code) == kbEsc) { *Ch = 27; return 1; }
if (kbCode(E.Key.Code) == kbEnter) { *Ch = 13; return 1; }
if (kbCode(E.Key.Code) == (kbEnter | kfCtrl)) { *Ch = 10; return 1; }
if (kbCode(E.Key.Code) == kbBackSp) { *Ch = 8; return 1; }
if (kbCode(E.Key.Code) == (kbBackSp | kfCtrl)) { *Ch = 127; return 1; }
if (kbCode(E.Key.Code) == kbTab) { *Ch = 9; return 1; }
if (kbCode(E.Key.Code) == kbDel) { *Ch = 127; return 1; }
if (keyType(E.Key.Code) == kfCtrl) {
*Ch = (char) (E.Key.Code & 0x1F);
return 1;
}
if (isAscii(E.Key.Code)) {
*Ch = (char)E.Key.Code;
return 1;
}
return 0;
}
int CompletePath(const char *Base, char *Match, int Count) {
char Name[MAXPATH];
const char *dirp;
char *namep;
int len, count = 0;
char cname[MAXPATH];
int hascname = 0;
RxMatchRes RM;
FileFind *ff;
FileInfo *fi;
int rc;
if (strcmp(Base, "") == 0) {
if (ExpandPath(".", Name) != 0) return -1;
} else {
if (ExpandPath(Base, Name) != 0) return -1;
}
// SlashDir(Name);
dirp = Name;
namep = SepRChr(Name);
if (namep == Name) {
dirp = SSLASH;
namep = Name + 1;
} else if (namep == NULL) {
namep = Name;
dirp = SDOT;
} else {
*namep = 0;
namep++;
}
len = strlen(namep);
strcpy(Match, dirp);
SlashDir(Match);
cname[0] = 0;
ff = new FileFind(dirp, "*",
ffDIRECTORY | ffHIDDEN
#if defined(USE_DIRENT)
| ffFAST // for SPEED
#endif
);
if (ff == 0)
return 0;
rc = ff->FindFirst(&fi);
while (rc == 0) {
char *dname = fi->Name();
// filter out unwanted files
if ((strcmp(dname, ".") != 0) &&
(strcmp(dname, "..") != 0) &&
(!CompletionFilter || RxExec(CompletionFilter, dname, strlen(dname), dname, &RM) != 1))
{
if ((
#if defined(UNIX)
strncmp
#else // os2, nt, ...
strnicmp
#endif
(namep, dname, len) == 0)
&& (dname[0] != '.' || namep[0] == '.'))
{
count++;
if (Count == count) {
Slash(Match, 1);
strcat(Match, dname);
if (
#if defined(USE_DIRENT) // for SPEED
IsDirectory(Match)
#else
fi->Type() == fiDIRECTORY
#endif
)
Slash(Match, 1);
} else if (Count == -1) {
if (!hascname) {
strcpy(cname, dname);
hascname = 1;
} else {
int o = 0;
#ifdef UNIX
while (cname[o] && dname[o] && (cname[o] == dname[o])) o++;
#endif
#if defined(OS2) || defined(NT) || defined(DOS) || defined(DOSP32)
while (cname[o] && dname[o] && (toupper(cname[o]) == toupper(dname[o]))) o++;
#endif
cname[o] = 0;
}
}
}
}
delete fi;
rc = ff->FindNext(&fi);
}
delete ff;
if (Count == -1) {
Slash(Match, 1);
strcat(Match, cname);
if (count == 1) SlashDir(Match);
}
return count;
}
int UnTabStr(char *dest, int maxlen, const char *source, int slen) {
char *p = dest;
int i;
int pos = 0;
maxlen--;
for (i = 0; i < slen; i++) {
if (maxlen > 0) {
if (source[i] == '\t') {
do {
if (maxlen > 0) {
*p++ = ' ';
maxlen--;
}
pos++;
} while (pos & 0x7);
} else {
*p++ = source[i];
pos++;
maxlen--;
}
} else
break;
}
//dest[pos] = 0;
*p = '\0';
return pos;
}