Welcome to mirror list, hosted at ThFree Co, Russian Federation.

file_manager.h « src « Alohalytics « 3party - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7240033a2e49062f43660ae4f2fa1bee90d02b7a (plain)
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
/*******************************************************************************
The MIT License (MIT)

Copyright (c) 2015 Alexander Zolotarev <me@alex.bio> from Minsk, Belarus

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*******************************************************************************/

#ifndef FILE_MANAGER_HPP
#define FILE_MANAGER_HPP

#include <string>
#include <functional>
#include <fstream>

namespace alohalytics {

// Functions are wrapped into the class for convenience.
struct FileManager {
  // Initialized separately for each platform.
  static const char kDirectorySeparator;

  // Checks and appends if necessary platform-dependent slash at the end of the
  // path.
  static void AppendDirectorySlash(std::string & directory) {
    // Fix missing slash if necessary.
    if (!directory.empty() && directory.back() != kDirectorySeparator) {
      directory.push_back(kDirectorySeparator);
    }
  }

  // Returns empty string for empty file_path.
  // Returns "." if file_path contains file name only.
  // Otherwise returns path with a slash at the end and without file name in it.
  static std::string GetDirectoryFromFilePath(std::string file_path) {
    if (file_path.empty()) {
      return std::string();
    }
    std::string::size_type slash = file_path.find_last_of(kDirectorySeparator);
    if (slash == std::string::npos) {
      return std::string(".");
    }
    return file_path.erase(slash + 1);
  }

  // Creates a new file or appends string to an existing one.
  // Returns false on error.
  static bool AppendStringToFile(const std::string & str, const std::string & file_path) {
    return std::ofstream(file_path, std::ofstream::app | std::ofstream::binary)
        .write(str.data(), str.size())
        .flush()
        .good();
  }

  // Returns file content as a string or empty string on error.
  static std::string ReadFileAsString(std::string const & file_path) {
    const int64_t size = GetFileSize(file_path);
    if (size > 0) {
      std::ifstream fi(file_path, std::ifstream::binary);
      std::string buffer(static_cast<const size_t>(size), '\0');
      if (fi.read(&buffer[0], static_cast<std::streamsize>(size)).good()) {
        return buffer;
      }
    }
    return std::string();
  }

  // Executes lambda for each regular file in the directory and stops
  // immediately if lambda returns false.
  static void ForEachFileInDir(std::string directory, std::function<bool(const std::string & full_path)> lambda);

  // Returns negative value on error and if full_path_to_file is a directory.
  static int64_t GetFileSize(const std::string & full_path_to_file);
};

}  // namespace alohalytics

#endif  // FILE_MANAGER_HPP