1 | #include "nonstd_stdio.h" |
---|
2 | #if defined _WIN32 && !defined SHP |
---|
3 | //tu nie trzeba includów zeby dzialal unlink() |
---|
4 | #else |
---|
5 | #include <unistd.h> |
---|
6 | #endif |
---|
7 | |
---|
8 | bool fileExists(const char* path) |
---|
9 | { |
---|
10 | //lepiej gdyby uzywalo stat bo mfopen mogloby cos niepotrzebnie wczytywac przy otwarciu pliku ale mfopen wiadomo ze zadziala wszedzie tak samo |
---|
11 | MFILE *f=mfopen(path,"r"); |
---|
12 | if (f==NULL) return false; |
---|
13 | mfclose(f); |
---|
14 | return true; |
---|
15 | } |
---|
16 | |
---|
17 | bool removeFile(const char* path) |
---|
18 | { |
---|
19 | return unlink(path)==0; //VS: "The POSIX name is deprecated. Instead, use the ISO C++ conformant name: _unlink" |
---|
20 | } |
---|
21 | |
---|
22 | |
---|
23 | #if defined SHP && defined BADA_API_1 |
---|
24 | |
---|
25 | MFILE *mfopen(const char*path, const char*mode) |
---|
26 | { |
---|
27 | Osp::Io::File *f = new Osp::Io::File(); |
---|
28 | result r = f->Construct(path, mode); |
---|
29 | if (IsFailed(r)) |
---|
30 | { |
---|
31 | delete f; |
---|
32 | f = NULL; |
---|
33 | } |
---|
34 | return f; |
---|
35 | } |
---|
36 | |
---|
37 | void mfclose(MFILE *f) |
---|
38 | { |
---|
39 | delete f; |
---|
40 | } |
---|
41 | |
---|
42 | int mfread(void *ptr, int size, int count, MFILE *f) |
---|
43 | { |
---|
44 | int bytes = size * count; |
---|
45 | int przeczytane = f->Read(ptr, bytes); |
---|
46 | return przeczytane != bytes ? przeczytane / size : count; |
---|
47 | } |
---|
48 | |
---|
49 | int mfwrite(const void *ptr, int size, int count, MFILE *f) |
---|
50 | { |
---|
51 | result r = f->Write(ptr, size * count); |
---|
52 | if (IsFailed(r)) |
---|
53 | return 0; //nie mozemy wykryc jesli udalo sie zapisac czêæ |
---|
54 | else |
---|
55 | return count; |
---|
56 | } |
---|
57 | |
---|
58 | int mfputs(const char *txt, MFILE *f) |
---|
59 | { |
---|
60 | int len = strlen(txt); |
---|
61 | int res = mfwrite(txt, len, 1, f); |
---|
62 | return res == 1 ? 1 : EOF; |
---|
63 | } |
---|
64 | |
---|
65 | char* mfgets(char *str, int num, MFILE *f) |
---|
66 | { |
---|
67 | bool err = false; |
---|
68 | int przeczytane = 0; |
---|
69 | num--; //zeby zawsze zostalo miejsce na wpisanie koncz¹cego NULL |
---|
70 | do |
---|
71 | { |
---|
72 | err = f->Read(str, 1) != 1; |
---|
73 | if (!err) |
---|
74 | { |
---|
75 | str++; |
---|
76 | przeczytane++; |
---|
77 | } |
---|
78 | } while (!err && przeczytane<num && *str != '\n'); |
---|
79 | if (*str == '\n' && przeczytane<num) |
---|
80 | *(str + 1) = 0; |
---|
81 | return przeczytane == 0 ? NULL : str; |
---|
82 | } |
---|
83 | |
---|
84 | int mfeof(MFILE *f) |
---|
85 | { |
---|
86 | //brzydkie obejscie zeby w bada wykryc czy FILE jest w stanie EOF |
---|
87 | static char buf[1]; |
---|
88 | int pos = f->Tell(); |
---|
89 | int przeczytane = f->Read(&buf, 1); |
---|
90 | f->Seek(Osp::Io::FILESEEKPOSITION_BEGIN,pos); |
---|
91 | return przeczytane == 1 ? 0 : 1; |
---|
92 | } |
---|
93 | |
---|
94 | int mfseek(MFILE *f, long position, int type) |
---|
95 | { |
---|
96 | result r; |
---|
97 | if (type == SEEK_SET) |
---|
98 | r = f->Seek(Osp::Io::FILESEEKPOSITION_BEGIN, position); |
---|
99 | else if (type == SEEK_CUR) |
---|
100 | r = f->Seek(Osp::Io::FILESEEKPOSITION_CURRENT, position); |
---|
101 | else if (type == SEEK_END) |
---|
102 | r = f->Seek(Osp::Io::FILESEEKPOSITION_END, position); |
---|
103 | else |
---|
104 | return 1; |
---|
105 | return IsFailed(r) ? 1 : 0; |
---|
106 | } |
---|
107 | |
---|
108 | long mftell(MFILE *f) |
---|
109 | { |
---|
110 | return f->Tell(); |
---|
111 | } |
---|
112 | |
---|
113 | #endif |
---|
114 | |
---|