libreis  0.1.0
A simple header-based drop-in library
io.h
1 #ifndef __REIS_IO_H__
2 #define __REIS_IO_H__
3 
4 #include <reis/base.h>
5 
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
9 
10 #define ANSI_RED "\x1b[31m" // ANSI code used for color printing to stdout
11 #define ANSI_GREEN "\x1b[32m"
12 #define ANSI_YELLOW "\x1b[33m"
13 #define ANSI_BLUE "\x1b[34m"
14 #define ANSI_MAGENTA "\x1b[35m"
15 #define ANSI_CYAN "\x1b[36m"
16 #define ASNI_WHITE "\x1b[37m"
17 #define ANSI_RESET "\x1b[0m" // ANSI reset needed to be called after color change
18 
19 // File descriptors
20 #define STDIN_FILENO 0
21 #define STDOUT_FILENO 1
22 #define STDERR_FILENO 2
23 
24 #if defined(__GNUC__) || defined(__clang__)
25 #define CHECK_PRINTF_FMT(a, b) __attribute__ ((format (printf, a, b)))
26 #else
27 #define CHECK_PRINTF_FMT(...)
28 #endif
29 
30 //#define cprintfb(fg, bg, format, ...) fprintf(stdout, fg bg format ANSI_RESET, \
31 // "\x1b[%dm\x1b[%dm%s%s", 30+fg, 40+bg,text,ANSI_RESET)
32 
33 CHECK_PRINTF_FMT(1, 2) void eprintf( const char *fmt, ... );
34 CHECK_PRINTF_FMT(1, 3) void cprintf(const char *color, const char *fmt, ...);
35 
36 /* IO */
37 typedef struct {
38  char* path;
39  char* filename;
40  char* extension;
41  char* stem;
42 } Filesystem;
43 
44 Filesystem* rsNewFilesystem( char *path );
45 void rsFreeFilesystem( Filesystem *fs );
46 
47 /* FILE HANDLING
48 =================*/
49 char fpeek( FILE *stream );
50 wchar_t fpeek_wc( FILE *stream );
51 char fspeek( FILE *stream, long int offset, int position );
52 int frpeek( FILE *stream, char c );
53 int frdpeek( FILE *stream, char d );
54 int fcounts( FILE *stream );
55 void fcopy( FILE *dest, FILE *src );
56 bool fexists( const char *file );
57 bool fmove( char *oldpath, char *newpath );
58 const char* ExtractFileName( const char *path );
59 const char* ExtractFileExtension( const char *filename );
60 bool dexists( const char *path );
61 
62 /* STDIN
63 ========*/
64 void sgets(char* str, int n);
65 bool PromptYesOrNo(const char *question);
66 
67 #ifdef __cplusplus
68 }
69 #endif
70 
71 #endif
Shared types and functions, accounts for GNU Linux and MacOS specifications.
int fcounts(FILE *stream)
Character count of current line of buffer.
Definition: io.c:107
bool fexists(const char *file)
Checks if file exists.
Definition: io.c:240
int frpeek(FILE *stream, char c)
A recursive peek that goes to end of line or EOF to get # of occurences.
Definition: io.c:62
CHECK_PRINTF_FMT(1, 2)
Makes writing error to stderr slightly simpler.
Filesystem * rsNewFilesystem(char *path)
Create a Filesystem object.
Definition: io.c:355
void rsFreeFilesystem(Filesystem *fs)
Terminates a Filesystem object.
Definition: io.c:375
char fspeek(FILE *stream, long int offset, int position)
View a character at position without moving pointer; Peeks a seek.
Definition: io.c:43
bool fmove(char *oldpath, char *newpath)
Move data from oldpath file to newpath.
Definition: io.c:198
int frdpeek(FILE *stream, char d)
A recursive peek that goes till the delimter d.
Definition: io.c:83
void fcopy(FILE *dest, FILE *src)
Copies data from src file to dest file.
Definition: io.c:125
char fpeek(FILE *stream)
View the next character in stream, doesn't move pointer.
Definition: io.c:22
const char * ExtractFileExtension(const char *filename)
Returns file extension.
Definition: io.c:141
const char * ExtractFileName(const char *path)
Returns filename.
Definition: io.c:168
bool PromptYesOrNo(const char *question)
Prompts the user with a yes or no question.
Definition: io.c:292
wchar_t fpeek_wc(FILE *stream)
Same as fpeek but for wchar_t.
Definition: io.c:33
void sgets(char *str, int n)
A safe way to read input that ensures no misc LF or breaks in read string.
Definition: io.c:275
bool dexists(const char *path)
Checks if directory exists.
Definition: io.c:252
Definition: io.h:37