-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUtils.cpp
138 lines (117 loc) · 2.83 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//
// Created by yuanyu on 2018.01.24.
//
#include <thread>
#include <iostream>
#include <sys/stat.h>
#include <sys/file.h>
#include <fcntl.h>
#include <cerrno>
#include "Utils.h"
#include "config.h"
int mkpath(std::string s, mode_t mode)
{
size_t pre = 0;
size_t pos;
std::string dir;
int mdret;
if (s[s.size()-1] != '/')
s +='/';
while (true)
{
pos = s.find_first_of('/', pre);
if (pos == std::string::npos)
break;
dir = s.substr(0, ++pos);
pre = pos;
if ( dir.empty() )
continue;
mdret = ::mkdir(dir.c_str(), mode);
if (mdret && errno != EEXIST)
{
return mdret;
}
}
return mdret;
}
int next_file_id(std::string fn)
{
FILE *fp = fopen(fn.c_str(), "r+");
if (fp == nullptr)
{
fp = fopen(fn.c_str(), "w+");
if (fp == nullptr)
return -1;
}
auto next_id = 0;
auto fd = fileno(fp);
flock(fd, LOCK_EX); //文件加锁
fseek(fp, 0L, SEEK_END);
auto size = ftell(fp);
if ( size != 0)
{
fseek(fp, 0L, SEEK_SET);
char buf[64] = {0};
fread(buf, 63, 1, fp);
next_id = atoi(buf);
}
++next_id;
std::string s_id = std::to_string(next_id);
freopen(fn.c_str(), "w+", fp);
fwrite(s_id.c_str(), s_id.size(), 1, fp);
fclose(fp); //关闭文件
flock(fd, LOCK_UN); //释放文件锁
return next_id;
}
int get_file_id(std::string fn)
{
FILE *fp = fopen(fn.c_str(), "r+");
if (fp == nullptr)
{
fp = fopen(fn.c_str(), "w+");
if (fp == nullptr)
return -1;
}
auto next_id = 0;
auto fd = fileno(fp);
flock(fd, LOCK_EX); //文件加锁
fseek(fp, 0L, SEEK_END);
auto size = ftell(fp);
if ( size != 0)
{
fseek(fp, 0L, SEEK_SET);
char buf[64] = {0};
fread(buf, 63, 1, fp);
next_id = atoi(buf);
}
fclose(fp); //关闭文件
flock(fd, LOCK_UN); //释放文件锁
return next_id;
}
int get_file_id(int width, int height)
{
return get_file_id(get_data_folder(width, height) + "/id.txt");
}
void folder_init(int width, int height)
{
mkpath(get_model_folder());
mkpath(get_sgf_folder(width, height));
mkpath(get_data_folder(width, height));
mkpath(get_rd_folder(width, height));
}
std::string get_model_folder()
{
return "./" + FOLDER_MODEL;
}
std::string get_data_folder(int width, int height)
{
return "./" + FOLDER_DATA + "/" + std::to_string(height) + 'x' + std::to_string(width);
}
std::string get_rd_folder(int width, int height)
{
return "./" + FOLDER_RD + "/" + std::to_string(height) + 'x' + std::to_string(width);
}
std::string get_sgf_folder(int width, int height)
{
return "./" + FOLDER_SGF + "/" + std::to_string(height) + 'x' + std::to_string(width);
}