Skip to content

Commit

Permalink
Move tl utils functions to tl_file_utils.
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 4b061356cc965e972af6d640507076a10743a20a
  • Loading branch information
levlam committed Mar 16, 2018
1 parent ea504ae commit 0bbad22
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 75 deletions.
2 changes: 2 additions & 0 deletions tdtl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set(TDTL_SOURCE
td/tl/tl_config.cpp
td/tl/tl_core.cpp
td/tl/tl_file_outputer.cpp
td/tl/tl_file_utils.cpp
td/tl/tl_generate.cpp
td/tl/tl_outputer.cpp
td/tl/tl_string_outputer.cpp
Expand All @@ -13,6 +14,7 @@ set(TDTL_SOURCE
td/tl/tl_config.h
td/tl/tl_core.h
td/tl/tl_file_outputer.h
td/tl/tl_file_utils.h
td/tl/tl_generate.h
td/tl/tl_outputer.h
td/tl/tl_simple.h
Expand Down
92 changes: 92 additions & 0 deletions tdtl/td/tl/tl_file_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//
// Copyright Aliaksei Levin ([email protected]), Arseny Smirnov ([email protected]) 2014-2018
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "tl_file_utils.h"

#include <cstdio>
#include <cstdlib>
#include <cstring>

namespace td {
namespace tl {

std::string get_file_contents(const std::string &file_name, const std::string &mode) {
FILE *f = std::fopen(file_name.c_str(), mode.c_str());
if (f == NULL) {
return std::string();
}

int fseek_res = std::fseek(f, 0, SEEK_END);
if (fseek_res != 0) {
std::fprintf(stderr, "Can't seek to the end of the file \"%s\"", file_name.c_str());
std::abort();
}
long size_long = std::ftell(f);
if (size_long < 0 || size_long >= (1 << 25)) {
std::fprintf(stderr, "Wrong file \"%s\" has wrong size = %ld", file_name.c_str(), size_long);
std::abort();
}
std::size_t size = static_cast<std::size_t>(size_long);

std::string result(size, ' ');
std::rewind(f);
std::size_t fread_res = std::fread(&result[0], size, 1, f);
if (size != 0 && fread_res != 1) {
std::fprintf(stderr, "Can't read file \"%s\"", file_name.c_str());
std::abort();
}
std::fclose(f);

return result;
}

bool put_file_contents(const std::string &file_name, const std::string &mode, const std::string &contents) {
FILE *f = std::fopen(file_name.c_str(), mode.c_str());
if (f == NULL) {
std::fprintf(stderr, "Can't open file \"%s\"\n", file_name.c_str());
return false;
}

std::size_t fwrite_res = std::fwrite(contents.c_str(), contents.size(), 1, f);
if (fwrite_res != 1) {
std::fclose(f);
return false;
}
if (std::fclose(f) != 0) {
return false;
}
return true;
}

std::string remove_documentation(const std::string &str) {
std::size_t line_begin = 0;
std::string result;
bool inside_documentation = false;
while (line_begin < str.size()) {
std::size_t line_end = str.find('\n', line_begin);
if (line_end == std::string::npos) {
line_end = str.size() - 1;
}
std::string line = str.substr(line_begin, line_end - line_begin + 1);
line_begin = line_end + 1;

std::size_t pos = line.find_first_not_of(' ');
if (pos != std::string::npos && ((line[pos] == '/' && line[pos + 1] == '/' && line[pos + 2] == '/') ||
(line[pos] == '/' && line[pos + 1] == '*' && line[pos + 2] == '*') ||
(inside_documentation && line[pos] == '*'))) {
inside_documentation = !(line[pos] == '/' && line[pos + 1] == '/' && line[pos + 2] == '/') &&
!(line[pos] == '*' && line[pos + 1] == '/');
continue;
}

inside_documentation = false;
result += line;
}
return result;
}

} // namespace tl
} // namespace td
21 changes: 21 additions & 0 deletions tdtl/td/tl/tl_file_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Copyright Aliaksei Levin ([email protected]), Arseny Smirnov ([email protected]) 2014-2018
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#pragma once

#include <string>

namespace td {
namespace tl {

std::string get_file_contents(const std::string &file_name, const std::string &mode);

bool put_file_contents(const std::string &file_name, const std::string &mode, const std::string &contents);

std::string remove_documentation(const std::string &str);

} // namespace tl
} // namespace td
76 changes: 1 addition & 75 deletions tdtl/td/tl/tl_generate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "tl_config.h"
#include "tl_core.h"
#include "tl_file_utils.h"
#include "tl_outputer.h"
#include "tl_string_outputer.h"
#include "tl_writer.h"
Expand Down Expand Up @@ -805,81 +806,6 @@ void write_tl(const tl_config &config, tl_outputer &out, const TL_writer &w) {
}
}

static std::string get_file_contents(const std::string &file_name, const std::string &mode) {
FILE *f = std::fopen(file_name.c_str(), mode.c_str());
if (f == NULL) {
return std::string();
}

int fseek_res = std::fseek(f, 0, SEEK_END);
if (fseek_res != 0) {
std::fprintf(stderr, "Can't seek to the end of the file \"%s\"", file_name.c_str());
std::abort();
}
long size_long = std::ftell(f);
if (size_long < 0 || size_long >= (1 << 25)) {
std::fprintf(stderr, "Wrong file \"%s\" has wrong size = %ld", file_name.c_str(), size_long);
std::abort();
}
std::size_t size = static_cast<std::size_t>(size_long);

std::string result(size, ' ');
std::rewind(f);
std::size_t fread_res = std::fread(&result[0], size, 1, f);
if (size != 0 && fread_res != 1) {
std::fprintf(stderr, "Can't read file \"%s\"", file_name.c_str());
std::abort();
}
std::fclose(f);

return result;
}

static bool put_file_contents(const std::string &file_name, const std::string &mode, const std::string &contents) {
FILE *f = std::fopen(file_name.c_str(), mode.c_str());
if (f == NULL) {
std::fprintf(stderr, "Can't open file \"%s\"\n", file_name.c_str());
return false;
}

std::size_t fwrite_res = std::fwrite(contents.c_str(), contents.size(), 1, f);
if (fwrite_res != 1) {
std::fclose(f);
return false;
}
if (std::fclose(f) != 0) {
return false;
}
return true;
}

static std::string remove_documentation(const std::string &str) {
std::size_t line_begin = 0;
std::string result;
bool inside_documentation = false;
while (line_begin < str.size()) {
std::size_t line_end = str.find('\n', line_begin);
if (line_end == std::string::npos) {
line_end = str.size() - 1;
}
std::string line = str.substr(line_begin, line_end - line_begin + 1);
line_begin = line_end + 1;

std::size_t pos = line.find_first_not_of(' ');
if (pos != std::string::npos && ((line[pos] == '/' && line[pos + 1] == '/' && line[pos + 2] == '/') ||
(line[pos] == '/' && line[pos + 1] == '*' && line[pos + 2] == '*') ||
(inside_documentation && line[pos] == '*'))) {
inside_documentation = !(line[pos] == '/' && line[pos + 1] == '/' && line[pos + 2] == '/') &&
!(line[pos] == '*' && line[pos + 1] == '/');
continue;
}

inside_documentation = false;
result += line;
}
return result;
}

tl_config read_tl_config_from_file(const std::string &file_name) {
std::string config = get_file_contents(file_name, "rb");
if (config.empty()) {
Expand Down

0 comments on commit 0bbad22

Please sign in to comment.