File Coverage

src/panda/time/util.cc
Criterion Covered Total %
statement 17 21 80.9
branch 15 32 46.8
condition n/a
subroutine n/a
pod n/a
total 32 53 60.3


line stmt bran cond sub pod time code
1             #include
2             #include
3              
4             namespace panda { namespace time {
5              
6             using panda::string;
7              
8 1291           string readfile (const std::string_view& path) {
9 1291           char spath[path.length()+1]; // need to make path null-terminated
10 1291           std::memcpy(spath, path.data(), path.length());
11 1291           spath[path.length()] = 0;
12              
13 1291 50         FILE* fh = fopen(spath, "rb");
14 1291 100         if (fh == NULL) return string();
15            
16 1241 50         if (fseek(fh, 0, SEEK_END) != 0) {
17 0 0         fclose(fh);
18 0           return string();
19             }
20            
21 1241 50         auto size = ftell(fh);
22 1241 50         if (size < 0) {
23 0 0         fclose(fh);
24 0           return string();
25             }
26            
27 1241 50         rewind(fh);
28 2532 50         string ret(size);
29 1241 50         size_t readsize = fread(ret.buf(), sizeof(char), size, fh);
    50          
30 1241 50         if (readsize != (size_t)size) return string();
31            
32 1241 50         fclose(fh);
33 1241           ret.length(readsize);
34 1241 50         return ret;
35             }
36              
37 72 50         }}
    50