-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.cpp
67 lines (54 loc) · 1.37 KB
/
utils.cpp
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
#include "utils.hpp"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <cstring>
#include <algorithm>
std::string confPath(GeanyData* geany, std::string name){
char * t = g_build_filename(geany->app->configdir, "plugins", "gycm", name.c_str(), NULL);
std::string x(t);
g_free(t);
return x;
}
bool fileExists(std::string name){
return access(name.c_str(), F_OK) != -1;
}
std::string slurp(std::string fname){
FILE* f = fopen(fname.c_str(), "r");
fseek(f, 0, SEEK_END);
size_t size = ftell(f);
char* data = new char[size];
rewind(f);
fread(data, sizeof(char), size, f);
std::string s(data,size);
delete[] data;
return s;
}
int getFreePort(){
int sockfd;
sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd < 0){
return -1;
}
struct sockaddr_in serv_addr;
memset(&serv_addr,0,sizeof(struct sockaddr_in));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = 0;
serv_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(sockfd,(struct sockaddr *) &serv_addr,sizeof(struct sockaddr_in)) < 0){
return -1;
}
if(listen(sockfd,1) == -1) {
return -1;
}
socklen_t len = sizeof(struct sockaddr_in);
if(getsockname(sockfd, (struct sockaddr *) &serv_addr, &len) < 0){
return -1;
}
close(sockfd);
return ntohs(serv_addr.sin_port);
}
std::string strToLower(std::string s){
std::transform(s.begin(), s.end(), s.begin(), ::tolower);
return s;
}