File: wp2lfuti.cc

package info (click to toggle)
wp2latex 3.97%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,228 kB
  • sloc: cpp: 45,091; ansic: 8,998; asm: 2,435; makefile: 529; sh: 19
file content (412 lines) | stat: -rw-r--r-- 8,772 bytes parent folder | download
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
/******************************************************************************
 * program:     wp2latex                                                      *
 * function:    convert WordPerfect files into LaTeX                          *
 * modul:       wp2lfuti.cc                                                   *
 * description: Some File Utilities for WP2Latex converter                    *
 ******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <common.h>

#ifndef __UNIX__
 #include <io.h>
#endif

#include "typedfs.h"
#include "stringa.h"

#include "wp2latex.h"
#include "struct.h"


#ifdef __UNIX__
 #define DIR_DELIMITER '/'
#else
 #define DIR_DELIMITER '\\'
#endif


#ifndef min
 #define min(value1,value2) ((value1 < value2) ? value1 : value2)
#endif

/*----------------------------------------------------*/
/*-------------Basic file I/O operations--------------*/
/*----------------------------------------------------*/


/** Opens a file for writting and optionally checks its existency.
 * @param[in]	name	File name.
 * @param[in]	type	Type of the file.
 * @param[in]   Err     File stream for error message, use NULL when no output is needed. */
FILE *OpenWrChk(const char *name, const char *type, FILE *Err)
{
FILE *fp;

  if(Err==NULL) Err=stderr;
  if(Noclobber)
    {
      if((fp = fopen(name, "r")) != NULL)
      {
        fclose(fp);
        fprintf(Err, _("\nError: File '%s' protected against overwritting."), name);
        return NULL;
      }
    }

  if((fp=fopen(name, type)) == NULL )
    {
    if(Err!=NULL)
        fprintf(Err, _("\nError: File '%s' can not be created."), name);
    return NULL;
    }
return fp;
}


/** Return a size of a file. filelength() is not available in UNIX.
 * @param[in]	f	Opened file. File position is preserved.
 * @return	File size detected or 0. */
long filesize(FILE *f)
{
#ifdef __UNIX__
  long pos, len;
  if((pos=ftell(f))<0) return(0);
  fseek(f,0,SEEK_END);
  len = ftell(f);
  fseek(f,pos,SEEK_SET);
  return(len);
#else
  return(filelength(fileno(f)));
#endif
}


/** This procedure copies "Src" file to "Dest"
 * @param[in]	 Dest	destination file name.
 * @param[in]	 Src	source file name.
 * @return	0 on success; <0 on failure. */
int CopyFile(const char *Dest, const char *Src)
{
FILE *in, *out;
char Buff[32];
size_t sz;

 out = fopen(Dest,"rb");
 if(out!=NULL)
	{
	fclose(out);
	return(-1);	//file already exist
	}

 if((in=fopen(Src,"rb"))==NULL)
	return(-2);
 if((out=fopen(Dest,"wb"))==NULL)
    {
    fclose(in);
    return(-3);
    }

 while((sz=fread(Buff,1,sizeof(Buff),in)) > 0)
    {
    fwrite(Buff,1,sz,out);
    }

 fclose(in);
 fclose(out);

return(0);
}


/** This procedure copies 'size' bytes from "Src" file to "Dest".
 * @return	0-OK; <0 Error;
 *              -1 Output file already exists; -2 Invalid input file;
 *		-3 Cannot create output file */
int CopyFile(const char *Dest, FILE *in, long size)
{
FILE *out;
char Buff[32];
size_t sz;

 out = fopen(Dest,"rb");
 if(out!=NULL)
   {
   fclose(out);
   return(-1);		//file already exist
   }

 if(in==NULL) return(-2);

 if(Dest==NULL || (out=fopen(Dest,"wb"))==NULL)
   {
   return(-3);
   }

 while(size > 0)
    {
    if(feof(in)) break;
    sz = fread(Buff, 1, min(sizeof(Buff),size), in);
    if(sz==0 || sz>sizeof(Buff)) break;
    fwrite(Buff,1,sz,out);
    size -= sz;
    }

 fclose(out);

return(size);
}


/** This procedure copies 'size' bytes from "Src" file to "Dest".
 * @return	0-OK; <0 Error;
 *              -1 Invalid output file; -2 Invalid input file; */
int CopyFile(FILE *out, FILE *in, long size)
{
char Buff[32];
size_t sz;

 if(out==NULL) return(-1);
 if(in==NULL) return(-2);

 while(size > 0)
    {
    if(feof(in)) break;
    sz = fread(Buff, 1, min(sizeof(Buff),size), in);
    if(sz==0 || sz>sizeof(Buff)) break;
    fwrite(Buff,1,sz,out);
    size -= sz;
    }

 fclose(out);

return(size);
}


DWORD ch_fgetc(FILE *F)
{
DWORD d;
  d=fgetc(F);
  if(d==EOF) d=0xFFFFFFFF;
  return(d);
}


