-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
77 lines (68 loc) · 1.42 KB
/
util.cpp
File metadata and controls
77 lines (68 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "util.h"
#include<fcntl.h>
#include<cassert>
#include<unistd.h>
using namespace std;
void tab(std::ostream& o,unsigned i){
while(i--) o<<"\t";
}
std::string slurp(std::string const& filename){
std::ifstream f(filename.c_str());
if(!f.good()){
throw File_not_found(filename);
}
std::stringstream ss;
while(f>>ss.rdbuf());
return ss.str();
}
void write_out(string const& filename,string const& data){
int mode=S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH;//the permissions for the file if a new one is created.
int fd=open(filename.c_str(),O_WRONLY|O_CREAT|O_TRUNC,mode);
assert(fd!=-1);
unsigned i=0;
while(i<data.size()){
auto r=write(fd,data.c_str()+i,data.size()-i);
assert(r>0);
i+=r;
}
int r=close(fd);
assert(!r);
}
bool prefix(string const& in,string const& val){
if(in.size()<val.size()) return 0;
for(unsigned i=0;i<val.size();i++){
if(in[i]!=val[i]) return 0;
}
return 1;
}
string cat(string const& a,string const& b){
stringstream ss;
ss<<a<<b;
return ss.str();
}
string cat(string const& a,char b){
stringstream ss;
ss<<a<<b;
return ss.str();
}
string cat(char a,char b){
stringstream ss;
ss<<a<<b;
return ss.str();
}
vector<string> lines(string const& s){
vector<string> r;
const char *at=s.c_str(),*begin=s.c_str();
while(*at){
while(*at && *at!='\n'){
at++;
}
if(*at=='\n'){
r.push_back(string(begin,at));
at++;
begin=at;
}
}
r.push_back(string(begin,at));
return r;
}