| 1 | /*************************************************************************** |
| 2 | * fheroes2: https://github.com/ihhub/fheroes2 * |
| 3 | * Copyright (C) 2022 - 2024 * |
| 4 | * * |
| 5 | * Free Heroes2 Engine: http://sourceforge.net/projects/fheroes2 * |
| 6 | * Copyright (C) 2009 by Andrey Afletdinov <[email protected]> * |
| 7 | * * |
| 8 | * This program is free software; you can redistribute it and/or modify * |
| 9 | * it under the terms of the GNU General Public License as published by * |
| 10 | * the Free Software Foundation; either version 2 of the License, or * |
| 11 | * (at your option) any later version. * |
| 12 | * * |
| 13 | * This program is distributed in the hope that it will be useful, * |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 | * GNU General Public License for more details. * |
| 17 | * * |
| 18 | * You should have received a copy of the GNU General Public License * |
| 19 | * along with this program; if not, write to the * |
| 20 | * Free Software Foundation, Inc., * |
| 21 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 22 | ***************************************************************************/ |
| 23 | |
| 24 | #include <algorithm> |
| 25 | #include <cstdint> |
| 26 | #include <cstdlib> |
| 27 | #include <filesystem> |
| 28 | #include <fstream> |
| 29 | #include <iostream> |
| 30 | #include <optional> |
| 31 | #include <string> |
| 32 | #include <system_error> |
| 33 | #include <type_traits> |
| 34 | #include <vector> |
| 35 | |
| 36 | #include "audio.h" |
| 37 | #include "system.h" |
| 38 | #include "tools.h" |
| 39 | |
| 40 | int main( int argc, char ** argv ) |
| 41 | { |
| 42 | if ( argc < 3 ) { |
| 43 | const std::string toolName = System::GetFileName( argv[0] ); |
| 44 | |
| 45 | std::cerr << toolName << " converts the specified XMI file(s) to MIDI format." << std::endl |
| 46 | << "Syntax: " << toolName << " dst_dir input_file.xmi ..." << std::endl; |
| 47 | return EXIT_FAILURE; |
| 48 | } |
| 49 | |
| 50 | const char * dstDir = argv[1]; |
| 51 | |
| 52 | std::vector<std::string> inputFileNames; |
| 53 | for ( int i = 2; i < argc; ++i ) { |
| 54 | if ( System::isShellLevelGlobbingSupported() ) { |
| 55 | inputFileNames.emplace_back( argv[i] ); |
| 56 | } |
| 57 | else { |
| 58 | System::globFiles( argv[i], inputFileNames ); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | std::error_code ec; |
| 63 | |
| 64 | // Using the non-throwing overloads |
| 65 | if ( !std::filesystem::exists( dstDir, ec ) && !std::filesystem::create_directories( dstDir, ec ) ) { |
| 66 | std::cerr << "Cannot create directory " << dstDir << std::endl; |
| 67 | return EXIT_FAILURE; |
| 68 | } |
| 69 | |
| 70 | uint32_t tracksConverted = 0; |
| 71 | |
| 72 | for ( const std::string & inputFileName : inputFileNames ) { |
| 73 | std::cout << "Processing " << inputFileName << "..." << std::endl; |
| 74 | |
| 75 | std::ifstream inputStream( inputFileName, std::ios_base::binary | std::ios_base::ate ); |
| 76 | if ( !inputStream ) { |
| 77 | std::cerr << "Cannot open file " << inputFileName << std::endl; |
| 78 | // A non-existent or inaccessible file is not considered a fatal error |
| 79 | continue; |
| 80 | } |
| 81 | |
| 82 | const auto size = fheroes2::checkedCast<size_t>( static_cast<std::streamoff>( inputStream.tellg() ) ); |
| 83 | if ( !size ) { |
| 84 | std::cerr << "File " << inputFileName << " is too large" << std::endl; |
| 85 | return EXIT_FAILURE; |
| 86 | } |
| 87 | |
| 88 | if ( size == 0U ) { |
| 89 | std::cerr << "File " << inputFileName << " is empty" << std::endl; |
| 90 | return EXIT_FAILURE; |
| 91 | } |
| 92 | |
| 93 | static_assert( std::is_same_v<uint8_t, unsigned char> ); |
| 94 | |
| 95 | std::vector<uint8_t> buf( size.value() ); |
| 96 | |
| 97 | inputStream.seekg( 0, std::ios_base::beg ); |
| 98 | |
| 99 | { |
| 100 | const auto streamSize = fheroes2::checkedCast<std::streamsize>( buf.size() ); |
| 101 | if ( !streamSize ) { |
| 102 | std::cerr << "File " << inputFileName << " is too large" << std::endl; |
| 103 | return EXIT_FAILURE; |
| 104 | } |
| 105 | |
| 106 | inputStream.read( reinterpret_cast<char *>( buf.data() ), streamSize.value() ); |
| 107 | } |
| 108 | |
| 109 | if ( !inputStream ) { |
| 110 | std::cerr << "Error reading from file " << inputFileName << std::endl; |
| 111 | return EXIT_FAILURE; |
| 112 | } |
| 113 | |
| 114 | buf = Music::Xmi2Mid( buf ); |
| 115 | if ( buf.empty() ) { |
| 116 | std::cerr << "Failed to convert file " << inputFileName << std::endl; |
| 117 | return EXIT_FAILURE; |
| 118 | } |
| 119 | |
| 120 | const std::filesystem::path outputFilePath = std::filesystem::path( dstDir ) / std::filesystem::path( inputFileName ).filename().replace_extension( "mid" ); |
| 121 | |
| 122 | std::ofstream outputStream( outputFilePath, std::ios_base::binary | std::ios_base::trunc ); |
| 123 | if ( !outputStream ) { |
| 124 | std::cerr << "Cannot open file " << outputFilePath << std::endl; |
| 125 | return EXIT_FAILURE; |
| 126 | } |
| 127 | |
| 128 | { |
| 129 | const auto streamSize = fheroes2::checkedCast<std::streamsize>( buf.size() ); |
| 130 | if ( !streamSize ) { |
| 131 | std::cerr << inputFileName << ": resulting MIDI is too large" << std::endl; |
| 132 | return EXIT_FAILURE; |
| 133 | } |
| 134 | |
| 135 | outputStream.write( reinterpret_cast<char *>( buf.data() ), streamSize.value() ); |
| 136 | } |
| 137 | |
| 138 | if ( !outputStream ) { |
| 139 | std::cerr << "Error writing to file " << outputFilePath << std::endl; |
| 140 | return EXIT_FAILURE; |
| 141 | } |
| 142 | |
| 143 | ++tracksConverted; |
| 144 | } |
| 145 | |
| 146 | std::cout << "Total converted tracks: " << tracksConverted << std::endl; |
| 147 | |
| 148 | return EXIT_SUCCESS; |
| 149 | } |