Example #1
0
void readBookFromFile(FILE *input, Book *book) {
    int id;
    int maxID = 0;
    bool unsorted = false;
    char *name, *number;
    while (!feof(input)) {
        fscanf(input, "%d", &id);
        name = read(input);
        number = read(input);
        printf ("debug %d %s %s\n", id, name, number);
        if (name[0] == 0 || number[0] == 0) {
            free(name);
            free(number);
            continue;
        }
        if (!correctName(name) || !correctNumber(number)) {
            printf("ERROR: cannot read %d %s %s\n", id, name, number);
            free(name);
            free(number);
            return;
        }
        if (id < maxID)
            unsorted = true;
        appendBookID(book, name, number, id);
        maxID = max(id, maxID);
        free(name);
        free(number);
    }
    if (unsorted)
        qsort(book->contacts, book->size, sizeof(Contact), compareContacts);
    book->lastId = id;
}
Example #2
0
bool simpleExpression(char s[], int &position)
{
	if (s[position] == '(') 
	{
		int current = position + 1;
		if (sumExpression(s, current))
		{
			position = current;
			if (s[position] == ')')
			{
				position++;
				return true;
			}
			else
			{
				return false;
			}
		}
		else
			return false;
	}
	else if (isNumberSymbol(s[position]) || isMinusSymbol(s[position]))
	{
		 return correctNumber(s,position);
	}
	else 
	{
		return false;
	}
}
Example #3
0
int main(int argc, char const *argv[]) {
    if (argc == 1) {
        printf("ERROR: the program takes one argument\n");
        return 0;
    }
    argv[1];
    FILE *input = fopen(argv[1], "r");
    Book *book = createBook();
    readBookFromFile(input, book);
    fclose(input);

    sh_fileName = argv[1];
    sh_book = book;
    signal(SIGINT, signal_handler); 

    char command[10];
    char *name = NULL;   //(char *) malloc(sizeof(char)*80);
    char *number = NULL; // (char *) malloc(sizeof(char)*80);
    char *str = NULL;    //(char *) malloc(80);

    bool corName;
    bool corNumber;
    
    while (true) {
        corName = true;
        corNumber = true;
        scanf("%s", command);
        if(!strcmp(command, "debprint")) {
            fullPrint(book);
        } else if(!strcmp(command, "print")) {
            printBook(stdout, book);
        } else if(!strcmp(command, "find")) {
            str = read(stdin);
            printFound(stdout, book, str);
            free(str);
        } else if(!strcmp(command, "create")) {
            name = read(stdin); number = read(stdin);
            corName = correctName(name);
            corNumber = correctNumber(number);
            if (correctName && correctNumber) {
                appendBook(book, name, number);
            }
            free(name); free(number);
        } else if(!strcmp(command, "delete")) {
            int id;
            scanf("%d", &id);
            removeContact(book, id);
        } else if(!strcmp(command, "change")) {
            int id;
            scanf("%d%s", &id, command);
            str = read(stdin);
            if(!strcmp(command, "number")) {
                corName = correctName(str);
                if (corName)
                    changeNumber(book, id, str);
            } else if (!strcmp(command, "name")) {
                corNumber = correctNumber(str);
                if (corNumber)
                    changeName(book, id, str);
            } else {
                printf("ERROR: Expected \"name\" or \"number\" after id in the command change\n");
            }
            free(str);
        } else if(!strcmp(command, "exit")) {
            FILE* output = fopen(argv[1], "w");
            printBook(output, book);
            fclose(output);
            deleteBook(book);
            break;
        } else {
            printf("ERROR: Unknown command %s\n", command);
        }
        if (!corName) {
            printf("ERROR: Invalid name\n");
        }
        if (!corNumber) {
            printf("ERROR: Invalid number\n");
        }
    }

    return 0;
}