Go to the documentation of this file.
23 inline bool exists(
const std::string& a_string) {
24 FILE* file = ::fopen(a_string.c_str(),
"rb");
25 if(!file)
return false;
30 inline bool write(
const std::string& a_file,
const std::string& a_string) {
32 FILE* file = ::fopen(a_file.c_str(),
"wb");
33 if(!file)
return false;
34 if(::fprintf(file,
"%s",a_string.c_str())<0) {
42 inline bool write(
const std::string& a_file,
const std::vector<std::string>& a_text){
43 FILE* file = ::fopen(a_file.c_str(),
"wb");
44 if(!file)
return false;
46 if(::fprintf(file,
"%s\n",(*it).c_str())<0) {
56 FILE* file = ::fopen(a_file.c_str(),
"wb");
57 if(!file)
return false;
62 inline bool read_buff(FILE* a_file,
char* a_buff,
unsigned int a_lbuf,
size_t& a_length) {
63 a_length = ::fread(a_buff,1,a_lbuf,a_file);
67 inline bool read_cstring(FILE* a_file,
char* a_buff,
unsigned int a_lbuf,
size_t& a_length) {
68 if(::fgets(a_buff,a_lbuf,a_file)==NULL) {
73 size_t l = ::strlen(a_buff);
76 if( (l>=2) && (a_buff[l-2]==
'\r') && (a_buff[l-1]==
'\n') ) {
79 }
else if( (l>=1) && (a_buff[l-1]==
'\n') ) {
89 inline bool read(FILE* a_FILE,std::vector<std::string>& a_text){
91 unsigned int BUFSIZE = 65536;
92 char* buffer =
new char[BUFSIZE+1];
93 if(!buffer)
return false;
97 a_text.push_back(buffer);
103 inline bool read(
const std::string& a_file,std::vector<std::string>& a_text){
104 FILE* file = ::fopen(a_file.c_str(),
"rb");
105 if(!file) {a_text.clear();
return false;}
106 bool status =
read(file,a_text);
111 inline bool read_num(
const std::string& a_file,std::vector<std::string>::size_type a_num,std::vector<std::string>& a_text,
const std::string& a_cmt =
""){
113 FILE* file = ::fopen(a_file.c_str(),
"rb");
114 if(!file)
return false;
115 unsigned int BUFSIZE = 65536;
116 char* buffer =
new char[BUFSIZE+1];
117 if(!buffer) {::fclose(file);
return false;}
121 if(a_cmt.size()&&(!strncmp(a_cmt.c_str(),buffer,a_cmt.size())))
continue;
122 if(a_text.size()<a_num) {
123 a_text.push_back(buffer);
133 inline bool read_bytes(
const std::string& a_file,
char*& a_buffer,
long& a_length){
135 FILE* file = ::fopen(a_file.c_str(),
"rb");
142 ::fseek(file,0L,SEEK_END);
143 long filesize = ::ftell(file);
146 a_buffer =
new char[1];
148 mem::increment(s_new().c_str());
154 a_buffer =
new char[filesize+1];
162 mem::increment(s_new().c_str());
165 size_t nitem = ::fread(a_buffer,(
size_t)filesize,(
size_t)1,file);
170 mem::decrement(s_new().c_str());
177 a_buffer[filesize] = 0;
182 inline bool write_bytes(
const std::string& a_file,
const char* a_buffer,
size_t a_length){
183 FILE* file = ::fopen(a_file.c_str(),
"wb");
184 if(!file)
return false;
189 size_t nitem = ::fwrite((
char*)a_buffer,a_length,(
size_t)1,file);
191 return (nitem!=1?
false:
true);
194 inline bool num_csv_doubles(
const std::string& a_string,
char& a_sep,
unsigned int& a_number){
195 if(a_string.empty()) {a_sep=0;a_number=0;
return true;}
199 {
const char* buffer = a_string.c_str();
201 const char* pos = buffer;
202 const char*
mx = buffer+a_string.size();
204 {
double d = ::strtod(pos,&
end);(void)d;}
206 if(
end==pos) {a_sep=0;a_number=0;
return false;}
207 if(
end==
mx) {a_sep=0;a_number=1;
return true;}
212 unsigned int number = 0;
214 const char* buffer = a_string.c_str();
216 const char* pos = buffer;
217 const char*
mx = buffer+a_string.size();
220 {
double d = ::strtod(pos,&
end);(void)d;}
246 inline bool is_hippo(
const std::string& a_file,
bool& a_is){
255 std::vector<std::string> txt;
257 if(txt.size()<3) {a_is=
false;
return true;}
261 std::vector<std::string> ws;
262 words(txt[1],
"\t",
false,ws);
263 if(ws.size()!=number) {a_is=
false;
return true;}
264 bool all_doubles =
true;
266 const char* pos = (*it).c_str();
268 double d = ::strtod(pos,&
end);(void)d;
269 if(
end==pos) {all_doubles=
false;
break;}
271 a_is = all_doubles?
false:
true;
277 inline bool is_csv(
const std::vector<std::string>& a_txt,
bool& a_is){
280 if(a_txt.empty())
return true;
284 {
const char* buffer = a_txt[0].c_str();
286 const char* pos = buffer;
288 {
double d = ::strtod(pos,&
end);(void)d;}
290 if(
end==pos)
return true;
296 unsigned int first_coln = 0;
299 const char* buffer = (*it).c_str();
301 const char* pos = buffer;
302 const char*
mx = buffer+(*it).size();
303 unsigned int coln = 0;
306 {
double d = ::strtod(pos,&
end);(void)d;}
309 if(*
end!=
sep)
return true;
315 if(it==a_txt.begin()) {
318 if(coln!=first_coln)
return true;
327 inline bool is_csv(
const std::string& a_file,
bool& a_is){
329 if(
suffix(a_file)==
"csv") {a_is =
true;
return true;}
331 std::vector<std::string> txt;
336 inline bool is_lua(
const std::string& a_file,
bool& a_is){
338 if(
suffix(a_file)==
"lua") {a_is =
true;
return true;}
344 inline bool is_py(
const std::string& a_file,
bool& a_is){
346 if(
suffix(a_file)==
"py") {a_is =
true;
return true;}
352 inline bool is_kumac(
const std::string& a_file,
bool& a_is){
354 if(
suffix(a_file)==
"kumac") {a_is =
true;
return true;}
360 inline bool is_insh(
const std::string& a_file,
bool& a_is){
361 if(
suffix(a_file)==
"insh") {a_is =
true;
return true;}
362 unsigned char head[6];
363 {
unsigned int num = 6;
364 if(!
signature(a_file,head,num)) {a_is =
false;
return false;}
365 if(num!=6) {a_is =
false;
return true;}}
366 if(head[0]!=
'#') {a_is =
false;
return true;}
367 if(head[1]!=
'!') {a_is =
false;
return true;}
368 if(head[2]!=
'i') {a_is =
false;
return true;}
369 if(head[3]!=
'n') {a_is =
false;
return true;}
370 if(head[4]!=
's') {a_is =
false;
return true;}
371 if(head[5]!=
'h') {a_is =
false;
return true;}
383 inline bool mime_type(
const std::string& a_file,std::string& a_mime){
389 a_mime =
"image/jpeg";
395 a_mime =
"image/png";
401 a_mime =
"image/vnd.microsoft.icon";
407 a_mime =
"image/fits";
413 a_mime =
"application/gzip";
419 a_mime =
"application/zip";
425 a_mime =
"application/octet-stream";
460 a_mime =
"application/postscript";
466 a_mime =
"text/plain";
478 a_mime =
"text/plain";
484 a_mime =
"text/plain";
490 a_mime =
"application/octet-stream";
494 a_mime =
"application/octet-stream";
507 inline bool found(
const std::string& a_file,
const std::string& a_what,std::vector<std::string>& a_found){
509 std::vector<std::string> text;
512 if((*it).find(a_what)!=std::string::npos) {
513 a_found.push_back(*it);
520 if(a_file.empty())
return true;
521 return (::
remove(a_file.c_str()) ==0 ?
true :
false);
533 inline bool std_rename(
const std::string& a_from,
const std::string& a_to) {
541 return (::rename(a_from.c_str(),a_to.c_str()) == 0 ?
true :
false);
544 inline bool copy_bytes(
const std::string& a_from,
const std::string& a_to) {
545 if(a_to==a_from)
return true;
548 if(!
read_bytes(a_from,buffer,length))
return false;
549 bool status =
write_bytes(a_to,buffer,(
size_t)length);
552 mem::decrement(s_new().c_str());
596 virtual bool open(
const std::string& a_file) {
598 m_FILE = ::fopen(a_file.c_str(),
"rb");
608 virtual bool read(
char* a_buff,
unsigned int a_lbuf,
size_t& a_length) {
609 a_length = ::fread(a_buff,1,a_lbuf,
m_FILE);
612 virtual bool get_line(
char* a_buff,
unsigned int a_lbuf) {
613 return ::fgets(a_buff,a_lbuf,
m_FILE)==NULL?
false:
true;
615 virtual bool eof()
const {
616 #if defined(_MSC_VER) && _MSC_VER <= 1400
617 return feof(
m_FILE)?
true:
false;
619 return ::feof(
m_FILE)?
true:
false;
#define tools_vforit(a__T, a__v, a__it)
#define tools_vforcit(a__T, a__v, a__it)