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
|
extern "C" {
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <fcntl.h>
#include <gmem.h>
}
#include <gfile.h>
#include <GString.h>
#include <qdir.h>
// replacement stubs to simplify (and speed up) operations
void *gmalloc ( int size ) { return malloc ( size ); }
void *grealloc ( void *p, int size ) { return realloc ( p, size ); }
void gfree ( void *p ) { free ( p ); }
char *copyString ( char *str ) { return strdup ( str ); }
char *getLine(char *buf, int size, FILE *f) { return fgets ( buf, size, f ); }
GString *getHomeDir ( ) { return new GString ( QDir::home ( ). absPath ( ). local8Bit ( )); }
GString *appendToPath ( GString *path, char *fileName ) { return new GString ( QDir ( path-> getCString ( )). absFilePath ( fileName ). local8Bit ( )); }
// mostly taken from XPDF, but simplified...
GBool openTempFile ( GString **name, FILE **f, char *mode, char *ext )
{
char *s, *p;
int fd;
if ( !ext )
ext = ".tmp";
if (!( s = tmpnam ( 0 )))
return gFalse;
*name = new GString ( s );
s = (*name)-> getCString ( );
if (( p = strrchr ( s, '.' )))
(*name)-> del ( p - s, (*name)-> getLength ( ) - ( p - s ));
(*name)-> append ( "_qpdf" );
(*name)-> append ( ext );
fd = open ((*name)-> getCString ( ), O_WRONLY | O_CREAT | O_EXCL, 0600 );
if ( fd < 0 || !( *f = fdopen ( fd, mode ))) {
delete *name;
return gFalse;
}
return gTrue;
}
|