Skip to content

Commit

Permalink
Corrected memory link and finally added help screen; this program nee…
Browse files Browse the repository at this point in the history
…ds a lot more work in the future
  • Loading branch information
drojaazu committed Apr 28, 2019
1 parent 5b89f18 commit a9cc184
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 109 deletions.
18 changes: 15 additions & 3 deletions include/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,26 @@
* \version 1.0
* \date 2017.12.30
* \copyright GNU Public License
*/

*/
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include <vector>
#include <getopt.h>

#include "jis_enc.h"
#include "shift_jis.h"

/*!
* \enum encodings
* \brief List of supported JIS encodings
*/
*/

enum encodings { shift_jis_enc, cp932_enc, euc_enc };

void process_args(int argc, char **argv);

void print_help();
228 changes: 122 additions & 106 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,119 +1,135 @@
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include <vector>

#include "main.h"
#include "jis_enc.h"
#include "shift_jis.h"

using namespace std;

istream* indata = nullptr;
const string version = string("0.2");

istream *indata = nullptr;
size_t min_len = 0;
encodings enc = shift_jis_enc;
bool big_endian = true;
bool jisx0213 = false;

int main(int argc, char** argv)
{
process_args(argc, argv);

if(indata == nullptr) indata = &cin;
else {
if(!indata->good()) {
cerr << "File could not be opened" << endl;
return 2;
}
indata->seekg(0);
}

jis_enc* encoding = nullptr;

switch(enc) {
case shift_jis_enc:
encoding = new shift_jis(indata);
break;
default:
cerr << "Encoding not yet supported" << endl;
return 3;
}

// set up command line values
if(min_len > 0) encoding->set_min_len(min_len);
encoding->set_is_big_endian(big_endian);
encoding->set_use_jisx0213(jisx0213);

vector<found_string>& res = *encoding->find();
found_string thisstring;
cout << showbase << internal << setfill('0') << hex;

for(size_t siter = 0; siter < res.size(); siter++) {
thisstring = res[siter];
cout << thisstring.address << " " << &thisstring.data[0] << endl;
}

if(indata != &cin) delete indata;
delete encoding;

return 0;
int main(int argc, char **argv) {
process_args(argc, argv);

if (indata == nullptr)
indata = &cin;
else {
if (!indata->good()) {
cerr << "File could not be opened" << endl;
return 2;
}
indata->seekg(0);
}

jis_enc *encoding = nullptr;

switch (enc) {
case shift_jis_enc:
encoding = new shift_jis(indata);
break;
default:
cerr << "Encoding not yet supported" << endl;
return 3;
}

// set up command line values
if (min_len > 0)
encoding->set_min_len(min_len);
encoding->set_is_big_endian(big_endian);
encoding->set_use_jisx0213(jisx0213);

auto res = encoding->find();
found_string thisstring;
cout << showbase << internal << setfill('0') << hex;

for (size_t siter = 0; siter < res->size(); siter++) {
thisstring = res->at(siter);
cout << thisstring.address << " " << &thisstring.data[0] << endl;
}

if (indata != &cin)
delete indata;
delete encoding;
delete res;

return 0;
}

void process_args(int argc, char** argv) {

// TODO: add option for double-byte strings bias
// OPTION -d - prefer double-byte strings




const char* const short_opts = ":m:e:l";
const option long_opts[] = {
{"min-length", required_argument, nullptr, 'm'},
{"encoding", required_argument, nullptr, 'e'},
{"little-endian", no_argument, nullptr, 'l'},
{"jisx0213", no_argument, nullptr, 'x'}
};

int this_opt;

while((this_opt = getopt_long(argc, argv, short_opts, long_opts, nullptr)) != -1) {
switch(this_opt) {
case 'm':
// OPTION -m - set minimum length of string
min_len = strtoul(optarg, nullptr, 10);
break;
case 'e':
// OPTION -e - set encoding
// TODO: move encodings into a map
if(!strcmp(optarg, "shift-jis") || !strcmp(optarg, "sjis"))
enc = shift_jis_enc;
else {
cerr << "Unsupported encoding, defaulting to Shift-JIS" << endl;
}
break;
case 'l':
// OPTION -l - little-endian
big_endian = false;
break;
case 'x':
// OPTION -jisx0213 - Use JIS X 0213 character set
jisx0213 = true;
break;
case '?':
cerr << "Unknown option: " << (char)optopt << endl;
break;
case ':':
cerr << "Missing arg for option: " << (char)optopt << endl;
break;
}
}

if(optind < argc) {
// only read the first non-option argument, assuming it is input filename
indata = new ifstream(argv[optind]);
}
void process_args(int argc, char **argv) {
// TODO: add option for double-byte strings bias
// OPTION -d - prefer double-byte strings

const char *const short_opts = ":hm:e:lx";
const option long_opts[] = {{"help", no_argument, nullptr, 'h'},
{"min-length", required_argument, nullptr, 'm'},
{"encoding", required_argument, nullptr, 'e'},
{"little-endian", no_argument, nullptr, 'l'},
{"jisx0213", no_argument, nullptr, 'x'},
{nullptr, 0, nullptr, 0}};

while (true) {
const auto this_opt =
getopt_long(argc, argv, short_opts, long_opts, nullptr);

if (this_opt == -1)
break;

switch (this_opt) {
case 'm':
// OPTION -m - set minimum length of string
min_len = strtoul(optarg, nullptr, 10);
break;
case 'e':
// OPTION -e - set encoding
// TODO: move encodings into a map
if (!strcmp(optarg, "shift-jis") || !strcmp(optarg, "sjis"))
enc = shift_jis_enc;
else {
cerr << "Unsupported encoding, defaulting to Shift-JIS" << endl;
}
break;
case 'l':
// OPTION -l - little-endian
big_endian = false;
break;
case 'x':
// OPTION -jisx0213 - Use JIS X 0213 character set
jisx0213 = true;
break;
case 'h':
print_help();
exit(0);
break;
case ':':
cerr << "Missing argument" << endl;
print_help();
exit(1);
break;
case '?':
cerr << "Invalid option" << endl;
print_help();
exit(1);
break;
default:
print_help();
exit(1);
break;
}
}

if (optind < argc) {
// only read the first non-option argument, assuming it is input filename
indata = new ifstream(argv[optind]);
}
}

void print_help() {
cerr << "jstrings version " << version << endl << endl;
cerr << "Valid options:" << endl;
cerr << " --encoding, -e Specify encoding to use" << endl;
cerr << " --jisx0213 Include JIS X 0213 definitions" << endl;
cerr << " --help, -h Display this text" << endl;
}

0 comments on commit a9cc184

Please sign in to comment.