NORMAL IS (kinda) WORKING.

master
Chris Nutter 2019-05-19 00:21:21 -07:00
parent 2d9e172985
commit da5270a6fc
7 changed files with 165 additions and 82 deletions

BIN
diff-rcm

Binary file not shown.

View File

@ -1,20 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleIdentifier</key>
<string>com.apple.xcode.dsym.diff-rcm</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>dSYM</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>

99
diff.c
View File

@ -12,11 +12,18 @@ int main(int argc, const char* argv[]) {
init(--argc, ++argv); init(--argc, ++argv);
loadfiles(files[0], files[1]); loadfiles(files[0], files[1]);
if (**argv != '-' || diffnormal == 1) { standard(); } if (ignorecase) { ignore_case(); }
if (report_identical) { loud (files[0], files[1]); }
if (showsidebyside) { sideside(files[0], files[1]); }
identical(files[0], files[1]); // -s and -y work for identical file names so I manipulated load order.
if (showsidebyside) { sideside(); }
if (showbrief) { quiet(files[0], files[1]); } if (showbrief) { quiet(files[0], files[1]); }
if (report_identical) { loud(files[0], files[1]); }
if (**argv != '-' || diffnormal == 1) { lineline(); }
if (showcontext) { context(); }
if (showunified) { unified(); }
return 0; return 0;
@ -66,8 +73,11 @@ void init(int argc, const char* argv[]) {
} }
void setoption(const char* arg, const char* s, const char* t, int* value) { void setoption(const char* arg, const char* s, const char* t, int* value) {
if ((strcmp(arg, s) == 0) || ((t != NULL && strcmp(arg, t) == 0))) { *value = 1; } if ((strcmp(arg, s) == 0) || ((t != NULL && strcmp(arg, t) == 0))) { *value = 1; }
} }
void diff_output_conflict_error(void) { void diff_output_conflict_error(void) {
fprintf(stderr, "diff-rcm: Conflicting output style options.\n"); fprintf(stderr, "diff-rcm: Conflicting output style options.\n");
@ -97,6 +107,8 @@ void loadfiles(const char* filename1, const char* filename2) {
} }
// ============================================================================= //
void version(void) { void version(void) {
printf("\n\n ██ ██ ████ ████ \n"); printf("\n\n ██ ██ ████ ████ \n");
printf(" ░██░░ ░██░ ░██░ \n"); printf(" ░██░░ ░██░ ░██░ \n");
@ -116,7 +128,7 @@ void help(void) {
printf("\nUsage: diff-rcm [OPTION]... FILES\n"); printf("\nUsage: diff-rcm [OPTION]... FILES\n");
printf("Compare FILES line by line.\n\n"); printf("Compare FILES line by line.\n\n");
printf("Mandatory arguments to long options are mandatory for short options too.\n\n"); printf("Mandatory arguments to long options are mandatory for short options too.\n\n");
printf("\t --standard\t\t output a standard diff (the default)\n"); printf("\t --lineline\t\t output a lineline diff (the default)\n");
printf("\t-q, --brief\t\t report only when files differ\n"); printf("\t-q, --brief\t\t report only when files differ\n");
printf("\t-s, --report-identical-files report when two files are the same\n"); printf("\t-s, --report-identical-files report when two files are the same\n");
printf("\t-c, -C NUM, --context[=NUM] output NUM (default 3) lines of copied context\n"); printf("\t-c, -C NUM, --context[=NUM] output NUM (default 3) lines of copied context\n");
@ -126,39 +138,70 @@ void help(void) {
printf("\t --help\t\t display this help and exit\n"); printf("\t --help\t\t display this help and exit\n");
printf("\t-v, --version\t\t output version information and exit\n\n"); printf("\t-v, --version\t\t output version information and exit\n\n");
printf("FILES are 'FILE1 FILE2'\n");
printf("If --from-file or --to-file is given, there are no restrictions on FILE(s).\n");
printf("If a FILE is '-', read standard input.\n");
printf("Exit status is 0 if inputs are the same, 1 if different, 2 if trouble.\n\n");
printf("Report bugs to: cdnutter@gmail.com\n"); printf("Report bugs to: cdnutter@gmail.com\n");
printf("diff-rcm homepage: <https://www.github.com/cdnutter/diff/>\n\n"); printf("diff-rcm homepage: <https://www.github.com/cdnutter/diff/>\n\n");
} }
void standard(void) { // ============================================================================= //
void ignore_case(void) {
} }
void sideside(void) { int identical(const char* filename1, const char* filename2) { if (*filename1 == *filename2) { exit(0); } else return 0; }
int foundmatch = 0; // ============================================================================= //
pa* qlast = q; void lineline(void) {
while(p != NULL) {
qlast = q; int foundmatch = 0; pa* qlast = q;
foundmatch = 0;
while (p != NULL) {
qlast = q; foundmatch = 0;
while (q != NULL && (foundmatch = pa_equal(p, q)) == 0) { q = pa_next(q); } while (q != NULL && (foundmatch = pa_equal(p, q)) == 0) { q = pa_next(q); }
q = qlast; q = qlast;
if (foundmatch) { if (foundmatch) {
while ((foundmatch = pa_equal(p, q)) == 0) { while ((foundmatch = pa_equal(p, q)) == 0) {
print_check(q, NULL, print_normal_right);
q = pa_next(q);
qlast = q;
}
print_check(p, q, line_check);
p = pa_next(p);
q = pa_next(q);
}
else { print_check(p, NULL, print_normal_left); p = pa_next(p); }
}
while (q != NULL) { print_check(q, NULL, print_normal_right); q = pa_next(q); }
}
void sideside(const char* filename1, const char* filename2) {
int foundmatch = 0; pa* qlast = q;
if (*filename1 == *filename2) {
while (p != NULL && q != NULL) { print_check(p, q, print_both);
p = pa_next(p); }
return;
}
while (p != NULL) {
qlast = q; foundmatch = 0;
while (q != NULL && (foundmatch = pa_equal(p, q)) == 0) { q = pa_next(q); }
q = qlast;
if (foundmatch) {
while ((foundmatch = pa_equal(p, q)) == 0) {
print_check(q, NULL, print_right); print_check(q, NULL, print_right);
q = pa_next(q); q = pa_next(q);
qlast = q; qlast = q;
} }
if (showleftcolumn) { print_check(p, q, print_left_paren); } if (showleftcolumn) { print_check(p, q, print_left_paren); }
@ -167,27 +210,27 @@ void sideside(void) {
p = pa_next(p); p = pa_next(p);
q = pa_next(q); q = pa_next(q);
} }
else { print_check(p, NULL, print_left); p = pa_next(p); } else { print_check(p, NULL, print_left); p = pa_next(p); }
} }
while(q != NULL) { while (q != NULL) { print_check(q, NULL, print_right); q = pa_next(q); }
print_check(q, NULL, print_right);
q = pa_next(q);
}
} }
void quiet(const char* filename1, const char* filename2) { if (identical(filename1, filename2) == 0) { printf("Files %s and %s differ.\n", filename1, filename2); } } void context(void) {}
void loud(const char* filename1, const char* filename2) { void unified(void) {}
void quiet(const char* filename1, const char* filename2) { if (identical(filename1, filename2) == 0) { printf("Files %s and %s differ.\n", filename1, filename2); } exit(0); }
void loud (const char* filename1, const char* filename2) {
if (*filename1 == *filename2 || (pa_equal(p, q) != 0)) { printf("Files %s and %s are identical.\n", filename1, filename2); } if (*filename1 == *filename2 || (pa_equal(p, q) != 0)) { printf("Files %s and %s are identical.\n", filename1, filename2); }
else { standard(); } else if (showsidebyside) { sideside(files[0], files[1]); exit(0); }
else if (showcontext) { context(); exit(0); }
else if (showunified) { unified(); exit(0); }
else { lineline(); exit(0); }
} }
int identical(const char* filename1, const char* filename2) { if (*filename1 == *filename2) { exit(0); } else return 0; } // ============================================================================= //

11
diff.h
View File

@ -18,7 +18,7 @@
TODO: check line by line in a pagraph, using '|' for differences"); TODO: check line by line in a pagraph, using '|' for differences");
TODO: this starter code does not yet handle printing all of fin1's pagraphs."); TODO: this starter code does not yet handle printing all of fin1's pagraphs.");
TODO: handle the rest of diff's options"); TODO: handle the rest of diff's options");
TODO: fix standard printing with no pameters"); TODO: fix lineline printing with no pameters");
TODO: implement multiple types of pameters\n"); TODO: implement multiple types of pameters\n");
*/ */
@ -49,8 +49,13 @@ void loadfiles(const char* filename1, const char* filename2);
void version(void); void version(void);
void help(void); void help(void);
void standard(void); void ignore_case(void);
void sideside(void);
void lineline(void);
void sideside(const char* filename1, const char* filename2);
void context (void);
void unified (void);
void quiet(const char* filename1, const char* filename2); void quiet(const char* filename1, const char* filename2);
void loud(const char* filename1, const char* filename2); void loud(const char* filename1, const char* filename2);

72
pa.c
View File

@ -4,7 +4,17 @@
#include "pa.h" #include "pa.h"
// ======================================================================= // // ============================================================================================================= //
FILE* openfile(const char* filename, const char* openflags) {
FILE* f;
if ((f = fopen(filename, openflags)) == NULL) { printf("can't open '%s'\n", filename); exit(1); }
return f;
}
// ============================================================================================================= //
pa* pa_make(char* base[], int filesize, int start, int stop) { pa* pa_make(char* base[], int filesize, int start, int stop) {
@ -70,7 +80,7 @@ int pa_equal(pa* p, pa* q) {
} }
// ======================================================================================================= // // ============================================================================================================= //
void sideside_type(const char* left, const char* right, int nocommon, int leftparen, char symbol) { void sideside_type(const char* left, const char* right, int nocommon, int leftparen, char symbol) {
@ -90,21 +100,13 @@ void sideside_type(const char* left, const char* right, int nocommon, int leftpa
} }
// ======================================================================================================= // // ============================================== SIDE-BY-SIDE ================================================ //
void print_left_paren(const char* left, const char* right) { sideside_type(left, right, 0, 1, '('); } void print_left_paren(const char* left, const char* right) { sideside_type(left, right, 0, 1, '('); }
void print_no_common(const char* left, const char* right) { sideside_type(left, right, 1, 0, ' '); } void print_no_common(const char* left, const char* right) { sideside_type(left, right, 1, 0, ' '); }
void print_side_normal(const char* left, const char* right) { sideside_type(left, right, 0, 0, ' '); } void print_side_normal(const char* left, const char* right) { sideside_type(left, right, 0, 0, ' '); }
FILE* openfile(const char* filename, const char* openflags) { void print_left(const char* left, const char* _) {
FILE* f;
if ((f = fopen(filename, openflags)) == NULL) { printf("can't open '%s'\n", filename); exit(1); }
return f;
}
void print_left(const char* left, const char* n) {
char buf[BUFLEN]; char buf[BUFLEN];
strcpy(buf, left); strcpy(buf, left);
@ -162,3 +164,49 @@ void print_second(pa* p, pa* q, void (*fp)(const char*, const char*)) {
} }
} }
// ================================================ NORMAL ================================================== //
void print_normal_left(const char* left, const char* _) {
char buf[BUFLEN];
strcpy(buf, left);
int j = 0, len = (int)strlen(buf) - 1;
for (j = 0; j <= 52 - len ; ++j) { buf[len + j] = ' '; }
printf("%s %s\n", "<", buf);
}
void print_normal_right(const char* right, const char* _) {
char buf[BUFLEN];
strcpy(buf, right);
int j = 0, len = (int)strlen(buf) - 1;
for (j = 0; j <= 52 - len ; ++j) {
buf[len + j] = ' ';
}
printf("%s %s\n", ">", buf);
}
void line_check(const char* left, const char* right) {
if (strcmp(left, right) == 0);
else { printf("< %s---\n> %s", left, right); }
}
int line_number(const char* left, const char* right) {
return 0;
}
// ================================================ CONTEXT ================================================== //

17
pa.h
View File

@ -39,16 +39,23 @@ void sideside_type(const char* left, const char* right, int nocommon, int leftpa
void print_check(pa* p, pa* q, void (*fp)(const char*, const char*)); void print_check(pa* p, pa* q, void (*fp)(const char*, const char*));
void print_first(pa* p, void (*fp)(const char*)); void print_first (pa* p, void (*fp)(const char*));
void print_second(pa* p, pa* q, void (*fp)(const char*, const char*)); void print_second(pa* p, pa* q, void (*fp)(const char*, const char*));
void print_left_paren(const char* left, const char* right); void print_left_paren (const char* left, const char* right);
void print_no_common(const char* left, const char* right); void print_no_common (const char* left, const char* right);
void print_side_normal(const char* left, const char* right); void print_side_normal(const char* left, const char* right);
void print_left(const char* left, const char*); void print_left (const char* left, const char*);
void print_right(const char* right, const char*); void print_right(const char* right, const char*);
void print_both(const char* left_right, const char*); void print_both (const char* left_right, const char*);
void print_normal_left (const char* left, const char*);
void print_normal_right(const char* right, const char*);
void print_normal_both (const char* left, const char* right, const char*);
void line_check(const char* right, const char*);
int line_number(const char* left, const char* right);
#endif #endif