1
0
Fork 0
mirror of git://git.psyc.eu/libpsyc synced 2024-08-15 03:19:02 +00:00

match: intermediate version

This commit is contained in:
psyc://psyced.org/~lynX 2011-04-16 13:49:47 +02:00
parent 50b9ccab36
commit a08c6f3f1d
2 changed files with 32 additions and 19 deletions

View file

@ -11,3 +11,8 @@ diet:
lib: lib:
${CC} -static -c -Os parser.c -lc -o libpsyc.o -DDEBUG -I../include ${CC} -static -c -Os parser.c -lc -o libpsyc.o -DDEBUG -I../include
ar rcs libpsyc.a libpsyc.o ar rcs libpsyc.a libpsyc.o
match: match.c
${CC} -o $@ -DTEST $<
it: match

View file

@ -1,37 +1,36 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
int main(int argc, char **argv) { #ifdef TEST
char *sho, *lon, *s, *l, *se, *le; # define PT(args) printf args;
int slen, llen; #else
# define PT(args)
#endif
if (argc != 3) { int psycmatch(char* sho, char* lon) {
printf("Usage: %s <short> <long>\n\nExample: %s _failure_delivery _failure_unsuccessful_delivery_death\n", argv[0], argv[0]); char *s, *l, *se, *le;
return -1; int slen, llen;
}
sho = argv[1];
lon = argv[2];
if (!(slen = strlen(sho)) || *sho != '_' || if (!(slen = strlen(sho)) || *sho != '_' ||
!(llen = strlen(lon)) || *lon != '_') { !(llen = strlen(lon)) || *lon != '_') {
printf("Please use long format keywords (compact ones would be faster, I know..)\n"); PT(("Please use long format keywords (compact ones would be faster, I know..)\n"))
return -2; return -2;
} }
if (slen > llen) { if (slen > llen) {
printf("The long string is shorter than the short one.\n"); PT(("The long string is shorter than the short one.\n"))
return -3; return -3;
} }
if (slen == llen) { if (slen == llen) {
if (!strcmp(sho, lon)) { if (!strcmp(sho, lon)) {
printf("Identical arguments.\n"); PT(("Identical arguments.\n"))
return 0; return 0;
} }
printf("Same length but different.\nNo match, but they could be related or have a common type.\n"); PT(("Same length but different.\nNo match, but they could be related or have a common type.\n"))
return -4; return -4;
} }
printf("*start short '%s' long '%s'\n", sho, lon); PT(("*start short '%s' long '%s'\n", sho, lon))
se = sho+slen; se = sho+slen;
le = lon+llen; le = lon+llen;
@ -39,10 +38,10 @@ int main(int argc, char **argv) {
sho++; lon++; sho++; lon++;
while(s = strchr(sho, '_')) { while(s = strchr(sho, '_')) {
*s = 0; *s = 0;
printf("sho goes '%c' and lon goes '%c'\n", *sho, *lon); PT(("sho goes '%c' and lon goes '%c'\n", *sho, *lon))
while(l = strchr(lon, '_')) { while(l = strchr(lon, '_')) {
*l = 0; *l = 0;
printf("strcmp short '%s' long '%s'\n", sho, lon); PT(("strcmp short '%s' long '%s'\n", sho, lon))
if (!strcmp(sho, lon)) goto foundone; if (!strcmp(sho, lon)) goto foundone;
if (l == le) goto failed; if (l == le) goto failed;
*l = '_'; *l = '_';
@ -50,17 +49,26 @@ int main(int argc, char **argv) {
} }
goto failed; goto failed;
foundone: foundone:
printf("found: short '%s' long '%s'\n", sho, lon); PT(("found: short '%s' long '%s'\n", sho, lon))
if (s == se) goto success; if (s == se) goto success;
*l = *s = '_'; *l = *s = '_';
sho = ++s; sho = ++s;
lon = ++l; lon = ++l;
} }
success: success:
printf("Yes, they match!\n");
return 0; return 0;
failed: failed:
printf("No, they don't!\n"); PT(("No, they don't match.\n"))
return 1; return 1;
} }
#ifdef TEST
int main(int argc, char **argv) {
if (argc != 3) {
printf("Usage: %s <short> <long>\n\nExample: %s _failure_delivery _failure_unsuccessful_delivery_death\n", argv[0], argv[0]);
return -1;
}
if (psycmatch(argv[1], argv[2]) == 0)
printf("Yes, they match!\n");
}
#endif