1 // Copyright 2016-2017 the nyan authors, LGPLv3+. See copying.md for legal info. 2 #pragma once 3 4 5 #include <string> 6 #include <vector> 7 8 9 namespace nyan { 10 11 12 /** 13 * Represents a nyan data file. 14 */ 15 class File { 16 public: 17 File(const std::string &path); 18 File(const std::string &virtual_name, std::string &&data); 19 20 // moving allowed 21 File(File &&other) noexcept = default; 22 File& operator =(File &&other) noexcept = default; 23 24 // no copies 25 File(const File &other) = delete; 26 File &operator =(const File &other) = delete; 27 28 virtual ~File() = default; 29 30 /** 31 * Return the file name. 32 */ 33 const std::string &get_name() const; 34 35 /** 36 * Return the file content. 37 */ 38 const std::string &get_content() const; 39 40 /** 41 * Return the given line number of the file. 42 * Starts at line 1. *buhuuuuu* *sob* *mrrrmmuu* *whimper* 43 */ 44 std::string get_line(size_t n) const; 45 46 /** 47 * Return the number of lines in the file. 48 * It will always have at least one line. 49 * The empty file has one line of length 0. 50 */ 51 size_t get_line_count() const; 52 53 /** 54 * Return a c string of the file content. 55 */ 56 const char *c_str() const; 57 58 /** 59 * Return the size of the file content. 60 */ 61 size_t size() const; 62 63 protected: 64 /** 65 * Create line_ends entries from the file content. 66 */ 67 void extract_lines(); 68 69 std::string name; 70 std::string data; 71 72 /** 73 * Stores the offsets of line endings in the file content. 74 */ 75 std::vector<size_t> line_ends; 76 }; 77 78 } // namespace std 79