-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles.cpp
More file actions
127 lines (110 loc) · 3.59 KB
/
Copy pathfiles.cpp
File metadata and controls
127 lines (110 loc) · 3.59 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include<iostream>
#include<vector>
#include<fstream>
#include<string>
#include<filesystem>
#include<algorithm>
#include "files.h"
#include "parsing.h"
#include "logging.h"
#include "play.h"
#include "platform_utils.h"
using namespace std;
namespace fs = std::filesystem;
vector<string> readFile(string fileName) {
dbg(LOG_NOTE, "Reading file: " + fileName);
ifstream file(fileName);
vector<string> lines;
string line;
if (file.is_open()) {
while (getline(file, line)) {
lines.push_back(line);
}
file.close();
} else {
cerr << "Failed to open file: " << fileName << endl;
}
return lines;
}
vector<string> sitsToFileStrings(vector<Situation> sits) {
vector<string> stringsToAdd;
for (Situation sit : sits) {
stringsToAdd.push_back("S" + to_string(sit.situationNum) + ". " + sit.title);
for (Option opt : sit.options) {
stringsToAdd.push_back("O" + to_string(opt.referSituationNum) + ". " + opt.optionText);
}
}
return stringsToAdd;
}
void writeFile(vector<string> lines, string filename, bool saveToDocuments) {
// Determine target path
fs::path filePath(filename);
if (filePath.extension() != ".story") {
filePath += ".story";
}
if (saveToDocuments) {
std::string documentsDir = PlatformUtils::getDocumentsPath();
filePath = fs::path(documentsDir) / filePath.filename();
}
ofstream file(filePath, ofstream::out | ofstream::trunc);
if (!file.is_open()) {
dbg(LOG_ERROR, "ERROR opening file: " + filePath.string());
cerr << "Error opening file: " << filePath.string() << endl;
return;
}
for (const auto& line : lines) {
file << line << "\n";
}
file.close();
}
vector<string> getStoryFilesFromDir(string dir) {
vector<string> fileNames;
try {
fs::path path(dir);
if (!fs::exists(path) || !fs::is_directory(path)) {
throw runtime_error("Invalid directory path: " + dir);
}
// Iterate through directory entries
for (const auto& entry : fs::directory_iterator(path)) {
// Check if it's a regular file and has .story extension
if (entry.is_regular_file() && entry.path().extension() == ".story") {
// Add the full path as string to our vector
fileNames.push_back(entry.path().string());
}
}
// Sort the files alphabetically
sort(fileNames.begin(), fileNames.end());
} catch (const exception& e) {
throw runtime_error("Error scanning directory: " + string(e.what()));
}
return fileNames;
}
string getShortFileName(const string& path) {
return fs::path(path).filename().string();
}
void selectNewFile(string& storyPath, vector<string>& lines, vector<Situation>& story, string homeDir) {
// get files from home dir
dbg(LOG_NOTE, "Getting .story files from home dir " + homeDir);
vector<string> fileNames = getStoryFilesFromDir(homeDir);
if (fileNames.size() == 0) {
dbg(LOG_ERROR, "No files in home directory");
// ERROR SAY TO USER
cout << "\033[2J\033[H"; // clear screen
cout << "No files found in documents folder, create a new story or download testStory.story from the website.";
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return;
}
vector<string> shortFileNames;
for (int i = 0; i < fileNames.size(); i++) {
dbg(LOG_NOTE, "Found story file: " + fileNames[i]);
shortFileNames.push_back(getShortFileName(fileNames[i]));
}
dbg(LOG_NOTE, "Asking what file...");
size_t selectedFileIndex = showOptions(shortFileNames, "Select file:");
// set as new story
dbg(LOG_WARNING, "Setting " + fileNames[selectedFileIndex] + " as new story.");
storyPath = fileNames[selectedFileIndex];
lines = readFile(storyPath);
story = parseStoryFile(lines);
dbg(LOG_NOTE, "Successfully set as new story, going back to main menu.");
}