Skip to content

Instantly share code, notes, and snippets.

@MikuroXina
Last active April 28, 2022 09:42
Show Gist options
  • Save MikuroXina/cc78aab9bba641928fbb577939ef5a9d to your computer and use it in GitHub Desktop.
Save MikuroXina/cc78aab9bba641928fbb577939ef5a9d to your computer and use it in GitHub Desktop.
ani2ico, like the file converter with c++
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void parse(string const &name);
bool is_icon(char const *buffer);
int main(int argc, char **argv) {
if (argc != 2) {
cerr << "Please give an .ani filename as an argument!\n";
return 0;
}
parse({argv[1]});
return 0;
}
void parse(string const &name) {
if (name.find(".ani") == string::npos) {
cerr << "Please give an .ani filename as an argument!\n";
return;
}
ifstream file(name, ios::binary);
if (!file.is_open()) {
cerr << "Unable to open file " << name << "\n";
return;
}
file.seekg(0, fstream::end);
size_t fileLen = file.tellg();
file.seekg(0, fstream::beg);
vector<char> buffer(fileLen + 1);
file.read(buffer.data(), fileLen);
file.close();
string new_ico_name;
int ico_counter = 1;
for (size_t i = 0; i <= fileLen; i++) {
if (ico_counter >= 9999) {
return;
}
if ((i + 4 <= fileLen) && find_icon(&buffer[i])) {
new_ico_name = name;
new_ico_name += ico_counter;
new_ico_name += ".ico";
ico_counter++;
ofstream ico_image(new_ico_name, ios::binary);
if (!ico_image.is_open()) {
cerr << "Unable to save file " << new_ico_name << "\n";
break;
}
size_t j = 8;
while (i + j + 4 <= fileLen) {
if (is_icon(&buffer[i + j + 1]))
break;
if (j == 10) {
ico_image << static_cast<char>(0x01);
} else {
ico_image << buffer[i + j];
}
j++;
}
if (i + j <= fileLen)
ico_image << buffer[i + j];
if (fileLen - i - j <= 3) {
ico_image << buffer[i + j + 1];
ico_image << buffer[i + j + 2];
}
i += j;
}
}
}
bool is_icon(char const *buffer) {
return std::strncmp(buffer, "icon", 4);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment