104 lines
1.8 KiB
C++
104 lines
1.8 KiB
C++
|
#include <iostream>
|
||
|
#include <fstream>
|
||
|
|
||
|
using namespace std;
|
||
|
|
||
|
const size_t MAXLINE = 256;
|
||
|
|
||
|
void init_key(char key[8])
|
||
|
{
|
||
|
for (size_t i = 0; i < 8; i++)
|
||
|
key[i] = 'A' + ::rand() % 26;
|
||
|
}
|
||
|
|
||
|
bool encode_string(const char* linein, char lineout[MAXLINE])
|
||
|
{
|
||
|
memset(lineout, 0, MAXLINE);
|
||
|
if (linein && *linein)
|
||
|
{
|
||
|
char key[8]; init_key(key);
|
||
|
size_t i;
|
||
|
for (i = 0; linein[i]; i++)
|
||
|
lineout[i] = linein[i] + (i < 8 ? key[i] : linein[i - 8]);
|
||
|
lineout[i] = '\0';
|
||
|
}
|
||
|
return *lineout >= ' ';
|
||
|
}
|
||
|
|
||
|
bool read_line(istream& txt, char line[MAXLINE])
|
||
|
{
|
||
|
memset(line, 0, MAXLINE);
|
||
|
while (!txt.eof())
|
||
|
{
|
||
|
txt.getline(line, MAXLINE-1);
|
||
|
|
||
|
if (line[0] == '/' && line[1] == '/')
|
||
|
continue;
|
||
|
|
||
|
for (size_t i = 0; line[i]; i++)
|
||
|
{
|
||
|
if (line[i] > ' ')
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
bool write_line(ostream& zip, const char* line)
|
||
|
{
|
||
|
bool ok = line && *line;
|
||
|
if (ok)
|
||
|
{
|
||
|
while (*line && *line <= ' ')
|
||
|
line++;
|
||
|
ok = *line > ' ';
|
||
|
if (ok)
|
||
|
{
|
||
|
char lineout[MAXLINE];
|
||
|
encode_string(line, lineout);
|
||
|
zip << lineout << endl;
|
||
|
}
|
||
|
}
|
||
|
return ok;
|
||
|
}
|
||
|
|
||
|
/* May be useful at least once
|
||
|
bool write_paragraph(ostream& zip, const char* line)
|
||
|
{
|
||
|
bool ok = line && *line;
|
||
|
if (ok)
|
||
|
zip << '[' << line << ']' << endl;
|
||
|
return ok;
|
||
|
}
|
||
|
*/
|
||
|
|
||
|
int main(int argc, char* argv[])
|
||
|
{
|
||
|
if (argc <= 1)
|
||
|
{
|
||
|
cerr << "Missing input file (dninst.txt)" << endl;
|
||
|
return 1;
|
||
|
}
|
||
|
if (argc <= 2)
|
||
|
{
|
||
|
cerr << "Missing output file (dninst.zip)" << endl;
|
||
|
return 2;
|
||
|
}
|
||
|
cerr << "Encoding " << argv[1] << " ..." << endl;
|
||
|
|
||
|
ifstream txt(argv[1]);
|
||
|
ofstream zip(argv[2]);
|
||
|
char line[MAXLINE];
|
||
|
|
||
|
read_line(txt, line);
|
||
|
::srand(883);
|
||
|
write_line(zip, line);
|
||
|
::srand(atoi(line));
|
||
|
while (read_line(txt, line))
|
||
|
write_line(zip, line);
|
||
|
|
||
|
cerr << "Encoded " << argv[2] << endl;
|
||
|
|
||
|
return 0;
|
||
|
}
|