#include "common.hpp" using dtl::Diff; using dtl::elemInfo; using dtl::uniHunk; static void showStats(std::string fp1, std::string fp2); static void unifiedDiff(std::string fp1, std::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]; struct tm *timeinfo[2]; struct stat st[2]; if (stat(fp1.c_str(), &st[0]) == -1) { std::cerr << "argv1 is invalid." << std::endl; exit(-1); } if (stat(fp2.c_str(), &st[1]) == -1) { std::cerr << "argv2 is invalid" << std::endl; exit(-1); } char buf[2][MAX_LENGTH + 1]; rawtime[0] = st[0].st_mtime; timeinfo[0] = localtime(&rawtime[0]); strftime(buf[0], MAX_LENGTH, time_format, timeinfo[0]); 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]); std::cout << "+++ " << fp2 << '\t' << buf[1] << std::endl; } static void unifiedDiff(std::string fp1, std::string fp2) { typedef std::string elem; typedef std::vector sequence; typedef std::pair sesElem; std::ifstream Aifs(fp1.c_str()); std::ifstream Bifs(fp2.c_str()); elem buf; sequence ALines, BLines; while (getline(Aifs, buf)) { ALines.push_back(buf); } while (getline(Bifs, buf)) { BLines.push_back(buf); } Diff diff(ALines, BLines); diff.onHuge(); // diff.onUnserious(); diff.compose(); // type unihunk definition test uniHunk hunk; if (diff.getEditDistance() > 0) { showStats(fp1, fp2); // show file info } diff.composeUnifiedHunks(); diff.printUnifiedFormat(); } int main(int argc, char *argv[]) { if (isFewArgs(argc)) { std::cerr << "Too few arguments." << std::endl; return -1; } std::string s1(argv[1]); std::string s2(argv[2]); bool fileExist = true; if (!isFileExist(s1)) { std::cerr << s1 << " is invalid." << std::endl; fileExist = false; } if (!isFileExist(s2)) { std::cerr << s2 << " is invalid." << std::endl; fileExist = false; } if (!fileExist) { return -1; } unifiedDiff(s1, s2); return 0; }