1 #include "shgetc.h"
2 
3 #include <stdio.h>
4 
shinit_wcstring(struct fake_file_t * f,const wchar_t * wcs)5 void shinit_wcstring(struct fake_file_t *f, const wchar_t* wcs) {
6     f->rstart = wcs;
7     f->rpos = wcs;
8     f->rend = wcs + wcslen(wcs);
9     f->extra_eof = 0;
10 }
11 
shgetc(struct fake_file_t * f)12 int shgetc(struct fake_file_t *f) {
13     if (f->rpos >= f->rend) {
14         f->extra_eof ++;
15         return EOF;
16     }
17     wchar_t wc = *f->rpos++;
18     int ch = (wc < 128) ? (int)wc : '@';
19     return ch;
20 }
21 
shunget(struct fake_file_t * f)22 void shunget(struct fake_file_t *f) {
23     if (f->extra_eof) {
24         f->extra_eof--;
25     } else if (f->rpos > f->rstart) {
26         f->rpos--;
27     }
28 }
29 
shlim(struct fake_file_t * f,off_t lim)30 void shlim(struct fake_file_t *f, off_t lim) {
31     int off = f->rpos - f->rstart;
32     if (off > lim)
33         f->rpos = f->rstart + lim;
34 
35 }
36 
shcnt(struct fake_file_t * f)37 off_t shcnt(struct fake_file_t *f) {
38     return (off_t)(f->rpos - f->rstart);
39 }
40