forked from CelestiaProject/Celestia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptmenu.cpp
More file actions
108 lines (93 loc) · 2.87 KB
/
Copy pathscriptmenu.cpp
File metadata and controls
108 lines (93 loc) · 2.87 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
// scriptmenu.cpp
//
// Copyright (C) 2007-2009, the Celestia Development Team
//
// Scan a directory and build a list of Celestia script files.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#include "scriptmenu.h"
#include <iostream>
#include <fmt/printf.h>
#include <celcompat/filesystem.h>
#include <celutil/filetype.h>
#include <celutil/gettext.h>
#include <fstream>
using namespace std;
static const char TitleTag[] = "Title:";
static void process(const fs::path& p, vector<ScriptMenuItem>* menuItems)
{
auto type = DetermineFileType(p);
#ifndef CELX
if (type != Content_CelestiaLegacyScript)
#else
if (type != Content_CelestiaScript &&
type != Content_CelestiaLegacyScript)
#endif
return;
// Scan the script file for metainformation. At the moment,
// the only thing searched for is the script title, which must
// appear on the first line after the string 'Title:'
ifstream in(p.string());
if (in.good())
{
ScriptMenuItem item;
item.filename = p;
// Read the first line, handling various newline conventions
char firstLineBuf[512];
size_t count = 0;
while (count < sizeof(firstLineBuf) - 1 && in.good())
{
int c = in.get();
if (c == '\n' || c == '\r')
break;
firstLineBuf[count++] = c;
}
string firstLine(firstLineBuf, count);
auto titlePos = firstLine.find(TitleTag);
// Skip spaces after the Title: tag
if (titlePos != string::npos)
titlePos = firstLine.find_first_not_of(' ', titlePos + (sizeof(TitleTag) - 1));
if (titlePos != string::npos)
{
item.title = firstLine.substr(titlePos);
}
else
{
// No title tag--just use the filename
item.title = p.filename().string();
}
menuItems->push_back(item);
}
}
vector<ScriptMenuItem>*
ScanScriptsDirectory(const fs::path& scriptsDir, bool deep)
{
vector<ScriptMenuItem>* scripts = new vector<ScriptMenuItem>;
if (scriptsDir.empty())
return scripts;
std::error_code ec;
if (!fs::is_directory(scriptsDir, ec))
{
fmt::fprintf(cerr, _("Path %s doesn't exist or isn't a directory"), scriptsDir);
return scripts;
}
if (deep)
{
auto iter = fs::recursive_directory_iterator(scriptsDir, ec);
for (; iter != end(iter); iter.increment(ec))
{
if (ec)
continue;
process(*iter, scripts);
}
}
else
{
for (const auto& p : fs::directory_iterator(scriptsDir, ec))
process(p, scripts);
}
return scripts;
}