forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilename_formatter.cpp
More file actions
117 lines (98 loc) · 2.97 KB
/
filename_formatter.cpp
File metadata and controls
117 lines (98 loc) · 2.97 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
// Aseprite
// Copyright (C) 2001-2016 David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/filename_formatter.h"
#include "base/convert_to.h"
#include "base/fs.h"
#include "base/replace_string.h"
#include <cstdlib>
#include <cstring>
#include <vector>
namespace app {
static bool replace_frame(const char* frameKey, // E.g. = "{frame"
int frameBase,
std::string& str)
{
size_t i = str.find(frameKey);
if (i != std::string::npos) {
int keyLen = std::strlen(frameKey);
size_t j = str.find("}", i+keyLen);
if (j != std::string::npos) {
std::string from = str.substr(i, j - i + 1);
if (frameBase >= 0) {
std::vector<char> to(32);
int offset = std::strtol(from.c_str()+keyLen, NULL, 10);
std::sprintf(&to[0], "%0*d", (int(j)-int(i+keyLen)), frameBase + offset);
base::replace_string(str, from, &to[0]);
}
else
base::replace_string(str, from, "");
}
return true;
}
else
return false;
}
std::string filename_formatter(
const std::string& format,
FilenameInfo& info,
bool replaceFrame)
{
const std::string& filename = info.filename();
std::string path = base::get_file_path(filename);
if (path.empty())
path = ".";
std::string output = format;
base::replace_string(output, "{fullname}", filename);
base::replace_string(output, "{path}", path);
base::replace_string(output, "{name}", base::get_file_name(filename));
base::replace_string(output, "{title}", base::get_file_title(filename));
base::replace_string(output, "{extension}", base::get_file_extension(filename));
base::replace_string(output, "{layer}", info.layerName());
if (replaceFrame) {
base::replace_string(output, "{tag}", info.innerTagName());
base::replace_string(output, "{innertag}", info.innerTagName());
base::replace_string(output, "{outertag}", info.outerTagName());
replace_frame("{frame", info.frame(), output);
replace_frame("{tagframe", info.tagFrame(), output);
}
return output;
}
std::string set_frame_format(
const std::string& format,
const std::string& newFrameFormat)
{
std::string output = format;
size_t i = output.find("{frame");
if (i != std::string::npos) {
size_t j = output.find("}", i+6);
if (j != std::string::npos) {
output.replace(i, j - i + 1, newFrameFormat);
}
}
return output;
}
std::string add_frame_format(
const std::string& format,
const std::string& newFrameFormat)
{
std::string output = format;
size_t i = output.find("{frame");
size_t j = output.find("{tagframe");
if (i == std::string::npos &&
j == std::string::npos) {
output =
base::join_path(
base::get_file_path(format),
base::get_file_title(format))
+ newFrameFormat + "." +
base::get_file_extension(format);
}
return output;
}
} // namespace app