⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rlep.cpp

📁 非常好用的五子棋游戏源码
💻 CPP
字号:
// Created:11-20-98
// By Jeff Connelly

// RLEP - RLE Packer

#include "comprlib.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>

// Returns the size of 'fp'
long file_length(FILE* fp)
{
    long pos, ret;
    pos = ftell(fp);
    fseek(fp, 0, SEEK_END);
    ret = ftell(fp);
    fseek(fp, pos, SEEK_SET);
    return ret;
}

const char* help =
    "RLE Packer, packes files using RLE method\n"
    "\tRLEP files...\n"
    "Multible files may be specified.  Output extension is .RLE\n"
    "\n";

int main(int argc, char* argv[])
{
    register int i;
    FILE* in;
    FILE* out;
    char* infn;
    char* outfn;
    char* buf;
    char* inbuf;
    char c;
    long inlen;

    if (argc == 1)
    {
        printf(help);
        exit (0);
    }

    // We could use file I/O, but memory I/O is faster
    ComprLibMemIO::Set();


    for (i = 1; i < argc; ++i)
    {
        // Copy 'fn' from 'argv[i]'
        infn = xmalloc(strlen(argv[i]));
        strcpy (infn, argv[i]);

        printf ("\nPacking %s...\t\t", infn);
        in = fopen(infn, "rb");
        if (!in)
        {
            printf ("Cannot open %s", infn);
            xfree(infn);
            continue;
        }

        // Figure out the output filename, with .RLE
        outfn = xmalloc(strlen(infn));     // Copy 'outfn' to 
        strcpy (outfn, infn);              // 'infn' and allocate memory

        if (!strchr(infn, '.'))     // No extension, just tack on .RLE
            strcat(outfn, ".RLE");
        else                        // Replace the extension with .RLE
        {
            buf = strchr(infn, '.');
            strcpy (buf, ".RLE");
        }
        if (__file_exists(outfn))
        {
            printf ("File already exists! [R]eplace, E[x]it, Re[n]ame, [S]kip");
            c = toupper(getc(stdin));
            if (c == 'X')
                exit (2);
            else if (c == 'S')
                continue;
            else if (c == 'N')
            {
                printf ("Name: ");
                gets(outfn);
            }
            else if (c == 'R')
                ;
            else
                exit(2);
        }                

        out = fopen(outfn, "wb");

        // Load the file into memory
        inlen = file_length(in);
        inbuf = xmalloc(inlen);
        fread (inbuf, inlen, 1, in);

        // Set up ComprLib
        ComprLibMemIO::source_ptr = inbuf;
        ComprLibMemIO::dest_ptr = NULL;
        ComprLibMemIO::source_len = inlen;
        ComprLibMemIO::dest_len = 0;

        rle1_encode();      // Encode it!

        // Write the output buffer
        fwrite (ComprLibMemIO::dest_ptr, 1, ComprLibMemIO::dest_len, out);

        // Clean up all the stuff
        xfree(inbuf);
        xfree(ComprLibMemIO::dest_ptr);
        xfree(infn);
        fclose(in);
        fclose(out);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -