Inexor
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
stream.hpp
Go to the documentation of this file.
1 #pragma once
4 
5 #include <stdio.h> // for size_t, SEEK_SET
6 #include <string.h> // for strlen
7 #include <zlib.h> // for Z_BEST_COMPRESSION
8 
9 #include "inexor/shared/cube_endian.hpp" // for bigswap, lilswap
10 #include "inexor/shared/cube_formatting.hpp" // for PRINTFARGS
11 #include "inexor/shared/cube_types.hpp" // for uchar, uint
12 #include "inexor/shared/cube_vector.hpp" // for vector
13 
14 // workaround for some C platforms that have these two functions as macros - not used anywhere
15 #ifdef getchar
16 #undef getchar
17 #endif
18 #ifdef putchar
19 #undef putchar
20 #endif
21 
22 struct stream
23 {
24 #ifdef WIN32
25 #ifdef __GNUC__
26  typedef off64_t offset;
27 #else
28  typedef __int64 offset;
29 #endif
30 #else
31  typedef off_t offset;
32 #endif
33 
34  virtual ~stream() {}
35  virtual void close() = 0;
36  virtual bool end() = 0;
37  virtual offset tell() { return -1; }
38  virtual offset rawtell() { return tell(); }
39  virtual bool seek(offset pos, int whence = SEEK_SET) { return false; }
40  virtual offset size();
41  virtual offset rawsize() { return size(); }
42  virtual size_t read(void *buf, size_t len) { return 0; }
43  virtual size_t write(const void *buf, size_t len) { return 0; }
44  virtual bool flush() { return true; }
45  virtual int getchar() { uchar c; return read(&c, 1) == 1 ? c : -1; }
46  virtual bool putchar(int n) { uchar c = n; return write(&c, 1) == 1; }
47  virtual bool getline(char *str, size_t len);
48  virtual bool putstring(const char *str) { size_t len = strlen(str); return write(str, len) == len; }
49  virtual bool putline(const char *str) { return putstring(str) && putchar('\n'); }
50  virtual size_t printf(const char *fmt, ...) PRINTFARGS(2, 3);
51  virtual uint getcrc() { return 0; }
52 
53  template<class T> size_t put(const T *v, size_t n) { return write(v, n*sizeof(T))/sizeof(T); }
54  template<class T> bool put(T n) { return write(&n, sizeof(n)) == sizeof(n); }
55  template<class T> bool putlil(T n) { return put<T>(lilswap(n)); }
56  template<class T> bool putbig(T n) { return put<T>(bigswap(n)); }
57 
58  template<class T> size_t get(T *v, size_t n) { return read(v, n*sizeof(T))/sizeof(T); }
59  template<class T> T get() { T n; return read(&n, sizeof(n)) == sizeof(n) ? n : 0; }
60  template<class T> T getlil() { return lilswap(get<T>()); }
61  template<class T> T getbig() { return bigswap(get<T>()); }
62 
63 };
64 
65 template<class T>
66 struct streambuf
67 {
69 
70  streambuf(stream *s) : s(s) {}
71 
72  T get() { return s->get<T>(); }
73  size_t get(T *vals, size_t numvals) { return s->get(vals, numvals); }
74  void put(const T &val) { s->put(&val, 1); }
75  void put(const T *vals, size_t numvals) { s->put(vals, numvals); }
76  size_t length() { return s->size(); }
77 };
78 
79 
80 extern char *makerelpath(const char *dir, const char *file, const char *prefix = nullptr, const char *cmd = nullptr);
81 extern char *path(char *s);
82 extern char *path(const char *s, bool copy);
83 extern const char *parentdir(const char *directory);
84 extern bool fileexists(const char *path, const char *mode);
85 extern bool createdir(const char *path);
86 extern size_t fixpackagedir(char *dir);
87 extern const char *addpackagedir(const char *dir);
88 extern const char *findfile(const char *filename, const char *mode);
89 extern stream *openrawfile(const char *filename, const char *mode);
90 extern stream *openfile(const char *filename, const char *mode);
91 extern stream *opentempfile(const char *filename, const char *mode);
92 extern stream *opengzfile(const char *filename, const char *mode, stream *file = nullptr, int level = Z_BEST_COMPRESSION);
93 extern stream *openutf8file(const char *filename, const char *mode, stream *file = nullptr);
94 extern char *loadfile(const char *fn, size_t *size, bool utf8 = true);
95 extern bool listdir(const char *dir, bool rel, const char *ext, vector<char *> &files);
96 extern int listfiles(const char *dir, const char *ext, vector<char *> &files);
97 
char * makerelpath(const char *dir, const char *file, const char *prefix=nullptr, const char *cmd=nullptr)
Append a string together but add the prefix in the field.
Definition: stream.cpp:35
T getlil()
Definition: stream.hpp:60
virtual offset rawsize()
Definition: stream.hpp:41
T bigswap(T n)
Definition: cube_endian.hpp:43
streambuf(stream *s)
Definition: stream.hpp:70
virtual bool putline(const char *str)
Definition: stream.hpp:49
virtual size_t virtual uint getcrc()
Definition: stream.hpp:51
virtual bool seek(offset pos, int whence=SEEK_SET)
Definition: stream.hpp:39
virtual bool putstring(const char *str)
Definition: stream.hpp:48
unsigned int uint
Definition: cube_types.hpp:9
virtual offset rawtell()
Definition: stream.hpp:38
StringFormatter::FmtMark fmt
Marker that starts formatting a string.
Definition: StringFormatter.cpp:5
virtual int getchar()
Definition: stream.hpp:45
char * loadfile(const char *fn, size_t *size, bool utf8=true)
Definition: stream.cpp:938
char * path(char *s)
Modifies the input string to only contain slashes in the direction the platform allows.
Definition: stream.cpp:63
off_t offset
Definition: stream.hpp:31
#define PRINTFARGS(fmt, args)
Definition: cube_formatting.hpp:11
bool createdir(const char *path)
Creates a directory of given name.
Definition: stream.cpp:150
stream * s
Definition: stream.hpp:68
void put(const T *vals, size_t numvals)
Definition: stream.hpp:75
virtual size_t read(void *buf, size_t len)
Definition: stream.hpp:42
virtual bool end()=0
bool fileexists(const char *path, const char *mode)
Checks whether given file exists (and is available in the specific mode) Where Path is the filename a...
Definition: stream.cpp:136
virtual bool putchar(int n)
Definition: stream.hpp:46
size_t length()
Definition: stream.hpp:76
stream * opengzfile(const char *filename, const char *mode, stream *file=nullptr, int level=Z_BEST_COMPRESSION)
Definition: stream.cpp:920
bool put(T n)
Definition: stream.hpp:54
const char * addpackagedir(const char *dir)
Add an optional media directory.
Definition: stream.cpp:179
stream * openutf8file(const char *filename, const char *mode, stream *file=nullptr)
Definition: stream.cpp:929
bool putbig(T n)
Definition: stream.hpp:56
virtual void close()=0
stream * opentempfile(const char *filename, const char *mode)
Definition: stream.cpp:912
Legacy file system streams.
Definition: stream.hpp:22
static cubeext & ext(cube &c)
Definition: octa.hpp:60
static aviwriter * file
Definition: movie.cpp:830
virtual bool flush()
Definition: stream.hpp:44
#define dir(name, v, d, s, os)
Definition: physics.cpp:2014
size_t put(const T *v, size_t n)
Definition: stream.hpp:53
unsigned char uchar
Basic type definitions.
Definition: cube_types.hpp:7
stream * openfile(const char *filename, const char *mode)
Definition: stream.cpp:907
const char * parentdir(const char *directory)
Definition: stream.cpp:120
GLuint GLuint GLintptr GLsizeiptr size
Definition: glexts.hpp:412
GLintptr offset
Definition: glexts.hpp:354
virtual ~stream()
Definition: stream.hpp:34
virtual size_t printf(const char *fmt,...) PRINTFARGS(2
Definition: stream.cpp:329
virtual bool getline(char *str, size_t len)
Definition: stream.cpp:318
GLenum GLenum GLuint GLint level
Definition: glexts.hpp:296
size_t get(T *v, size_t n)
Definition: stream.hpp:58
T getbig()
Definition: stream.hpp:61
void copy()
Definition: octaedit.cpp:1629
virtual offset tell()
Definition: stream.hpp:37
size_t fixpackagedir(char *dir)
Definition: stream.cpp:165
const char * findfile(const char *filename, const char *mode)
Checks whether given file exists (and is available in the specific mode) Where Path is the filename a...
Definition: stream.cpp:196
virtual size_t write(const void *buf, size_t len)
Definition: stream.hpp:43
virtual offset size()
Definition: stream.cpp:310
bool listdir(const char *dir, bool rel, const char *ext, vector< char * > &files)
Internal use only Use listfiles instead.
Definition: stream.cpp:231
stream * openrawfile(const char *filename, const char *mode)
Definition: stream.cpp:898
void put(const T &val)
Definition: stream.hpp:74
bool putlil(T n)
Definition: stream.hpp:55
int listfiles(const char *dir, const char *ext, vector< char * > &files)
Lists all files in given directory and put it into vector files ext optionally filters for occurences...
Definition: stream.cpp:285
T lilswap(T n)
Definition: cube_endian.hpp:41
Definition: stream.hpp:66