| 1 | #cxx . . $ `<file>/src/pt2_module_saver.c`/include <ctype.h> |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <stdint.h> |
| 5 | #include <stdbool.h> |
| 6 | #include <string.h> |
| 7 | #include <sys/stat.h> |
| 8 | #include "pt2_mouse.h"cxx . . $ `<file>/src/pt2_mouse.h`/ |
| 9 | #include "pt2_textout.h"cxx . . $ `<file>/src/pt2_textout.h`/ |
| 10 | #include "pt2_helpers.h"cxx . . $ `<file>/src/pt2_helpers.h`/ |
| 11 | #include "pt2_sampler.h"cxx . . $ `<file>/src/pt2_sampler.h`/ |
| 12 | #include "pt2_config.h"cxx . . $ `<file>/src/pt2_config.h`/ |
| 13 | #include "pt2_askbox.h"cxx . . $ `<file>/src/pt2_askbox.h`/ |
| 14 | |
| 15 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! modSavecxx . . $ modSave(c6b7e35ff6de5671).(char *fileName) |
| 16 | { |
| 17 | FILEcxx . . $ FILE# *f = fopencxx . . $ fopen(82ec0787cd37e0a3).(fileName, "wb"); |
| 18 | if (f == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 19 | { |
| 20 | displayErrorMsgcxx . . $ displayErrorMsg(85c52e162fed56f9).("FILE I/O ERROR !"); |
| 21 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 22 | } |
| 23 | |
| 24 | fwritecxx . . $ fwrite(ab0609fa07c47d6e).(songcxx . . $ song.->headercxx . . $ module_t#header..namecxx . . $ moduleHeader_t#name., 1, 20, f); |
| 25 | |
| 26 | for (int32_tcxx . . $ int32_t# i = 0; i < MOD_SAMPLEScxx . . $ `src/pt2_header.h:74:9`!; i++) |
| 27 | { |
| 28 | moduleSample_tcxx . . $ moduleSample_t# *s = &songcxx . . $ song.->samplescxx . . $ module_t#samples.[i]; |
| 29 | |
| 30 | fwritecxx . . $ fwrite(ab0609fa07c47d6e).(s->textcxx . . $ moduleSample_t#text., 1, 22, f); |
| 31 | |
| 32 | uint16_tcxx . . $ uint16_t# length = SWAP16cxx . . $ `src/pt2_helpers.h:6:9`!(s->length >> 1); |
| 33 | fwritecxx . . $ fwrite(ab0609fa07c47d6e).(&length, sizeof (int16_tcxx . . $ int16_t#), 1, f); |
| 34 | |
| 35 | fputccxx . . $ fputc(b1cd03d6c470d1bd).(s->fineTunecxx . . $ moduleSample_t#fineTune. & 0xF, f); |
| 36 | fputccxx . . $ fputc(b1cd03d6c470d1bd).(((uint8_tcxx . . $ uint8_t#)s->volumecxx . . $ moduleSample_t#volume. > 64) ? 64 : s->volumecxx . . $ moduleSample_t#volume., f); |
| 37 | |
| 38 | int32_tcxx . . $ int32_t# loopStart = s->loopStartcxx . . $ moduleSample_t#loopStart.; |
| 39 | int32_tcxx . . $ int32_t# loopLength = s->loopLengthcxx . . $ moduleSample_t#loopLength.; |
| 40 | |
| 41 | if (loopLength < 2) |
| 42 | loopLength = 2; |
| 43 | |
| 44 | if (loopStart+loopLength <= 2 || loopStart+loopLength > s->lengthcxx . . $ moduleSample_t#length.) |
| 45 | { |
| 46 | loopStart = 0; |
| 47 | loopLength = 2; |
| 48 | } |
| 49 | |
| 50 | uint16_tcxx . . $ uint16_t# loopStart16 = SWAP16cxx . . $ `src/pt2_helpers.h:6:9`!(loopStart >> 1); |
| 51 | uint16_tcxx . . $ uint16_t# loopLength16 = SWAP16cxx . . $ `src/pt2_helpers.h:6:9`!(loopLength >> 1); |
| 52 | |
| 53 | fwritecxx . . $ fwrite(ab0609fa07c47d6e).(&loopStart16, sizeof (int16_tcxx . . $ int16_t#), 1, f); |
| 54 | fwritecxx . . $ fwrite(ab0609fa07c47d6e).(&loopLength16, sizeof (int16_tcxx . . $ int16_t#), 1, f); |
| 55 | } |
| 56 | |
| 57 | fputccxx . . $ fputc(b1cd03d6c470d1bd).((uint8_tcxx . . $ uint8_t#)songcxx . . $ song.->headercxx . . $ module_t#header..songLengthcxx . . $ moduleHeader_t#songLength., f); |
| 58 | fputccxx . . $ fputc(b1cd03d6c470d1bd).(0x7F, f); // ProTracker puts 0x7F at this place (restartPos/tempo in other trackers) |
| 59 | |
| 60 | for (int32_tcxx . . $ int32_t# i = 0; i < 128; i++) |
| 61 | fputccxx . . $ fputc(b1cd03d6c470d1bd).((uint8_tcxx . . $ uint8_t#)songcxx . . $ song.->headercxx . . $ module_t#header..patternTablecxx . . $ moduleHeader_t#patternTable.[i], f); |
| 62 | |
| 63 | int32_tcxx . . $ int32_t# numPatterns = 0; |
| 64 | for (int32_tcxx . . $ int32_t# i = 0; i < 128; i++) |
| 65 | { |
| 66 | if (songcxx . . $ song.->headercxx . . $ module_t#header..patternTablecxx . . $ moduleHeader_t#patternTable.[i] > numPatterns) |
| 67 | numPatterns = songcxx . . $ song.->headercxx . . $ module_t#header..patternTablecxx . . $ moduleHeader_t#patternTable.[i]; |
| 68 | } |
| 69 | |
| 70 | numPatterns++; |
| 71 | if (numPatterns > MAX_PATTERNScxx . . $ `src/pt2_header.h:75:9`!) |
| 72 | numPatterns = MAX_PATTERNScxx . . $ `src/pt2_header.h:75:9`!; |
| 73 | |
| 74 | fwritecxx . . $ fwrite(ab0609fa07c47d6e).((numPatterns <= 64) ? "M.K." : "M!K!", 1, 4, f); |
| 75 | |
| 76 | for (int32_tcxx . . $ int32_t# i = 0; i < numPatterns; i++) |
| 77 | { |
| 78 | note_tcxx . . $ note_t# *note = songcxx . . $ song.->patternscxx . . $ module_t#patterns.[i]; |
| 79 | for (int32_tcxx . . $ int32_t# j = 0; j < MOD_ROWScxx . . $ `src/pt2_header.h:73:9`! * PAULA_VOICEScxx . . $ `src/pt2_paula.h:13:9`!; j++, note++) |
| 80 | { |
| 81 | fputccxx . . $ fputc(b1cd03d6c470d1bd).((note->samplecxx . . $ note_t#sample. & 0xF0) | (note->periodcxx . . $ note_t#period. >> 8), f); |
| 82 | fputccxx . . $ fputc(b1cd03d6c470d1bd).(note->periodcxx . . $ note_t#period. & 0xFF, f); |
| 83 | fputccxx . . $ fputc(b1cd03d6c470d1bd).(((note->samplecxx . . $ note_t#sample. << 4) & 0xF0) | (note->commandcxx . . $ note_t#command. & 0x0F), f); |
| 84 | fputccxx . . $ fputc(b1cd03d6c470d1bd).(note->paramcxx . . $ note_t#param., f); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | for (int32_tcxx . . $ int32_t# i = 0; i < MOD_SAMPLEScxx . . $ `src/pt2_header.h:74:9`!; i++) |
| 89 | { |
| 90 | moduleSample_tcxx . . $ moduleSample_t# *s = &songcxx . . $ song.->samplescxx . . $ module_t#samples.[i]; |
| 91 | const int8_tcxx . . $ int8_t# *smpPtr8 = &songcxx . . $ song.->sampleDatacxx . . $ module_t#sampleData.[s->offsetcxx . . $ moduleSample_t#offset.]; |
| 92 | |
| 93 | // clear first two bytes of non-looping samples (prevents stuck beep) |
| 94 | if (s->lengthcxx . . $ moduleSample_t#length. >= 2 && s->loopStartcxx . . $ moduleSample_t#loopStart.+s->loopLengthcxx . . $ moduleSample_t#loopLength. == 2) |
| 95 | { |
| 96 | fputccxx . . $ fputc(b1cd03d6c470d1bd).(0, f); |
| 97 | fputccxx . . $ fputc(b1cd03d6c470d1bd).(0, f); |
| 98 | |
| 99 | fwritecxx . . $ fwrite(ab0609fa07c47d6e).(&smpPtr8[2], 1, s->lengthcxx . . $ moduleSample_t#length.-2, f); |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | fwritecxx . . $ fwrite(ab0609fa07c47d6e).(smpPtr8, 1, s->lengthcxx . . $ moduleSample_t#length., f); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | fclosecxx . . $ fclose(89bc1efcdac0ca22).(f); |
| 108 | |
| 109 | displayMsgcxx . . $ displayMsg(85c52e162fed56f9).("MODULE SAVED !"); |
| 110 | setMsgPointercxx . . $ setMsgPointer(49f6e7a06ebc5aa8).(); |
| 111 | |
| 112 | diskopcxx . . $ diskop..cachedcxx . . $ diskop_t#cached. = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 113 | if (uicxx . . $ ui..diskOpScreenShowncxx . . $ ui_t#diskOpScreenShown.) |
| 114 | uicxx . . $ ui..updateDiskOpFileListcxx . . $ ui_t#updateDiskOpFileList. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 115 | |
| 116 | updateWindowTitlecxx . . $ updateWindowTitle(b45ddcbc8423892c).(MOD_NOT_MODIFIEDcxx . . $ MOD_NOT_MODIFIED.); |
| 117 | return truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 118 | } |
| 119 | |
| 120 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! saveModulecxx . . $ saveModule(f2cb034b043a1ce9).(boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! checkIfFileExist, boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! giveNewFreeFilename) |
| 121 | { |
| 122 | char fileName[128], tmpBuffer[64]; |
| 123 | struct statcxx . . $ stat# statBuffer; |
| 124 | |
| 125 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(tmpBuffer, 0, sizeof (tmpBuffer)); |
| 126 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(fileName, 0, sizeof (fileName)); |
| 127 | |
| 128 | if (configcxx . . $ config..modDotcxx . . $ config_t#modDot.) |
| 129 | { |
| 130 | // extension.filename |
| 131 | if (songcxx . . $ song.->headercxx . . $ module_t#header..namecxx . . $ moduleHeader_t#name.[0] == '\0') |
| 132 | { |
| 133 | strcatcxx . . $ strcat(b7b3a83273496486).(fileName, "mod.untitled"); |
| 134 | } |
| 135 | else |
| 136 | { |
| 137 | strcatcxx . . $ strcat(b7b3a83273496486).(fileName, "mod."); |
| 138 | for (int32_tcxx . . $ int32_t# i = 4; i < 20+4; i++) |
| 139 | { |
| 140 | fileName[i] = (char)tolowercxx . . $ `<external>/17904084882347303253/ctype.h:220:11`!(song->header.name[i-4]); |
| 141 | if (fileName[i] == '\0') break; |
| 142 | sanitizeFilenameCharcxx . . $ sanitizeFilenameChar(fda57c8427c60377).(&fileName[i]); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | else |
| 147 | { |
| 148 | // filename.extension |
| 149 | if (songcxx . . $ song.->headercxx . . $ module_t#header..namecxx . . $ moduleHeader_t#name.[0] == '\0') |
| 150 | { |
| 151 | strcatcxx . . $ strcat(b7b3a83273496486).(fileName, "untitled.mod"); |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | for (int32_tcxx . . $ int32_t# i = 0; i < 20; i++) |
| 156 | { |
| 157 | fileName[i] = (char)tolowercxx . . $ `<external>/17904084882347303253/ctype.h:220:11`!(song->header.name[i]); |
| 158 | if (fileName[i] == '\0') break; |
| 159 | sanitizeFilenameCharcxx . . $ sanitizeFilenameChar(fda57c8427c60377).(&fileName[i]); |
| 160 | } |
| 161 | strcatcxx . . $ strcat(b7b3a83273496486).(fileName, ".mod"); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if (giveNewFreeFilename && statcxx . . $ stat(a9cd1e23e96f423c).(fileName, &statBuffer) == 0) |
| 166 | { |
| 167 | for (int32_tcxx . . $ int32_t# i = 1; i <= 999; i++) |
| 168 | { |
| 169 | if (configcxx . . $ config..modDotcxx . . $ config_t#modDot.) |
| 170 | { |
| 171 | // extension.filename |
| 172 | if (songcxx . . $ song.->headercxx . . $ module_t#header..namecxx . . $ moduleHeader_t#name.[0] == '\0') |
| 173 | { |
| 174 | sprintfcxx . . $ sprintf(26c8642d26409d6d).(fileName, "mod.untitled-%d", i); |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | for (int32_tcxx . . $ int32_t# j = 0; j < 20; j++) |
| 179 | { |
| 180 | tmpBuffer[j] = (char)tolowercxx . . $ `<external>/17904084882347303253/ctype.h:220:11`!(song->header.name[j]); |
| 181 | if (tmpBuffer[j] == '\0') break; |
| 182 | sanitizeFilenameCharcxx . . $ sanitizeFilenameChar(fda57c8427c60377).(&tmpBuffer[j]); |
| 183 | } |
| 184 | sprintfcxx . . $ sprintf(26c8642d26409d6d).(fileName, "mod.%s-%d", tmpBuffer, i); |
| 185 | } |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | // filename.extension |
| 190 | if (songcxx . . $ song.->headercxx . . $ module_t#header..namecxx . . $ moduleHeader_t#name.[0] == '\0') |
| 191 | { |
| 192 | sprintfcxx . . $ sprintf(26c8642d26409d6d).(fileName, "untitled-%d.mod", i); |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | for (int32_tcxx . . $ int32_t# j = 0; j < 20; j++) |
| 197 | { |
| 198 | tmpBuffer[j] = (char)tolowercxx . . $ `<external>/17904084882347303253/ctype.h:220:11`!(song->header.name[j]); |
| 199 | if (tmpBuffer[j] == '\0') break; |
| 200 | sanitizeFilenameCharcxx . . $ sanitizeFilenameChar(fda57c8427c60377).(&tmpBuffer[j]); |
| 201 | } |
| 202 | sprintfcxx . . $ sprintf(26c8642d26409d6d).(fileName, "%s-%d.mod", tmpBuffer, i); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | if (statcxx . . $ stat(a9cd1e23e96f423c).(fileName, &statBuffer) != 0) |
| 207 | break; // this filename can be used |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | if (checkIfFileExist && statcxx . . $ stat(a9cd1e23e96f423c).(fileName, &statBuffer) == 0) |
| 212 | { |
| 213 | if (!askBoxcxx . . $ askBox(7371ee129cebb502).(ASKBOX_YES_NOcxx . . $ ASKBOX_YES_NO., "OVERWRITE FILE ?")) |
| 214 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 215 | } |
| 216 | |
| 217 | return modSavecxx . . $ modSave(c6b7e35ff6de5671).(fileName); |
| 218 | } |