]> git.sesse.net Git - s573compress/commitdiff
Move the compressor into a separate function, callable from the outside as a library.
authorSteinar H. Gunderson <steinar+ddr@gunderson.no>
Tue, 26 Nov 2019 17:59:43 +0000 (18:59 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 26 Nov 2019 17:59:43 +0000 (18:59 +0100)
compress.cpp

index dc4594f8aa0d8cf755d629777bbb5b33614b8df4..4117a4fe7d139b25e9067c369889fb2562198370 100644 (file)
 
 using namespace std;
 
-static string read_file(const char *filename)
-{
-       FILE *fp = fopen(filename, "rb");
-       if (fp == nullptr) {
-               perror(filename);
-               exit(1);
-       }
-
-       string ret;
-       while (!feof(fp)) {
-               int ch = getc(fp);
-               if (ch == -1) {
-                       break;
-               }
-               ret.push_back(ch);
-       }
-
-       fclose(fp);
-
-       return ret;
-}
-
 static uint16_t read16(const char *ptr)
 {
        // We don't care about the endianness, so native is fine.
@@ -110,14 +88,8 @@ static void consider_transition(int src_pos, int dst_pos, TagType tag_type, int
        (*states)[dst_pos].match_distance = match_distance;
 }
 
-int main(int argc, char **argv)
+string compress_konamilz77(const string &input)
 {
-       if (argc != 3) {
-               fprintf(stderr, "USAGE: compress INFILE OUTFILE\n");
-               exit(1);
-       }
-       string input = read_file(argv[1]);
-
        // First hash all possible 2-byte matches in the file.
        unordered_map<uint16_t, vector<int>> match_to_pos;
        for (size_t i = 0; i < input.size() - 2; ++i) {
@@ -193,7 +165,7 @@ int main(int argc, char **argv)
                        }
                }
 
-               if (pos % 100 == 0) {
+               if (pos % 1000 == 0) {
                        fprintf(stderr, "Compressing %d/%zu (%.2f%%)...\r", pos, input.size(), 100.0 * pos / input.size());
                }
        }
@@ -260,6 +232,41 @@ int main(int argc, char **argv)
        output.push_back(0xff);  // EOF tag.
        output[control_byte_pos] |= (1u << control_bits_used);
 
+       return output;
+}
+
+#ifdef COMPRESS_MAIN
+static string read_file(const char *filename)
+{
+       FILE *fp = fopen(filename, "rb");
+       if (fp == nullptr) {
+               perror(filename);
+               exit(1);
+       }
+
+       string ret;
+       while (!feof(fp)) {
+               int ch = getc(fp);
+               if (ch == -1) {
+                       break;
+               }
+               ret.push_back(ch);
+       }
+
+       fclose(fp);
+
+       return ret;
+}
+
+int main(int argc, char **argv)
+{
+       if (argc != 3) {
+               fprintf(stderr, "USAGE: compress INFILE OUTFILE\n");
+               exit(1);
+       }
+       string input = read_file(argv[1]);
+       string output = compress_konamilz77(input);
+
        fprintf(stderr, "End result: %zu bytes.              \n", output.size());
 
        // Write the file to disk.
@@ -277,3 +284,4 @@ int main(int argc, char **argv)
                exit(1);
        }
 }
+#endif