From 9df202451be8699dd32cbbd715acb9b5b547c6b1 Mon Sep 17 00:00:00 2001 From: Luke Bratch Date: Sat, 30 Oct 2010 01:34:26 +0100 Subject: Add support for multiple names per line in phonebook.conf --- blasms.c | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) (limited to 'blasms.c') diff --git a/blasms.c b/blasms.c index e0c1020..97a37de 100644 --- a/blasms.c +++ b/blasms.c @@ -65,13 +65,17 @@ void setname(char* telnum) { while (fgets(line, 1024, fp) != NULL) { remtrailn(line); - if ((strchrp = strchr(line, ' ')) != NULL) { + if ((strchrp = strchr(line + offset, ' ')) != NULL) { offset = strchrp - line; if ((strlen(telnum) == offset) && !strncmp(telnum, line, offset)) { + if ((strchrp = strchr(line + offset + 1, ' ')) != NULL) { + line[strchrp - line] = '\0'; + } //printf("Name match: %s\n", line); strcat(telnum, " ("); strcat(telnum, line + offset + 1); strcat(telnum, ")"); + return; } } } @@ -82,9 +86,10 @@ void settelnum(char* telnum, char* sms, int *offsetptr) { FILE *fp; char line[1024]; char name[1000]; - char namelower[1000]; + char curname[1000]; char *strchrp; int offset = *offsetptr; + int i, j; fp = fopen("phonebook.conf", "r"); @@ -100,18 +105,30 @@ void settelnum(char* telnum, char* sms, int *offsetptr) { name[offset] = '\0'; } + strtolower(name, name); + while (fgets(line, 1024, fp) != NULL) { remtrailn(line); - if ((strchrp = strchr(line, ' ')) != NULL) { - offset = strchrp - line; - strtolower(namelower, name); - strtolower(line, line); - if ((strlen(name) == strlen(line) - offset - 1) && !strncmp(namelower, line + offset + 1, strlen(name))) { - //printf("Name match: %s\n", line); - strxfrm(telnum, line, offset); - telnum[offset] = '\0'; - *offsetptr = *offsetptr + strlen(name) + 1; - return; + strtolower(line, line); + for (i = 0; i < strlen(line); i++) { + if (line[i] == ' ') { + break; + } + } + offset = i + 1; + for (i = 0, j = 0; i < strlen(line + offset - 1); i++, j++) { + if (line[i + offset] != ' ' && line[i + offset] != '\0') { + curname[j] = line[i + offset]; + } else { + curname[j] = '\0'; + if (!strcmp(name, curname)) { + strncpy(telnum, line, offset - 1); + telnum[offset] = '\0'; + *offsetptr = *offsetptr + strlen(name) + 1; + return; + } else { + j = -1; + } } } } -- cgit v1.2.3