fixed identical

master
Chris Nutter 2019-05-15 17:24:09 -07:00
parent 9903c8cf44
commit e5badeec87
15 changed files with 153 additions and 48 deletions

BIN
deprecated/newfiles/test_diff Executable file

Binary file not shown.

BIN
diff-rcm

Binary file not shown.

40
diff.c
View File

@ -79,7 +79,6 @@ void diff_output_conflict_error(void) {
void loadfiles(const char* filename1, const char* filename2) {
if (filename2 == NULL) { printf("Usage: ./diff [options] file1 file2\n"); exit(1); }
if (*filename1 == *filename2) { exit(0); }
memset(buf, 0, sizeof(buf));
memset(strings1, 0, sizeof(strings1));
@ -135,28 +134,53 @@ void help() {
int normal(const char* filename1, const char* filename2) {
identical(filename1, filename2);
printf("\nTHIS IS NOT NORMAL FOR NOW. THIS IS PLACEHOLDER. MMKAY.\n");
printf("THIS IS NOT NORMAL FOR NOW. THIS IS PLACEHOLDER. MMKAY.\n");
printf("THIS IS NOT NORMAL FOR NOW. THIS IS PLACEHOLDER. MMKAY.\n");
printf("THIS IS NOT NORMAL FOR NOW. THIS IS PLACEHOLDER. MMKAY.\n\n\n");
printf("THIS IS NOT NORMAL FOR NOW. THIS IS PLACEHOLDER. MMKAY.\n\n");
pa_print(p, printnormal);
//pa_print(p, printnormal);
return 0;
}
int sideside(const char* filename1, const char* filename2) {
if (pa_equal(p, q)) { pa_print(p, printboth); return 0; }
pa_print(q, printright);
while ((q = pa_next(q)) != NULL && !equal) { equal = pa_equal(p, q); pa_print(q, equal ? printboth : printright); }
pa_print((p = pa_next(p)), printleft);
pa* qlast = q;
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) {
pa_print(q, printright);
q = pa_next(q);
qlast = q;
}
pa_print(q, printboth);
p = pa_next(p);
q = pa_next(q);
} else {
pa_print(p, printleft);
p = pa_next(p);
}
}
while (q != NULL) {
pa_print(q, printright);
q = pa_next(q);
}
return 0;
}
void quiet(const char* filename1, const char* filename2) { if (pa_equal(p, q) == 0) { printf("The files are not the same.\n"); } else { return; } }
void loud(const char* filename1, const char* filename2) {
if (pa_equal(p, q) != 0) { printf("The files are identical.\n"); }
if (*filename1 == *filename2 || (pa_equal(p, q) != 0)) { printf("The files are identical.\n"); }
else { normal(files[0], files[1]); }
}
void identical(const char* filename1, const char* filename2) { if (*filename1 == *filename2) { exit(0); } else return; }

4
diff.h
View File

@ -32,7 +32,7 @@ char *strings1[MAXSTRINGS], *strings2[MAXSTRINGS];
int showversion = 0, showbrief = 0, ignorecase = 0, report_identical = 0, showsidebyside = 0;
int showleftcolumn = 0, showunified = 0, showcontext = 0, suppresscommon = 0, diffnormal = 0;
int showhelp = 0;
int showhelp = 0, foundmatch = 0;
int equal = 0, cnt = 0, count = 0;
@ -53,3 +53,5 @@ int normal(const char* filename1, const char* filename2);
int sideside(const char* filename1, const char* filename2);
void quiet(const char* filename1, const char* filename2);
void loud(const char* filename1, const char* filename2);
void identical(const char* filename1, const char* filename2);

25
left.txt Normal file
View File

@ -0,0 +1,25 @@
This part of the
document has stayed the
sme from version to
version. It shouldn't
be shown if it doesn't
change. Otherwise, that
would not be helping to
compress the size of the
changes.
This paragraph contains
text that is outdated.
It will be deleted in the
near future.
It is important to spell
check this dokument. On
the other hand, a
misspelled word isn't
the end of the world.
Nothing in the rest of
this paragraph needs to
be changed. Things can
be added after it.

96
pa.c
View File

