move to cmake

This commit is contained in:
2024-06-03 09:59:35 -05:00
parent d1c056e734
commit 087f132a32
41 changed files with 558 additions and 367 deletions

107
examples/CMakeLists.txt Normal file
View File

@@ -0,0 +1,107 @@
add_executable(bdiff
examples/bdiff.cpp
examples/common.cpp
)
target_include_directories(bdiff BEFORE PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/examples
)
add_executable(fpatch
examples/fpatch.cpp
examples/common.cpp
)
target_include_directories(fpatch BEFORE PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/examples
)
add_executable(intdiff
examples/intdiff.cpp
examples/common.cpp
)
target_include_directories(intdiff BEFORE PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/examples
)
add_executable(intdiff3
examples/intdiff3.cpp
examples/common.cpp
)
target_include_directories(intdiff3 BEFORE PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/examples
)
add_executable(patch
examples/patch.cpp
examples/common.cpp
)
target_include_directories(patch BEFORE PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/examples
)
add_executable(st2ses
examples/st2ses.cpp
examples/common.cpp
)
target_include_directories(st2ses BEFORE PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/examples
)
add_executable(strdiff
examples/strdiff.cpp
examples/common.cpp
)
target_include_directories(strdiff BEFORE PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/examples
)
add_executable(strdiff3
examples/strdiff3.cpp
examples/common.cpp
)
target_include_directories(strdiff3 BEFORE PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/examples
)
add_executable(strdiff_cp
examples/strdiff_cp.cpp
examples/common.cpp
)
target_include_directories(strdiff_cp BEFORE PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/examples
)
add_executable(strdiff_storage
examples/strdiff_storage.cpp
examples/common.cpp
)
target_include_directories(strdiff_storage BEFORE PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/examples
)
add_executable(unidiff
examples/unidiff.cpp
examples/common.cpp
)
target_include_directories(unidiff BEFORE PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/examples
)
add_executable(unistrdiff
examples/unistrdiff.cpp
examples/common.cpp
)
target_include_directories(unistrdiff BEFORE PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/examples
)

View File

@@ -1,43 +0,0 @@
import os
# SConstruct for dtl examples
env = Environment(CPPPATH='..')
debug = ARGUMENTS.get('debug', 'n')
if debug == 'y' or debug == 'yes':
env.Append(CPPFLAGS = ['-Wall', '-g'])
else:
env.Append(CPPFLAGS = ['-Wall', '-O2'])
if os.sys.platform != "win32":
env.Append(CPPDEFINES = ['HAVE_UNISTD_H'])
conf = Configure(env);
if not conf.CheckCXX():
print("The C++ compiler is not installed!")
Exit(1)
libs = ['stdc++']
for lib in libs:
if not conf.CheckLib(lib):
print("library " + lib + " not installed!")
Exit(1)
conf.Finish()
targets = { 'strdiff' : ['strdiff.cpp', 'common.cpp'], # diff between two string sequences
'intdiff' : ['intdiff.cpp'], # diff between two integer sequences
'unidiff' : ['unidiff.cpp', 'common.cpp'], # unified diff between two files
'unistrdiff' : ['unistrdiff.cpp', 'common.cpp'], # unified diff between two strings
'bdiff' : ['bdiff.cpp', 'common.cpp'], # diff between two byte sequences
'strdiff3' : ['strdiff3.cpp', 'common.cpp'], # three-way string diff program using dtl
'intdiff3' : ['intdiff3.cpp'], # three-way integer diff program using dtl
'patch' : ['patch.cpp', 'common.cpp'], # string patch program using dtl
'fpatch' : ['fpatch.cpp', 'common.cpp'], # file patch program using dtl
'st2ses' : ['st2ses.cpp', 'common.cpp'], # convert SES format file to SES instance
'strdiff_cp' : ['strdiff_cp.cpp', 'common.cpp'], # diff between two string sequences with custom printer
'strdiff_storage' : ['strdiff_storage.cpp', 'common.cpp'], # diff between two string sequences with custom storage
}
[ env.Program(target, targets[target]) for target in targets ]

