| 41 | |
| 42 | |
| 43 | bool FileUtils::ReadFile(const char *path, string *content) { |
| 44 | char newpath[1024]{0}; |
| 45 | if ('~' == path[0]) { |
| 46 | snprintf(newpath, sizeof(newpath), "%s%s", getenv("HOME"), path + 1); |
| 47 | } else { |
| 48 | snprintf(newpath, sizeof(newpath), "%s", path); |
| 49 | } |
| 50 | |
| 51 | bool ret = false; |
| 52 | |
| 53 | int fd = ::open(newpath, O_RDONLY); |
| 54 | if (fd >= 0) { |
| 55 | struct stat file_stat; |
| 56 | if (0 == fstat(fd, &file_stat)) { |
| 57 | content->resize(file_stat.st_size); |
| 58 | |
| 59 | if (read(fd, (char *) content->data(), file_stat.st_size) == file_stat.st_size) { |
| 60 | ret = true; |
| 61 | } else { |
| 62 | phxrpc::log(LOG_ERR, "WARN: read( ..., %llu ) fail, errno %d, %s", |
| 63 | (unsigned long long) file_stat.st_size, errno, strerror(errno)); |
| 64 | } |
| 65 | } else { |
| 66 | phxrpc::log(LOG_ERR, "WARN: stat %s fail, errno %d, %s", newpath, errno, strerror(errno)); |
| 67 | } |
| 68 | |
| 69 | close(fd); |
| 70 | } else { |
| 71 | phxrpc::log(LOG_ERR, "WARN: open %s fail, errno %d, %s", newpath, errno, strerror(errno)); |
| 72 | } |
| 73 | |
| 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | void FileUtils::StrSplitList(const string &str, const string &delimiters, |
| 78 | vector<string> &results) { |