forked from timre13/sdl_file_chooser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
128 lines (120 loc) · 3.54 KB
/
main.cpp
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
128
#include "sdl_file_chooser.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
// Function to replace all occurrences of \n with a real line feed
std::string replaceLiteralNewlines(const std::string &str)
{
std::string result = str;
size_t pos = 0;
while ((pos = result.find("\\n", pos)) != std::string::npos)
{
result.replace(pos, 2, "\n");
pos += 1; // Advance one position after replacement
}
return result;
}
int main(int argc, char *argv[])
{
std::string directory = ".";
std::string title = FILECHOOSER_TITLE;
std::string backgroundImage;
bool recursive = false;
std::vector<std::string> customChoices;
bool useCustomChoices = false;
std::vector<std::string> filters; // Container for filters
// Parse arguments
for (int i = 1; i < argc; ++i)
{
if (strcmp(argv[i], "-d") == 0 && i + 1 < argc)
{
directory = argv[++i];
}
else if (strcmp(argv[i], "-t") == 0 && i + 1 < argc)
{
if (strlen(argv[i + 1]) > 0)
{
title = replaceLiteralNewlines(argv[++i]);
}
else
{
std::cerr << "Error: Title cannot be empty. Using default title." << std::endl;
title = " ";
}
}
else if (strcmp(argv[i], "-i") == 0 && i + 1 < argc)
{
backgroundImage = argv[++i];
}
else if (strcmp(argv[i], "-r") == 0)
{
recursive = true;
}
else if (strcmp(argv[i], "-c") == 0)
{
useCustomChoices = true;
while (i + 1 < argc && argv[i + 1][0] != '-')
{
customChoices.push_back(argv[++i]);
}
}
else if (strcmp(argv[i], "-cf") == 0)
{
// Collect choices from a file
if (++i < argc)
{
std::ifstream inputFile(argv[i]);
if (!inputFile)
{
std::cerr << "Error: Cannot open file " << argv[i] << "\n";
return 1;
}
std::string line;
while (std::getline(inputFile, line))
{
if (!line.empty())
{
customChoices.push_back(line);
}
}
useCustomChoices = true; // Ensure custom choices are used
}
else
{
std::cerr << "Error: No file specified for -cf option\n";
return 1;
}
}
else if (strcmp(argv[i], "-f") == 0)
{
// Collect filters
while (i + 1 < argc && argv[i + 1][0] != '-')
{
filters.push_back(argv[++i]);
}
}
}
// Choose the correct FileChooser constructor based on the conditions
FileChooser *fileChooser = nullptr;
if (useCustomChoices)
{
fileChooser = new FileChooser(customChoices, title, backgroundImage);
}
else
{
fileChooser = new FileChooser(directory, title, backgroundImage, recursive, filters); // Pass filters to FileChooser
}
// Get the chosen file
std::string chosenFile = fileChooser->get();
if (!chosenFile.empty())
{
std::cout << "You selected: " << chosenFile << "\n";
}
else
{
std::cout << "No file selected\n";
}
delete fileChooser; // Clean up the dynamically allocated object
return 0;
}