View File

@@ -1,24 +1,9 @@
#include "common.hpp"
#include <dtl/dtl.hpp>
#include <cstdio>
#include <fcntl.h>
#include <iostream>
#include <string>
#include <sys/stat.h>
#include <sys/types.h>
#include <vector>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif // HAVE_UNISTD_H
using namespace std;
using dtl::Diff;
typedef unsigned char elem;
typedef vector<elem> sequence;
typedef std::vector<elem> sequence;
static int create_byte_seq(const char *fs, sequence &seq);
static int create_byte_seq(const char *fs, sequence &seq) {
@@ -26,7 +11,7 @@ static int create_byte_seq(const char *fs, sequence &seq) {
int siz;
elem buf[BUFSIZ];
if ((fd = open(fs, O_RDONLY)) == -1) {
cout << "Opening failed." << endl;
std::cout << "Opening failed." << std::endl;
return -1;
}
while ((siz = read(fd, buf, sizeof(buf))) > 0) {
@@ -36,7 +21,7 @@ static int create_byte_seq(const char *fs, sequence &seq) {
}
if (siz < 0) {
close(fd);
cout << "Read error." << endl;
std::cout << "Read error." << std::endl;
return -1;
}
close(fd);
@@ -46,12 +31,12 @@ static int create_byte_seq(const char *fs, sequence &seq) {
int main(int argc, char *argv[]) {
if (isFewArgs(argc)) {
cerr << "Too few arguments." << endl;
std::cerr << "Too few arguments." << std::endl;
return -1;
}
string fs1(argv[1]);
string fs2(argv[2]);
std::string fs1(argv[1]);
std::string fs2(argv[2]);
sequence seq1;
sequence seq2;
@@ -62,9 +47,9 @@ int main(int argc, char *argv[]) {
d.compose();
if (d.getEditDistance() == 0) {
cout << fs1 << " is the same as " << fs2 << endl;
std::cout << fs1 << " is the same as " << fs2 << std::endl;
} else {
cout << fs1 << " is different from " << fs2 << endl;
std::cout << fs1 << " is different from " << fs2 << std::endl;
}
return 0;

View File

@@ -1,7 +1,6 @@
#include "common.hpp"
bool isFileExist(string &fs) {
bool isFileExist(std::string &fs) {
FILE *fp;
if ((fp = fopen(fs.c_str(), "r")) == NULL) {
return false;

View File

@@ -1,13 +1,9 @@
#ifndef DTL_EXAMPLE_COMMON_H
#define DTL_EXAMPLE_COMMON_H
#include <cstdio>
#include <string>
#include "dtl.hpp"
using namespace std;
bool isFileExist(string &fs);
bool isFileExist(std::string &fs);
bool isFewArgs(int argc, int limit = 3);
#endif

View File

@@ -1,34 +1,25 @@
#include "common.hpp"
#include <cassert>
#include <dtl/dtl.hpp>
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
using dtl::Diff;
int main(int argc, char *argv[]) {
if (isFewArgs(argc)) {
cerr << "Too few arguments." << endl;
std::cerr << "Too few arguments." << std::endl;
return -1;
}
string A(argv[1]);
string B(argv[2]);
std::string A(argv[1]);
std::string B(argv[2]);
bool fileExist = true;
if (!isFileExist(A)) {
cerr << "file A does not exist" << endl;
std::cerr << "file A does not exist" << std::endl;
fileExist = false;
}
if (!isFileExist(B)) {
cerr << "file B does not exist" << endl;
std::cerr << "file B does not exist" << std::endl;
fileExist = false;
}
@@ -36,14 +27,14 @@ int main(int argc, char *argv[]) {
return -1;
}
typedef string elem;
typedef vector<elem> sequence;
typedef std::string elem;
typedef std::vector<elem> sequence;
ifstream Aifs(A.c_str());
ifstream Bifs(B.c_str());
std::ifstream Aifs(A.c_str());
std::ifstream Bifs(B.c_str());
elem buf;
sequence ALines, BLines;
ostringstream ossLine, ossInfo;
std::ostringstream ossLine, ossInfo;
while (getline(Aifs, buf)) {
ALines.push_back(buf);
@@ -60,14 +51,14 @@ int main(int argc, char *argv[]) {
// fpatch
assert(BLines == s2);
cout << "fpatch succeeded" << endl;
std::cout << "fpatch succeeded" << std::endl;
d.composeUnifiedHunks();
sequence s3 = d.uniPatch(s1);
// unipatch
assert(BLines == s3);
cout << "unipatch succeeded" << endl;
std::cout << "unipatch succeeded" << std::endl;
return 0;
}

View File

@@ -1,9 +1,4 @@
#include <dtl/dtl.hpp>
#include <iostream>
#include <vector>
using namespace std;
#include "common.hpp"
using dtl::Diff;
@@ -14,16 +9,16 @@ int main(int, char **) {
int asiz = sizeof(a) / sizeof(int);
int bsiz = sizeof(b) / sizeof(int);
for (int i = 0; i < asiz; ++i) {
cout << a[i] << " ";
std::cout << a[i] << " ";
}
cout << endl;
std::cout << std::endl;
for (int i = 0; i < bsiz; ++i) {
cout << b[i] << " ";
std::cout << b[i] << " ";
}
cout << endl;
std::cout << std::endl;
typedef int elem;
typedef vector<int> sequence;
typedef std::vector<int> sequence;
sequence A(&a[0], &a[asiz]);
sequence B(&b[0], &b[bsiz]);
@@ -31,18 +26,18 @@ int main(int, char **) {
d.compose();
// editDistance
cout << "editDistance:" << d.getEditDistance() << endl;
std::cout << "editDistance:" << d.getEditDistance() << std::endl;
// Longest Common Subsequence
sequence lcs_v = d.getLcsVec();
cout << "LCS: ";
std::cout << "LCS: ";
for (sequence::iterator vit = lcs_v.begin(); vit != lcs_v.end(); ++vit) {
cout << *vit << " ";
std::cout << *vit << " ";
}
cout << endl;
std::cout << std::endl;
// Shortest Edit Script
cout << "SES" << endl;
std::cout << "SES" << std::endl;
d.printSES();
return 0;

View File

@@ -1,10 +1,4 @@
#include <cassert>
#include <dtl/dtl.hpp>
#include <iostream>
#include <vector>
using namespace std;
#include "common.hpp"
using dtl::Diff3;
@@ -15,24 +9,24 @@ int main(int, char **) {
int c[10] = {1, 2, 3, 9, 5, 6, 7, 8, 9, 10};
int answer[10] = {1, 2, 3, 9, 5, 6, 7, 3, 9, 10};
cout << "a:";
std::cout << "a:";
for (int i = 0; i < 10; ++i) {
cout << a[i] << " ";
std::cout << a[i] << " ";
}
cout << endl;
cout << "b:";
std::cout << std::endl;
std::cout << "b:";
for (int i = 0; i < 10; ++i) {
cout << b[i] << " ";
std::cout << b[i] << " ";
}
cout << endl;
cout << "c:";
std::cout << std::endl;
std::cout << "c:";
for (int i = 0; i < 10; ++i) {
cout << c[i] << " ";
std::cout << c[i] << " ";
}
cout << endl;
std::cout << std::endl;
typedef int elem;
typedef vector<int> sequence;
typedef std::vector<int> sequence;
sequence A(&a[0], &a[10]);
sequence B(&b[0], &b[10]);
sequence C(&c[0], &c[10]);
@@ -40,18 +34,18 @@ int main(int, char **) {
Diff3<elem> diff3(A, B, C);
diff3.compose();
if (!diff3.merge()) {
cerr << "conflict." << endl;
std::cerr << "conflict." << std::endl;
return -1;
}
sequence s = diff3.getMergedSequence();
cout << "s:";
std::cout << "s:";
for (sequence::iterator it = s.begin(); it != s.end(); ++it) {
cout << *it << " ";
std::cout << *it << " ";
}
cout << endl;
std::cout << std::endl;
assert(s == Answer);
cout << "intdiff3 OK" << endl;
std::cout << "intdiff3 OK" << std::endl;
return 0;
}

View File

@@ -1,23 +1,16 @@
#include "common.hpp"
#include <cassert>
#include <dtl/dtl.hpp>
#include <iostream>
#include <vector>
using namespace std;
using dtl::Diff;
int main(int argc, char *argv[]) {
if (isFewArgs(argc)) {
cerr << "Too few arguments." << endl;
std::cerr << "Too few arguments." << std::endl;
return -1;
}
typedef char elem;
typedef string sequence;
typedef std::string sequence;
sequence A(argv[1]);
sequence B(argv[2]);
@@ -30,15 +23,15 @@ int main(int argc, char *argv[]) {
d.composeUnifiedHunks();
sequence s3 = d.uniPatch(s1);
cout << "before:" << s1 << endl;
cout << "after :" << s2 << endl;
std::cout << "before:" << s1 << std::endl;
std::cout << "after :" << s2 << std::endl;
assert(B == s2);
cout << "patch succeeded" << endl;
std::cout << "patch succeeded" << std::endl;
cout << "before:" << s1 << endl;
cout << "after :" << s3 << endl;
std::cout << "before:" << s1 << std::endl;
std::cout << "after :" << s3 << std::endl;
assert(B == s3);
cout << "unipatch succeeded" << endl;
std::cout << "unipatch succeeded" << std::endl;
return 0;
}

View File

@@ -1,9 +1,9 @@
#ifndef DTL_PRINTERS
#define DTL_PRINTERS
#include <dtl/dtl.hpp>
#include "common.hpp"
template <typename sesElem, typename stream = ostream>
template <typename sesElem, typename stream = std::ostream>
class customChangePrinter : public dtl::Printer<sesElem, stream> {
public:
customChangePrinter() : dtl::Printer<sesElem, stream>() {}

View File

@@ -1,35 +1,27 @@
#include "common.hpp"
#include <dtl/dtl.hpp>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
using namespace dtl;
int main(int argc, char *argv[]) {
if (isFewArgs(argc, 2)) {
cerr << "Too few arguments." << endl;
std::cerr << "Too few arguments." << std::endl;
return -1;
}
typedef string elem;
typedef vector<string> sequence;
typedef std::string elem;
typedef std::vector<string> sequence;
string s(argv[1]);
std::string s(argv[1]);
if (!isFileExist(s)) {
cerr << s << " is invalid." << endl;
std::cerr << s << " is invalid." << std::endl;
return -1;
}
ifstream fs(s.c_str());
std::ifstream fs(s.c_str());
const Ses<elem> ses =
Diff<elem, sequence>::composeSesFromStream<ifstream>(fs);
Diff<elem, sequence>::composeSesFromStream<std::ifstream>(fs);
dtl::Diff<elem, sequence>::printSES(ses);
return 0;

View File

@@ -1,7 +1,7 @@
#ifndef DTL_STORAGE
#define DTL_STORAGE
#include <dtl/dtl.hpp>
#include "common.hpp"
template <typename sesElem, typename storedData>
class CustomStorage : public dtl::Storage<sesElem, storedData> {

View File

@@ -1,23 +1,16 @@
#include "common.hpp"
#include <dtl/dtl.hpp>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using dtl::Diff;
int main(int argc, char *argv[]) {
if (isFewArgs(argc)) {
cerr << "Too few arguments." << endl;
std::cerr << "Too few arguments." << std::endl;
return -1;
}
typedef char elem;
typedef string sequence;
typedef std::string sequence;
sequence A(argv[1]);
sequence B(argv[2]);
@@ -27,15 +20,15 @@ int main(int argc, char *argv[]) {
d.compose();
// editDistance
cout << "editDistance:" << d.getEditDistance() << endl;
std::cout << "editDistance:" << d.getEditDistance() << std::endl;
// Longest Common Subsequence
vector<elem> lcs_v = d.getLcsVec();
std::vector<elem> lcs_v = d.getLcsVec();
sequence lcs_s(lcs_v.begin(), lcs_v.end());
cout << "LCS:" << lcs_s << endl;
std::cout << "LCS:" << lcs_s << std::endl;
// Shortest Edit Script
cout << "SES" << endl;
std::cout << "SES" << std::endl;
d.printSES();
return 0;

View File

@@ -1,23 +1,16 @@
#include "common.hpp"
#include <dtl/dtl.hpp>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using dtl::Diff3;
int main(int argc, char *argv[]) {
if (isFewArgs(argc, 4)) {
cerr << "Too few arguments." << endl;
std::cerr << "Too few arguments." << std::endl;
return -1;
}
typedef char elem;
typedef string sequence;
typedef std::string sequence;
sequence A(argv[1]);
sequence B(argv[2]);
@@ -26,10 +19,10 @@ int main(int argc, char *argv[]) {
Diff3<elem, sequence> diff3(A, B, C);
diff3.compose();
if (!diff3.merge()) {
cerr << "conflict." << endl;
std::cerr << "conflict." << std::endl;
return 0;
}
cout << "result:" << diff3.getMergedSequence() << endl;
std::cout << "result:" << diff3.getMergedSequence() << std::endl;
return 0;
}

View File

@@ -1,24 +1,18 @@
#include "common.hpp"
#include <dtl/dtl.hpp>
#include <iostream>
#include <sstream>
#include <string>
#include "printers.hpp"
using namespace std;
using dtl::Diff;
int main(int argc, char *argv[]) {
if (isFewArgs(argc)) {
cerr << "Too few arguments." << endl;
std::cerr << "Too few arguments." << std::endl;
return -1;
}
typedef char elem;
typedef string sequence;
typedef std::string sequence;
sequence A(argv[1]);
sequence B(argv[2]);
@@ -27,9 +21,9 @@ int main(int argc, char *argv[]) {
d.compose();
// Shortest Edit Script
cout << "SES" << endl;
std::cout << "SES" << std::endl;
d.printSES<ostream, customChangePrinter>(cout);
d.printSES<std::ostream, customChangePrinter>(std::cout);
return 0;
}

View File

@@ -1,23 +1,18 @@
#include "common.hpp"
#include <dtl/dtl.hpp>
#include <iostream>
#include <string>
#include "storage.hpp"
using namespace std;
using dtl::Diff;
int main(int argc, char *argv[]) {
if (isFewArgs(argc)) {
cerr << "Too few arguments." << endl;
std::cerr << "Too few arguments." << std::endl;
return -1;
}
typedef char elem;
typedef string sequence;
typedef std::string sequence;
sequence A(argv[1]);
sequence B(argv[2]);
@@ -26,13 +21,13 @@ int main(int argc, char *argv[]) {
d.compose();
// Shortest Edit Script
cout << "SES" << endl;
std::cout << "SES" << std::endl;
string result;
std::string result;
d.storeSES<string, CustomStorage>(result);
d.storeSES<std::string, CustomStorage>(result);
cout << result;
std::cout << result;
return 0;
}

View File

@@ -1,24 +1,13 @@
#include "common.hpp"
#include <dtl/dtl.hpp>
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
#include <sys/stat.h>
#include <time.h>
using namespace std;
using dtl::Diff;
using dtl::elemInfo;
using dtl::uniHunk;
static void showStats(string fp1, string fp2);
static void unifiedDiff(string fp1, string fp2);
static void showStats(std::string fp1, std::string fp2);
static void unifiedDiff(std::string fp1, std::string fp2);
static void showStats(string fp1, string fp2) {
static void showStats(std::string fp1, std::string fp2) {
const int MAX_LENGTH = 255;
char time_format[] = "%Y-%m-%d %H:%M:%S %z";
time_t rawtime[2];
@@ -26,11 +15,11 @@ static void showStats(string fp1, string fp2) {
struct stat st[2];
if (stat(fp1.c_str(), &st[0]) == -1) {
cerr << "argv1 is invalid." << endl;
std::cerr << "argv1 is invalid." << std::endl;
exit(-1);
}
if (stat(fp2.c_str(), &st[1]) == -1) {
cerr << "argv2 is invalid" << endl;
std::cerr << "argv2 is invalid" << std::endl;
exit(-1);
}
@@ -38,20 +27,20 @@ static void showStats(string fp1, string fp2) {
rawtime[0] = st[0].st_mtime;
timeinfo[0] = localtime(&rawtime[0]);
strftime(buf[0], MAX_LENGTH, time_format, timeinfo[0]);
cout << "--- " << fp1 << '\t' << buf[0] << endl;
std::cout << "--- " << fp1 << '\t' << buf[0] << std::endl;
rawtime[1] = st[1].st_mtime;
timeinfo[1] = localtime(&rawtime[1]);
strftime(buf[1], MAX_LENGTH, time_format, timeinfo[1]);
cout << "+++ " << fp2 << '\t' << buf[1] << endl;
std::cout << "+++ " << fp2 << '\t' << buf[1] << std::endl;
}
static void unifiedDiff(string fp1, string fp2) {
typedef string elem;
typedef vector<elem> sequence;
typedef pair<elem, elemInfo> sesElem;
static void unifiedDiff(std::string fp1, std::string fp2) {
typedef std::string elem;
typedef std::vector<elem> sequence;
typedef std::pair<elem, elemInfo> sesElem;
ifstream Aifs(fp1.c_str());
ifstream Bifs(fp2.c_str());
std::ifstream Aifs(fp1.c_str());
std::ifstream Bifs(fp2.c_str());
elem buf;
sequence ALines, BLines;
@@ -80,21 +69,21 @@ static void unifiedDiff(string fp1, string fp2) {
int main(int argc, char *argv[]) {
if (isFewArgs(argc)) {
cerr << "Too few arguments." << endl;
std::cerr << "Too few arguments." << std::endl;
return -1;
}
string s1(argv[1]);
string s2(argv[2]);
std::string s1(argv[1]);
std::string s2(argv[2]);
bool fileExist = true;
if (!isFileExist(s1)) {
cerr << s1 << " is invalid." << endl;
std::cerr << s1 << " is invalid." << std::endl;
fileExist = false;
}
if (!isFileExist(s2)) {
cerr << s2 << " is invalid." << endl;
std::cerr << s2 << " is invalid." << std::endl;
fileExist = false;
}

View File

@@ -1,22 +1,16 @@
#include "common.hpp"
#include <dtl/dtl.hpp>
#include <iostream>
#include <vector>
using namespace std;
using dtl::Diff;
int main(int argc, char *argv[]) {
if (isFewArgs(argc)) {
cerr << "Too few arguments." << endl;
std::cerr << "Too few arguments." << std::endl;
return -1;
}
typedef char elem;
typedef string sequence;
typedef std::string sequence;
sequence A(argv[1]);
sequence B(argv[2]);
@@ -26,12 +20,12 @@ int main(int argc, char *argv[]) {
d.composeUnifiedHunks();
// editDistance
cout << "editDistance:" << d.getEditDistance() << endl;
std::cout << "editDistance:" << d.getEditDistance() << std::endl;
// Longest Common Subsequence
vector<elem> lcs_v = d.getLcsVec();
std::vector<elem> lcs_v = d.getLcsVec();
sequence lcs_s(lcs_v.begin(), lcs_v.end());
cout << "LCS:" << lcs_s << endl;
std::cout << "LCS:" << lcs_s << std::endl;
// print Unified Format
d.printUnifiedFormat();