Modernize symal source code.

Part of symal was still written in K&R C (though with an anachronistic C99
version comment!).  Updating it to ANSI C, adding const in many places for
clarity.  Reordering functions to eliminate forward declarations.

Moving definition of Cmd_T into cmd.c, since it's local to that file.
Adding some wrappers to reduce the casts around Cmd_T.p.  Narrowing the
scope of some variables.

Also updating the C++: use anonymous namespace for "static" definitions,
use enums and constants instead of preprocessor macros, use false/true as
boolean constants, throw exceptions instead of print-and-exit, avoid use of
"final" as an identifier since it's now a reserved word.
This commit is contained in:
Jeroen Vermeulen 2015-05-10 15:28:28 +07:00
parent 85c1af4d72
commit 91e699f90d
3 changed files with 612 additions and 627 deletions

File diff suppressed because it is too large Load Diff

View File

@ -5,47 +5,32 @@
#define CMD_H
#define CMDDOUBLETYPE 1
#define CMDENUMTYPE 2
#define CMDINTTYPE 3
#define CMDSTRINGTYPE 4
#define CMDSUBRANGETYPE 5
#define CMDGTETYPE 6
#define CMDLTETYPE 7
#define CMDSTRARRAYTYPE 8
#define CMDBOOLTYPE 9
enum CommandType
{
CMDDOUBLETYPE = 1,
CMDENUMTYPE,
CMDINTTYPE,
CMDSTRINGTYPE,
CMDSUBRANGETYPE,
CMDGTETYPE,
CMDLTETYPE,
CMDSTRARRAYTYPE,
CMDBOOLTYPE
};
typedef struct {
const char *Name;
int Idx;
} Enum_T;
typedef struct {
int Type;
char *Name,
*ArgStr;
void *Val,
*p;
} Cmd_T;
#ifdef __cplusplus
extern "C" {
#endif
#if defined(__STDC__)
int DeclareParams(char *, ...);
#else
int DeclareParams();
#endif
int GetParams(int *n, char ***a,char *CmdFileName),
SPrintParams(),
PrintParams();
int DeclareParams(const char *, ...);
int GetParams(int *n, char ***a, const char *CmdFileName);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -5,6 +5,7 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <list>
#include <vector>
@ -15,21 +16,24 @@
using namespace std;
#define MAX_WORD 10000 // maximum lengthsource/target strings
#define MAX_M 400 // maximum length of source strings
#define MAX_N 400 // maximum length of target strings
const int MAX_WORD = 10000; // maximum lengthsource/target strings
const int MAX_M = 400; // maximum length of source strings
const int MAX_N = 400; // maximum length of target strings
#define UNION 1
#define INTERSECT 2
#define GROW 3
#define SRCTOTGT 4
#define TGTTOSRC 5
#define BOOL_YES 1
#define BOOL_NO 0
enum Alignment
{
UNION = 1,
INTERSECT,
GROW,
SRCTOTGT,
TGTTOSRC,
};
#define END_ENUM { (char*)0, 0 }
const Enum_T END_ENUM = {'\0', 0};
static Enum_T AlignEnum [] = {
namespace
{
Enum_T AlignEnum [] = {
{ "union", UNION },
{ "u", UNION },
{ "intersect", INTERSECT},
@ -43,18 +47,16 @@ static Enum_T AlignEnum [] = {
END_ENUM
};
static Enum_T BoolEnum [] = {
{ "true", BOOL_YES },
{ "yes", BOOL_YES },
{ "y", BOOL_YES },
{ "false", BOOL_NO },
{ "no", BOOL_NO },
{ "n", BOOL_NO },
Enum_T BoolEnum [] = {
{ "true", true },
{ "yes", true },
{ "y", true },
{ "false", false },
{ "no", false },
{ "n", false },
END_ENUM
};
// global variables and constants
int* fa; //counters of covered foreign positions
@ -117,7 +119,7 @@ int getals(istream& inp,int& m, int *a,int& n, int *b)
} else
return 0;
};
}
//compute union alignment
@ -226,7 +228,7 @@ int printsrctotgt(ostream& out,int m,int *a,int n,int* b)
//to represent the grow alignment as the unionalignment of a
//directed and inverted alignment
int printgrow(ostream& out,int m,int *a,int n,int* b, bool diagonal=false,bool final=false,bool bothuncovered=false)
int printgrow(ostream& out,int m,int *a,int n,int* b, bool diagonal=false,bool isfinal=false,bool bothuncovered=false)
{
ostringstream sout;
@ -322,7 +324,7 @@ int printgrow(ostream& out,int m,int *a,int n,int* b, bool diagonal=false,bool f
}
}
if (final) {
if (isfinal) {
for (k=unionalignment.begin(); k!=unionalignment.end(); k++)
if (A[k->first][k->second]==1) {
point.first=k->first;
@ -383,6 +385,7 @@ int printgrow(ostream& out,int m,int *a,int n,int* b, bool diagonal=false,bool f
return 1;
}
} // namespace
//Main file here
@ -395,7 +398,7 @@ int main(int argc, char** argv)
char* input= NULL;
char* output= NULL;
int diagonal=false;
int final=false;
int isfinal=false;
int bothuncovered=false;
@ -403,8 +406,8 @@ int main(int argc, char** argv)
"alignment", CMDENUMTYPE, &alignment, AlignEnum,
"d", CMDENUMTYPE, &diagonal, BoolEnum,
"diagonal", CMDENUMTYPE, &diagonal, BoolEnum,
"f", CMDENUMTYPE, &final, BoolEnum,
"final", CMDENUMTYPE, &final, BoolEnum,
"f", CMDENUMTYPE, &isfinal, BoolEnum,
"final", CMDENUMTYPE, &isfinal, BoolEnum,
"b", CMDENUMTYPE, &bothuncovered, BoolEnum,
"both", CMDENUMTYPE, &bothuncovered, BoolEnum,
"i", CMDSTRINGTYPE, &input,
@ -412,9 +415,9 @@ int main(int argc, char** argv)
"v", CMDENUMTYPE, &verbose, BoolEnum,
"verbose", CMDENUMTYPE, &verbose, BoolEnum,
(char*)NULL);
NULL);
GetParams(&argc, &argv, (char*)NULL);
GetParams(&argc, &argv, NULL);
if (alignment==0) {
cerr << "usage: symal [-i=<inputfile>] [-o=<outputfile>] -a=[u|i|g] -d=[yes|no] -b=[yes|no] -f=[yes|no] \n"
@ -426,21 +429,17 @@ int main(int argc, char** argv)
istream *inp = &std::cin;
ostream *out = &std::cout;
try
{
if (input) {
fstream *fin = new fstream(input,ios::in);
if (!fin->is_open()) {
cerr << "cannot open " << input << "\n";
exit(1);
}
if (!fin->is_open()) throw runtime_error("cannot open " + string(input));
inp = fin;
}
if (output) {
fstream *fout = new fstream(output,ios::out);
if (!fout->is_open()) {
cerr << "cannot open " << output << "\n";
exit(1);
}
if (!fout->is_open()) throw runtime_error("cannot open " + string(output));
out = fout;
}
@ -472,11 +471,11 @@ int main(int argc, char** argv)
break;
case GROW:
cerr << "symal: computing grow alignment: diagonal ("
<< diagonal << ") final ("<< final << ")"
<< diagonal << ") final ("<< isfinal << ")"
<< "both-uncovered (" << bothuncovered <<")\n";
while(getals(*inp,m,a,n,b))
printgrow(*out,m,a,n,b,diagonal,final,bothuncovered);
printgrow(*out,m,a,n,b,diagonal,isfinal,bothuncovered);
break;
case TGTTOSRC:
@ -498,7 +497,7 @@ int main(int argc, char** argv)
cerr << "Sents: " << sents << endl;
break;
default:
exit(1);
throw runtime_error("Unknown alignment");
}
delete [] fa;
@ -512,6 +511,12 @@ int main(int argc, char** argv)
if (out != &std::cout) {
delete inp;
}
}
catch (const std::exception &e)
{
cerr << e.what() << std::endl;
exit(1);
}
exit(0);
}