@ -12,29 +12,83 @@ pa* pa_make(char* base[], int filesize, int start, int stop) {
p->filesize = filesize;
p->start = start;
p->stop = stop;
p->firstline = (p == NULL || start < 0) ? NULL : p->base[start];
p->secondline = (p == NULL || start < 0 || filesize < 2) ? NULL : p->base[start + 1];
return p;
}
pa* pa_first(char* base[], int size) {
pa* p = pa_make(base, size, 0, -1);
return pa_next(p);
}
void pa_destroy(pa* p) { free(p); }
pa* pa_next(pa* p) {
if (p->stop == p->filesize) { return NULL; }
if (p == NULL || p->stop == p->filesize) { return NULL; }
int i;
pa* pnew = pa_make(p->base, p->filesize, p->stop + 1, p->stop + 1);
for (i = pnew->start; i < p->filesize && strcmp(p->base[i], "\n") != 0; ++i) { }
pnew->stop = i;
if (pnew->start >= p->filesize) {
free(pnew);
pnew = NULL;
}
return pnew;
}
size_t pa_filesize(pa* p) { return p == NULL ? 0 : p->filesize; }
size_t pa_size(pa* p) { return p == NULL || p->stop < p->start ? 0 : p->stop - p->start + 1; }
char** pa_base(pa* p) { return p->base; }
char* pa_info(pa* p) {
static char buf[BUFLEN]; // static for a reason
snprintf(buf, sizeof(buf), "base: %p, filesize: %d, start: %d, stop: %d\n",
p->base, p->filesize, p->start, p->stop);
return buf; // buf MUST be static
}
int pa_equal(pa* p, pa* q) {
if (p == NULL || q == NULL) { return 0; }
if (pa_size(p) != pa_size(q)) { return 0; }
if (p->start >= p->filesize || q->start >= q->filesize) { return 0; }
int i = p->start, j = q->start, equal = 0;
while ((equal = strcmp(p->base[i], q->base[i])) == 0) { ++i; ++j; }
return 1;
}
void pa_print(pa* p, void (*fp)(const char*)) {
if (p == NULL) { return; }
for (int i = p->start; i <= p->stop && i != p->filesize; ++i) { fp(p->base[i]); }
}
void pa_destroy(pa* p) { free(p); }
void pa_printfile(char* base[], int count, void (*fp)(const char*)) {
pa* p = pa_first(base, count);
while (p != NULL) {
pa_print(p, fp);
p = pa_next(p);
}
printline();
}
char* yesorno(int condition) { return condition == 0 ? "no" : "YES"; }
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;
}
void printline(void) {
for (int i = 0; i < 10; ++i) { printf("=========="); }
printf("\n");
}
void printleft(const char* left) {
char buf[BUFLEN];
@ -45,10 +99,12 @@ void printleft(const char* left) {
buf[len + j++] = '\0';
printf("%s\n", buf);
}
void printright(const char* right) {
if (right == NULL) { return; }
printf("%50s %s", ">", right);
}
void printboth(const char* left_right) {
char buf[BUFLEN];
size_t len = strlen(left_right);
@ -56,39 +112,3 @@ void printboth(const char* left_right) {
buf[len - 1] = '\0';
printf("%-50s %s", buf, left_right);
}
void printnormal(const char* first) {
char buf[BUFLEN];
size_t len1 = strlen(first);
if (len1 > 0) { strncpy(buf, first, len1); }
buf[len1 - 1] = '\0';
}
size_t pa_filesize(pa* p) { return p == NULL ? 0 : p->filesize; }
size_t pa_size(pa* p) { return p == NULL || p->stop < p->start ? 0 : p->stop - p->start + 1; }
char** pa_base(pa* p) { return p->base; }
char* yesorno(int condition) { return condition == 0 ? "no" : "YES"; }
char* pa_info(pa* p) {
static char buf[BUFLEN]; // static for a reason
snprintf(buf, sizeof(buf), "base: %p, filesize: %d, start: %d, stop: %d\n",
p->base, p->filesize, p->start, p->stop);
return buf; // buf MUST be static
}
int pa_equal(pa* p, pa* q) {
if (p == NULL || q == NULL) { return 0; }
if (pa_size(p) != pa_size(q)) { return 0; }
if (p->start >= p->filesize || q->start >= q->filesize) { return 0; }
int i = p->start, j = q->start, equal = 0, psize = p->filesize, qsize = q->filesize;
while (i < psize && j < qsize && (equal = strcmp(p->base[i], q->base[i])) == 0) { ++i; ++j; }
return 1;
}
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;
}

5
pa.h
View File

@ -13,6 +13,8 @@ struct pa {
int filesize;
int start;
int stop;
char* firstline; // DEBUG only
char* secondline;
};
pa* p;
@ -30,8 +32,9 @@ void pa_print(pa* p, void (*fp)(const char*));
void printboth(const char* left_right);
void printleft(const char* left);
void printright(const char* right);
void printnormal(const char* first);
void pa_printfile(char* base[], int count, void (*fp)(const char*));
FILE* openfile(const char* filename, const char* openflags);
char* yesorno(int condition);
void printline(void);
#endif

31
right.txt Normal file
View File

@ -0,0 +1,31 @@
This is an important
notice! It should
therefore be located at
the beginning of this
document!
This part of the
document has stayed the
sme from version to
version. It shouldn't
be shown if it doesn't
change. Otherwise, that
would not be helping to
compress the size of the
changes.
It is important to spell
check this document. On
the other hand, a
misspelled word isn't
the end of the world.
Nothing in the rest of
this paragraph needs to
be changed. Things can
be added after it.
This pragraph contains
important new additions
to this document.