Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include "headers/InputChecker.h"
- /*the input function, takes in input from the keyboard, and goes through a series of functions tests,
- If they all pass, then the input is valid. Once they do pass, the names are put to lowercase, and the score
- is converted into an int for storage later. Then it's checked if this is the first time running to program,
- if so, then the nalloc variable becomes a 1, which lets the array have an initial size.
- Then adds the data into the array from the validated input. and nused is increased by 1.
- When it loops through again, it checks to see if the nalloc is the same increment as the nused. If it is,
- then it takes the size of the array, and stores it into a buffer (temp), and increases the size of it by double.
- Then that value is used to overwrite the size of the current array. Effectively increasing the array everytime
- it's needed.
- At the end, when the EOF key is pressed, a for loop is called, which loops through the amount of nused, and
- free's up the allocated memory.*/
- void input(record_list *recordList){
- char lineChecker[LINESIZE];
- char id[LINESIZE];
- char first[LINESIZE];
- char last[LINESIZE];
- /*the score is at first a string, because I needed to check if there was letter or
- decimals in it. Which I couldn't think of how to do if it was just an int.
- Later it'll be converted over to an int using the atoi() function, only if it passes the
- validation tests. A classmate told me about in order to overcome this issue.*/
- char tempScore[LINESIZE];
- /*a buffer for the record used for copying the array over*/
- record tempRec;
- (*recordList).nused = (*recordList).nalloc = 0;
- /*As long as we can read a line, pressing the OEF key will be the only way to break
- out of this loop.*/
- while(fgets(lineChecker, LINESIZE, stdin)){
- /*pass in the inputs from lineChecker (which is the stdin) to id, first, last, and score*/
- if(sscanf(lineChecker, "%s %s %s %s", id, first, last, tempScore) >= 4){
- /*check inputs for validation*/
- if( check_sizes(id, first, last) == 1 &&
- check_id_format(id) == 1 &&
- score_digits(tempScore) == 1){
- /*converts the string input of score to an int for storage*/
- int score = atoi(tempScore);
- /*if passed, then convert names to loweercase*/
- name_tolower(first);
- name_tolower(last);
- /* This is just left in so I can remember how I debugged an issue.
- fprintf(stderr, "nalloc before adding: %lu\n", (*recordList).nalloc);
- fprintf(stderr, "nused before adding: %lu\n", (*recordList).nused);*/
- /*checks if array size is zero*/
- check_nalloc_equal_zero(recordList);
- /*checks if array needs to be expanded, and then expands it*/
- check_and_expand(recordList);
- /*copies over the data into the array's variables*/
- strcpy(tempRec.id, id);
- strcpy(tempRec.name.last, last);
- strcpy(tempRec.name.first, first);
- tempRec.score = score;
- /*store new data into the array from the tempRec*/
- (*recordList).data[(*recordList).nused] = tempRec;
- /*increase the amount of nused count*/
- (*recordList).nused++;
- /* Once again, left in so I remember later how to debug for future C assignments.
- fprintf(stderr, "nused: %lu\n", (*recordList).nused);
- fprintf(stderr, "nalloc: %lu", (*recordList).nalloc);
- fprintf(stderr, "\n");*/
- }
- }
- }
- }
- /*If the nalloc is zero, then start up the nalloc amount.*/
- void check_nalloc_equal_zero(record_list *recordList){
- /*checks if the storage amount is zero*/
- if((*recordList).nalloc == 0){
- /*creates a temp record*/
- record *temp;
- /*increases the amount by 1 (so 0 -> 1)*/
- (*recordList).nalloc++;
- /*reallocs the temp array to become 1*/
- temp = realloc((*recordList).data, ((*recordList).nalloc + sizeof(record)));
- /*the dynamic array now becomes the size of the temp array*/
- (*recordList).data = temp;
- #ifdef DEBUG
- fprintf(stderr, "#");
- #endif
- }
- }
- /*checks if the array needs to be expanded or not.*/
- void check_and_expand(record_list *recordList){
- /*if the amount used is the same as the amount allocated*/
- if((*recordList).nused == (*recordList).nalloc){
- /*created a temp record*/
- record *temp;
- /*increase the size of the nalloc by 2 times the current amount*/
- (*recordList).nalloc *= 2;
- /*assign the space of the current nalloc, to a temp*/
- temp = realloc((*recordList).data, ((*recordList).nalloc * sizeof(record)));
- /*remake the size of the data to the size of the temp*/
- (*recordList).data = temp;
- #ifdef DEBUG
- fprintf(stderr, "#");
- #endif
- }
- }
- /*If the the student ID isn't the required size,
- or the first name is bigger than the allocated namesize
- or the last name is bigger than the allocated namesize
- then return -1, and fail*/
- int check_sizes(char id[], char first[], char last[]){
- if(strlen(id) != (IDSIZE-1) || strlen(first) >= NAMESIZE
- || strlen(last) >= NAMESIZE){
- return -1;
- }
- /*return 0 if correct*/
- return 1;
- }
- /*check if the first spot in the array for id is not an 'a'
- if it's not then loop through, and check if the remaining characters
- are not digits, if they aren't, then return -1
- if the first character isn't an 'a', return -1*/
- int check_id_format(char id[]){
- size_t i;
- /*if the first character isn't an a*/
- if(id[0] != 'a'){
- /*then loop through the rest of the array, and check
- if they aren't digits*/
- for(i = 1; id[i] != '\0'; i++){
- if(!isdigit(id[i])){
- /*if they aren't digits, return a false*/
- return -1;
- }
- }
- /*if the first one isn't an 'a', return a false*/
- return -1;
- }
- /*return 0 if correct*/
- return 1;
- }
- /*if the score isn't above 0, or below 100,
- then return -1*/
- int score_digits(char scoreCheck[]){
- int score = atoi(scoreCheck);
- size_t i;
- for(i = 0; scoreCheck[i] != '\0'; i++){
- if(i == '.' || i == '#' || !isdigit(scoreCheck[i])){
- return -1;
- }
- }
- if(score < 0 || score > 100){
- return -1;
- }
- /*return 1 if correct*/
- return 1;
- }
- /*loops through the first and last name, and converts the characters in the array to lowercase*/
- void name_tolower(char name[]){
- size_t i;
- /*loop through the array of first name, and set all the characters
- to lowercase*/
- for(i = 0; name[i] != '\0'; i++){
- name[i] = tolower(name[i]);
- }
- }
- /*Also taken from my lab6*/
- /*loops through the data, and prints out the array data*/
- void print_record(const record_list *recordList){
- size_t i;
- for(i = 0; i < (*recordList).nused; i++){
- printf("%s : %s, %s : %d\n",
- (*recordList).data[i].id,
- (*recordList).data[i].name.last,
- (*recordList).data[i].name.first,
- (*recordList).data[i].score);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement