Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
20 | pmbaty | 1 | #include "brstdfile.h" |
2 | |||
3 | #include "CORE/FW/diag.h" |
||
4 | #include "harness/hooks.h" |
||
5 | #include "harness/trace.h" |
||
6 | #include <stdio.h> |
||
7 | #include <string.h> |
||
8 | |||
9 | // Global variables |
||
10 | br_filesystem BrStdioFilesystem = { |
||
11 | "Standard IO", |
||
12 | BrStdioAttributes, |
||
13 | BrStdioOpenRead, |
||
14 | BrStdioOpenWrite, |
||
15 | BrStdioClose, |
||
16 | BrStdioEof, |
||
17 | BrStdioGetChar, |
||
18 | BrStdioPutChar, |
||
19 | BrStdioRead, |
||
20 | BrStdioWrite, |
||
21 | BrStdioGetLine, |
||
22 | BrStdioPutLine, |
||
23 | BrStdioAdvance |
||
24 | }; |
||
25 | br_filesystem* _BrDefaultFilesystem = &BrStdioFilesystem; |
||
26 | |||
27 | br_uint_32 BrStdioAttributes(void) { |
||
28 | return BR_FS_ATTR_READABLE | BR_FS_ATTR_WRITEABLE | BR_FS_ATTR_HAS_TEXT | BR_FS_ATTR_HAS_BINARY | BR_FS_ATTR_HAS_ADVANCE; |
||
29 | } |
||
30 | |||
31 | void* BrStdioOpenRead(char* name, br_size_t n_magics, br_mode_test_cbfn* identify, int* mode_result) { |
||
32 | FILE* fh; |
||
33 | //char* br_path; // Pierre-Marie Baty -- unused variable |
||
34 | //char config_path[512]; // Pierre-Marie Baty -- unused variable |
||
35 | //char try_name[512]; // Pierre-Marie Baty -- unused variable |
||
36 | //char* cp; // Pierre-Marie Baty -- unused variable |
||
37 | br_uint_8 magics[16]; |
||
38 | int open_mode; |
||
39 | LOG_TRACE("(\"%s\", %d, %p, %p)", name, n_magics, identify, mode_result); |
||
40 | |||
41 | open_mode = BR_FS_MODE_BINARY; |
||
42 | fh = fopen(name, "rb"); |
||
43 | if (fh == NULL) { |
||
44 | // skip logic around getting BRENDER_PATH from ini files etc |
||
45 | return NULL; |
||
46 | } |
||
47 | |||
48 | if (n_magics != 0) { |
||
49 | if (fread(magics, 1u, n_magics, fh) != n_magics) { |
||
50 | fclose(fh); |
||
51 | return 0; |
||
52 | } |
||
53 | if (identify != NULL) { |
||
54 | open_mode = identify(magics, n_magics); |
||
55 | } |
||
56 | if (mode_result != NULL) { |
||
57 | *mode_result = open_mode; |
||
58 | } |
||
59 | } |
||
60 | |||
61 | fclose(fh); |
||
62 | if (open_mode == BR_FS_MODE_BINARY) { |
||
63 | fh = fopen(name, "rb"); |
||
64 | } else if (open_mode == BR_FS_MODE_TEXT) { |
||
65 | fh = fopen(name, "rt"); |
||
66 | } else { |
||
67 | BrFailure("BrStdFileOpenRead: invalid open_mode %d", open_mode); |
||
68 | return NULL; |
||
69 | } |
||
70 | return fh; |
||
71 | } |
||
72 | |||
73 | void* BrStdioOpenWrite(char* name, int mode) { |
||
74 | FILE* fh; |
||
75 | |||
76 | if (mode == BR_FS_MODE_TEXT) { |
||
77 | fh = Harness_Hook_fopen(name, "w"); |
||
78 | } else { |
||
79 | fh = Harness_Hook_fopen(name, "wb"); |
||
80 | } |
||
81 | |||
82 | return fh; |
||
83 | } |
||
84 | |||
85 | void BrStdioClose(void* f) { |
||
86 | LOG_TRACE("(%p)", f); |
||
87 | |||
88 | fclose(f); |
||
89 | } |
||
90 | |||
91 | int BrStdioEof(void* f) { |
||
92 | return feof((FILE*)f); |
||
93 | } |
||
94 | |||
95 | int BrStdioGetChar(void* f) { |
||
96 | int c; |
||
97 | c = fgetc(f); |
||
98 | // LOG_DEBUG("file pos: %d, char: %d", ftell(f) - 1, c); |
||
99 | return c; |
||
100 | } |
||
101 | |||
102 | void BrStdioPutChar(int c, void* f) { |
||
103 | fputc(c, f); |
||
104 | } |
||
105 | |||
106 | br_size_t BrStdioRead(void* buf, br_size_t size, unsigned int n, void* f) { |
||
107 | int i; |
||
108 | |||
109 | LOG_TRACE9("(%p, %d, %d, %p)", buf, size, n, f); |
||
110 | i = fread(buf, size, n, f); |
||
111 | return i; |
||
112 | } |
||
113 | |||
114 | br_size_t BrStdioWrite(void* buf, br_size_t size, unsigned int n, void* f) { |
||
115 | return fwrite(buf, size, n, f); |
||
116 | } |
||
117 | |||
118 | br_size_t BrStdioGetLine(char* buf, br_size_t buf_len, void* f) { |
||
119 | br_size_t l; |
||
120 | |||
121 | LOG_TRACE9("(%p, %d, %p)", buf, buf_len, f); |
||
122 | if (fgets(buf, buf_len, f) == NULL) { |
||
123 | return 0; |
||
124 | } |
||
125 | |||
126 | l = strlen(buf); |
||
127 | |||
128 | if (l != 0 && buf[l - 1] == '\n') { |
||
129 | buf[--l] = '\0'; |
||
130 | } |
||
131 | |||
132 | return l; |
||
133 | } |
||
134 | |||
135 | void BrStdioPutLine(char* buf, void* f) { |
||
136 | fputs(buf, f); |
||
137 | fputc('\n', f); |
||
138 | } |
||
139 | |||
140 | void BrStdioAdvance(br_size_t count, void* f) { |
||
141 | fseek(f, count, SEEK_CUR); |
||
142 | } |