1 //
2 // Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 #include "td/tl/tl_file_utils.h"
8 
9 #include <cstdio>
10 #include <cstdlib>
11 
12 namespace td {
13 namespace tl {
14 
get_file_contents(const std::string & file_name,const std::string & mode)15 std::string get_file_contents(const std::string &file_name, const std::string &mode) {
16   FILE *f = std::fopen(file_name.c_str(), mode.c_str());
17   if (f == NULL) {
18     return std::string();
19   }
20 
21   int fseek_res = std::fseek(f, 0, SEEK_END);
22   if (fseek_res != 0) {
23     std::fprintf(stderr, "Can't seek to the end of the file \"%s\"", file_name.c_str());
24     std::abort();
25   }
26   long size_long = std::ftell(f);
27   if (size_long < 0 || size_long >= (1 << 25)) {
28     std::fprintf(stderr, "Wrong file \"%s\" has wrong size = %ld", file_name.c_str(), size_long);
29     std::abort();
30   }
31   std::size_t size = static_cast<std::size_t>(size_long);
32 
33   std::string result(size, ' ');
34   if (size != 0) {
35     std::rewind(f);
36     std::size_t fread_res = std::fread(&result[0], size, 1, f);
37     if (fread_res != 1) {
38       std::fprintf(stderr, "Can't read file \"%s\"", file_name.c_str());
39       std::abort();
40     }
41   }
42   std::fclose(f);
43 
44   return result;
45 }
46 
put_file_contents(const std::string & file_name,const std::string & mode,const std::string & contents)47 bool put_file_contents(const std::string &file_name, const std::string &mode, const std::string &contents) {
48   FILE *f = std::fopen(file_name.c_str(), mode.c_str());
49   if (f == NULL) {
50     std::fprintf(stderr, "Can't open file \"%s\"\n", file_name.c_str());
51     return false;
52   }
53 
54   std::size_t fwrite_res = std::fwrite(contents.c_str(), contents.size(), 1, f);
55   if (fwrite_res != 1) {
56     std::fclose(f);
57     return false;
58   }
59   if (std::fclose(f) != 0) {
60     return false;
61   }
62   return true;
63 }
64 
remove_documentation(const std::string & str)65 std::string remove_documentation(const std::string &str) {
66   std::size_t line_begin = 0;
67   std::string result;
68   bool inside_documentation = false;
69   while (line_begin < str.size()) {
70     std::size_t line_end = str.find('\n', line_begin);
71     if (line_end == std::string::npos) {
72       line_end = str.size() - 1;
73     }
74     std::string line = str.substr(line_begin, line_end - line_begin + 1);
75     line_begin = line_end + 1;
76 
77     std::size_t pos = line.find_first_not_of(' ');
78     if (pos != std::string::npos && ((line[pos] == '/' && line[pos + 1] == '/' && line[pos + 2] == '/') ||
79                                      (line[pos] == '/' && line[pos + 1] == '*' && line[pos + 2] == '*') ||
80                                      (inside_documentation && line[pos] == '*'))) {
81       inside_documentation = !(line[pos] == '/' && line[pos + 1] == '/' && line[pos + 2] == '/') &&
82                              !(line[pos] == '*' && line[pos + 1] == '/');
83       continue;
84     }
85 
86     inside_documentation = false;
87     result += line;
88   }
89   return result;
90 }
91 
92 }  // namespace tl
93 }  // namespace td
94