DWORD w_fgetc(FILE *F)
{
WORD w;
  if(RdWORD_LoEnd(&w,F)<2) return(0xFFFFFFFF);
  return(w);
}


DWORD W_fgetc(FILE *F)
{
WORD w;
  if(RdWORD_HiEnd(&w,F)<2) return(0xFFFFFFFF);
  return(w);
}



/*---------------------------------------------------------*/
/*--------------File related string operations-------------*/
/*---------------------------------------------------------*/

/**This function reads one line from the text file and store it to the string.*/
string & fGets2(FILE *f, string & pstr, long MaxStrSize)
{
  char c;

  pstr.erase();
  while(!feof(f))
     {
     c = getc(f);
     if (c == '\n') break;
     if (c == '\r') continue;
     if ((unsigned char)c == 0xFF)
	if(feof(f)) break;	//Fix the situation, when 0xFF is read before eof

     pstr += c;
     if(MaxStrSize>0)
        if(--MaxStrSize==0) break;
     }

 return(pstr);
}



/*---------------------------------------------------------*/
/*----------Filename handling string operations------------*/
/*---------------------------------------------------------*/

/**This function returns a positive value >=0 if the given path is absolute*/
int AbsolutePath(const char *TestedPath)
{
 if(TestedPath==NULL) return(-1);
 if(*TestedPath==0) return(-2);

#if defined(__MSDOS__) || defined(__WIN32__) || defined(_WIN32) || defined(_WIN64) || defined(__OS2__)
 if(*TestedPath>' ')	//path is at least 2 byte long
   if(TestedPath[1]==':') return(1);
#endif
#ifdef __UNIX__
 if(*TestedPath=='~') return(1);
#endif

 if(*TestedPath=='\\' || *TestedPath=='/') return(1);

return 0;
}


/** This function extracts filename from a full path */
string CutFileName(const char *FullFilename)
{
char ch;
int i,j;

 if(FullFilename==NULL) return string("Unknown");
 while(*FullFilename==' ') FullFilename++;  //Erase spaces at the beginning of the filename
 i=strlen(FullFilename);
 j=-i;
 if(i==0) return string("Unknown");

 while(i>=0)	//search form end of a string
   {
   ch=FullFilename[i];
   if(ch=='.' && j<0)
	j=i;		//extension found

   if(ch=='/' || ch=='\\' || ch==':')
   	{
	i++;
	break;
	}
   i--;
   }

 if(i<0) i=0;
 if(j<0) j=-j;		//No extension found

return copy(FullFilename,i,j-i);
}


/// This function gets path end position from path and file name.
int GetPathEnd(const char *FullName)
{
unsigned LastPos;
const char *NameStep;

 if(FullName==NULL) return(0);

 LastPos = 0;
 NameStep = FullName;
 while(*NameStep!=0)
   {
   switch(*NameStep)
     {
     case ':':		/* Patch for MsDOS like c:\ or x: */	
	       if(NameStep-FullName == 1)
	          LastPos = NameStep-FullName;
	       break;
     case '/':
     case '\\':LastPos = NameStep-FullName;
               break;
     }
   NameStep++;
   }

return(LastPos);
}


const char *GetExtension(const char *FullName)
{
const char *NameStep;
int LastPos;

if(FullName==NULL) return(NULL);

LastPos=strlen(FullName);
NameStep=FullName+LastPos+1;
while(NameStep>FullName)
	{
	NameStep--;

	if(*NameStep=='/' || *NameStep=='\\' || *NameStep==':')
		break;
	if(*NameStep=='.')
		return(NameStep);
	}

return(FullName+LastPos);
}


/** Obtain filename with extension without leading path. */
const char *GetFullFileName(const char *PathName)
{
const char *NameStep;

 if(PathName==NULL) return(NULL);

 NameStep = PathName+strlen(PathName);
 while(NameStep>PathName)
	{
	if(*NameStep=='/' || *NameStep=='\\' || *NameStep==':')
		return(NameStep+1);
	NameStep--;
	}

return(PathName);
}


temp_string MergePaths(const string & Path, const string & RelPath, bool EnsureTrail)
{
temp_string tmp;
char c;
 if(RelPath=="") return(Path);
 c = RelPath[0];
 if(AbsolutePath(RelPath())) 
   {
   tmp = RelPath;
   }
 else
   {
   if(RelPath[0]=='.')
      {
      c = RelPath[1];
      if(c==0) return(Path);
      if(c=='/' || c=='\\')
         {
	 tmp = Path;
         c=Path[StrLen(Path)-1];
         if(c=='/' || c=='\\')
                {
		tmp += RelPath()+2;
		tmp += DIR_DELIMITER;
                return(tmp);
                }
	 tmp += DIR_DELIMITER;
         tmp+=RelPath()+2;
         return tmp;
         }
      }
	// Ensure that resulting path ends with '/' or '\\' based on OS
    tmp = Path+RelPath;
    }

  if(EnsureTrail && length(tmp)>0)
  {
    c = tmp[length(tmp)-1];
    if(c!='/' && c!='\\') tmp += DIR_DELIMITER;
  }
return tmp;
}