Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-Studio-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 : #ifndef MU_ENGRAVING_MSCIO_H
23 : #define MU_ENGRAVING_MSCIO_H
24 :
25 : #include <string>
26 :
27 : #include "io/path.h"
28 :
29 : namespace mu::engraving {
30 : //! NOTE The main format is MuseScore, is a zip archive with a specific structure
31 : static const std::string MSCZ = "mscz";
32 :
33 : //! NOTE This is the automatically generated backup file.
34 : static const std::string MSCZ_BACKUP = "mscz~";
35 :
36 : //! NOTE Before MuseScore 4, MuseScore could save data in one xml file (excluding binary) with `.mscx` extension.
37 : //! Starting from MuseScore 4, only the score domain model store to the `.mscx` file,
38 : //! and other data, such as styles, chordlist, synthesizer settings, etc. are stored in separate files.
39 : //! We can choose to save a `.mscx` file, in this case a folder with the same structure
40 : //! as a zip container will be created (i.e. an unpacked zip container)
41 : //! We can select `.mscx` to open, in this case it is now expected that it is in a folder
42 : //! in which there are also other data, structured as in a zip container.
43 : static const std::string MSCX = "mscx";
44 :
45 : //! NOTE For testing purposes and some workflows of work with score, there is a need to store all data in one text file.
46 : //! Therefore, a new file type has been introduced for this.
47 : //! This is a XML file that stores all files from the ZIP container by concatenation (excluding binaries)
48 : static const std::string MSCS = "mscs";
49 :
50 13 : inline bool isMuseScoreFile(const std::string& suffix)
51 : {
52 13 : return suffix == MSCZ || suffix == MSCX || suffix == MSCS || suffix == MSCZ_BACKUP;
53 : }
54 :
55 : enum class MscIoMode : unsigned char {
56 : Unknown = 0,
57 : Zip,
58 : Dir,
59 : XmlFile
60 : };
61 :
62 1009 : inline MscIoMode mscIoModeBySuffix(const std::string& suffix)
63 : {
64 1009 : if (suffix == MSCZ || suffix == MSCZ_BACKUP) {
65 11 : return MscIoMode::Zip;
66 998 : } else if (suffix == MSCX) {
67 998 : return MscIoMode::Dir;
68 0 : } else if (suffix == MSCS) {
69 0 : return MscIoMode::XmlFile;
70 : }
71 0 : return MscIoMode::Unknown;
72 : }
73 :
74 998 : inline muse::io::path_t containerPath(const muse::io::path_t& path)
75 : {
76 998 : if (muse::io::suffix(path) == MSCX) {
77 998 : return muse::io::absoluteDirpath(path);
78 : }
79 :
80 0 : return path;
81 : }
82 :
83 0 : inline muse::io::path_t mainFilePath(const muse::io::path_t& path)
84 : {
85 0 : if (isMuseScoreFile(muse::io::suffix(path))) {
86 0 : return path;
87 : }
88 :
89 0 : return path.appendingComponent(muse::io::filename(path)).appendingSuffix(MSCX);
90 : }
91 :
92 0 : inline muse::io::path_t mainFileName(const muse::io::path_t& path)
93 : {
94 0 : return muse::io::filename(path, !isMuseScoreFile(muse::io::suffix(path))).appendingSuffix(MSCX);
95 : }
96 : }
97 :
98 : #endif // MU_ENGRAVING_MSCIO_H
|