aboutsummaryrefslogblamecommitdiff
path: root/utils.cpp
blob: 698c7ebec79cb10621e9108fb5e404e1d7437d35 (plain) (tree)
1
2
3
4
5
6
7
8

                      



                  

                   


                             

                                                                                 
                     



                                    






                                                                               
                                       



                         
                                                    


                                                                                


                                                 
                  


                             




                                                                     
                                         
























                                                            
//Chris Xiong 2018
//3-Clause BSD License
#include <cstdio>
#include <cstring>
#include <cerrno>
#include <cctype>
#include <unistd.h>
#include <fcntl.h>
#include "utils.hpp"
int readint(const char* path)
{
	int fd=open(path,O_RDONLY);
	if(!~fd)return LOG('W',"failed to open %s for reading: %d",path,errno),0;
	char buf[16];
	int l=read(fd,buf,15);
	if(!~l){close(fd);return 0;}
	buf[l]=0;
	close(fd);
	return atoi(buf);
}
float readfloat(const char* path)
{
	FILE* f=fopen(path,"r");
	if(!f)return LOG('W',"failed to open %s for reading: %d",path,errno),0;
	char buf[16];
	ignore_result(fgets(buf,16,f));
	buf[15]=0;
	fclose(f);
	return atof(buf);
}
std::string readstr(const char* path,int max_length)
{
	FILE* f=fopen(path,"r");
	if(!f)return LOG('W',"failed to open %s for reading: %d",path,errno),"";
	char* buf=new char[max_length+1];
	ignore_result(fgets(buf,max_length+1,f));
	buf[max_length]=0;
	fclose(f);
	std::string ret(buf);
	delete buf;
	return ret;
}
void writeint(const char* path,int v)
{
	FILE* f=fopen(path,"w");
	if(!f){LOG('W',"failed to open %s for writing",path);return;}
	ignore_result(fprintf(f,"%d",v));
	fclose(f);
}
std::string trim(std::string s)
{
	size_t st=0;
	for(;st<s.length()&&isspace(s[st]);++st);
	if(st==s.length())return "";
	s=s.substr(st);
	while(s.length()&&isspace(s.back()))s.pop_back();
	return s;
}
void split(std::string s,char c,std::vector<std::string>& v)
{
	v.clear();
	for(size_t anch=0;;)
	{
		std::string sec;
		if(s.find(c,anch)==std::string::npos)
		sec=s.substr(anch);
		else sec=s.substr(anch,s.find(c,anch)-anch);
		v.push_back(sec);
		if(s.find(c,anch)==std::string::npos)break;
		anch=s.find(c,anch)+1;
	}
}