Time Calculator/src/timef.h (Time Calculator)
//This file contains function declaration and implementations for all file related functions like
//opening file and parsing from file and fsize() that returns file size
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define ERR_FILE_NOT_FOUND "The specified file is not found, please recheck the file name and path\n"
#define ERR_CODE_FILE_NOT_FOUND 40
#define MAX_USERS_BYTE 251
typedef struct {
char id[26];
Time time;
}User;
int totalLines(FILE* file);
int len(char* str);
long fsize(FILE* file);
char* removeSpaces(char* str,int length);
int fileIt(int argc,char** argv);
char* loadLine(FILE* file,int n);
int countChars(char* str,char c);
int totalUsers(FILE* file);
User** parseUsers(FILE* file,int n);
void parseUserTime(char* line,User** users,int totalUsers);
void calcUserTime(FILE* file,User** users);
int removeColon(char* str,int len);
int len(char* str){
int counter = 0;
short ended = 0;
while(ended == 0){
if (str[counter] == '\0'){
ended = 1;
}
else{
counter++;
}
}
return counter;
}
long fsize(FILE* file){
int currLoc = ftell(file);
fseek(file,0,SEEK_END);
long fileSize = ftell(file);
fseek(file,0,currLoc);
return fileSize;
}
char* removeSpaces(char* str,int length){
char* tempStr = (char*) malloc(length * sizeof(char));
int counter = 0;
for(int i=0;i<length;i++){
if(str[i] != ' '){
tempStr[counter] = str[i];
counter++;
}
}
tempStr[counter] = '\0';
return tempStr;
}
int fileIt(int argc, char** argv){
FILE* file = fopen(argv[2],"r");
if (file == NULL){
printf(ERR_FILE_NOT_FOUND);
return ERR_CODE_FILE_NOT_FOUND;
}
int tUsers = totalUsers(file);
rewind(file);
printf("Total users : %d\n",tUsers);
User** users = parseUsers(file,tUsers);
calcUserTime(file,users);
for(int i=0;i < tUsers;i++){
printf("User %d : %s\n",i+1,users[i]->id);
printf("\tTotal time : %dh %dmins\n",users[i]->time.hour,users[i]->time.minute);
}
return 0;
}
char* loadLine(FILE* file, int n){
char* string = malloc(sizeof(char) * n);
for(int i =0; i < n;i++){
string[i] = fgetc(file);
if(string[i] == '\n'){
string[i+1] = '\0';
return string;
}
if(feof(file)){
string[i+1] = '\0';
return string;
}
}
return NULL;
}
int totalLines(FILE* file){
int lines = 0;
while(1){
if(fgetc(file) == '\n'){
lines++;
if(feof(file)){
return lines;
}
} else {
if(feof(file)){
lines++;
return lines;
}
}
}
}
int countChar(char* str,char c){
int cCount = 0;
int i = 0;
while(str[i] != '\0'){
if(str[i] == c){
cCount++;
i++;
}
i++;
}
return cCount;
}
int totalUsers(FILE* file){
char* line = loadLine(file,MAX_USERS_BYTE);
int tUsers = countChar(line,';');
rewind(file);
return tUsers;
}
User** parseUsers(FILE* file,int n){
char* line = loadLine(file,MAX_USERS_BYTE);
User** users = malloc(sizeof(User*) * n);
int llen = strlen(line);
char* tempId = malloc(sizeof(char) * 26);
int idCount = 0;
int userCount = 0;
for(int i =0; i < llen;i++){
if(line[i] == ';'){
tempId[idCount] = '\0';
if(!strcmp(tempId,"")){
free(tempId);
return NULL;
}
users[userCount] = malloc(sizeof(User));
strcpy(users[userCount]->id,tempId);
if(userCount >= n-1){
free(tempId);
return users;
}
userCount++;
idCount = 0;
if(line[i] == '\n'){
free(tempId);
return users;
}
}
else{
tempId[idCount] = line[i];
idCount++;
}
}
free(tempId);
return NULL;
}
int removeColon(char* str,int len){
for(int i =0;i < len;i++){
if(str[i] == ';'){
str[i] = '\0';
return i;
}
}
return -1;
}
void parseUserTime(char* line,User** users,int totalUsers){
Time start,end;
char parsedUser[26];
Time* addedTime;
sscanf(line,"%d:%d-%d:%d|%s;",&start.hour,&start.minute,&end.hour,&end.minute,parsedUser);
Time* calculatedTime = calcTime(&start,&end);
parsedUser[removeColon(parsedUser,26)] = '\0';
for(int i = 0;i < totalUsers;i++){
if(!strcmp(users[i]->id,parsedUser)){
Time* addedTime = addTime(calculatedTime,&(users[i]->time));
users[i]->time.hour = addedTime->hour;
users[i]->time.minute = addedTime->minute;
return;
}
}
return;
}
void calcUserTime(FILE* file,User** users){
int lines = totalLines(file);
rewind(file);
/* using this function to skip the first line*/
int tUsers = totalUsers(file);
parseUsers(file,tUsers);
for(int i =0;i < lines;i++){
char* line = loadLine(file,100);
line = removeSpaces(line,strlen(line));
parseUserTime(line,users,tUsers);
}
return;
}
//opening file and parsing from file and fsize() that returns file size
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define ERR_FILE_NOT_FOUND "The specified file is not found, please recheck the file name and path\n"
#define ERR_CODE_FILE_NOT_FOUND 40
#define MAX_USERS_BYTE 251
typedef struct {
char id[26];
Time time;
}User;
int totalLines(FILE* file);
int len(char* str);
long fsize(FILE* file);
char* removeSpaces(char* str,int length);
int fileIt(int argc,char** argv);
char* loadLine(FILE* file,int n);
int countChars(char* str,char c);
int totalUsers(FILE* file);
User** parseUsers(FILE* file,int n);
void parseUserTime(char* line,User** users,int totalUsers);
void calcUserTime(FILE* file,User** users);
int removeColon(char* str,int len);
int len(char* str){
int counter = 0;
short ended = 0;
while(ended == 0){
if (str[counter] == '\0'){
ended = 1;
}
else{
counter++;
}
}
return counter;
}
long fsize(FILE* file){
int currLoc = ftell(file);
fseek(file,0,SEEK_END);
long fileSize = ftell(file);
fseek(file,0,currLoc);
return fileSize;
}
char* removeSpaces(char* str,int length){
char* tempStr = (char*) malloc(length * sizeof(char));
int counter = 0;
for(int i=0;i<length;i++){
if(str[i] != ' '){
tempStr[counter] = str[i];
counter++;
}
}
tempStr[counter] = '\0';
return tempStr;
}
int fileIt(int argc, char** argv){
FILE* file = fopen(argv[2],"r");
if (file == NULL){
printf(ERR_FILE_NOT_FOUND);
return ERR_CODE_FILE_NOT_FOUND;
}
int tUsers = totalUsers(file);
rewind(file);
printf("Total users : %d\n",tUsers);
User** users = parseUsers(file,tUsers);
calcUserTime(file,users);
for(int i=0;i < tUsers;i++){
printf("User %d : %s\n",i+1,users[i]->id);
printf("\tTotal time : %dh %dmins\n",users[i]->time.hour,users[i]->time.minute);
}
return 0;
}
char* loadLine(FILE* file, int n){
char* string = malloc(sizeof(char) * n);
for(int i =0; i < n;i++){
string[i] = fgetc(file);
if(string[i] == '\n'){
string[i+1] = '\0';
return string;
}
if(feof(file)){
string[i+1] = '\0';
return string;
}
}
return NULL;
}
int totalLines(FILE* file){
int lines = 0;
while(1){
if(fgetc(file) == '\n'){
lines++;
if(feof(file)){
return lines;
}
} else {
if(feof(file)){
lines++;
return lines;
}
}
}
}
int countChar(char* str,char c){
int cCount = 0;
int i = 0;
while(str[i] != '\0'){
if(str[i] == c){
cCount++;
i++;
}
i++;
}
return cCount;
}
int totalUsers(FILE* file){
char* line = loadLine(file,MAX_USERS_BYTE);
int tUsers = countChar(line,';');
rewind(file);
return tUsers;
}
User** parseUsers(FILE* file,int n){
char* line = loadLine(file,MAX_USERS_BYTE);
User** users = malloc(sizeof(User*) * n);
int llen = strlen(line);
char* tempId = malloc(sizeof(char) * 26);
int idCount = 0;
int userCount = 0;
for(int i =0; i < llen;i++){
if(line[i] == ';'){
tempId[idCount] = '\0';
if(!strcmp(tempId,"")){
free(tempId);
return NULL;
}
users[userCount] = malloc(sizeof(User));
strcpy(users[userCount]->id,tempId);
if(userCount >= n-1){
free(tempId);
return users;
}
userCount++;
idCount = 0;
if(line[i] == '\n'){
free(tempId);
return users;
}
}
else{
tempId[idCount] = line[i];
idCount++;
}
}
free(tempId);
return NULL;
}
int removeColon(char* str,int len){
for(int i =0;i < len;i++){
if(str[i] == ';'){
str[i] = '\0';
return i;
}
}
return -1;
}
void parseUserTime(char* line,User** users,int totalUsers){
Time start,end;
char parsedUser[26];
Time* addedTime;
sscanf(line,"%d:%d-%d:%d|%s;",&start.hour,&start.minute,&end.hour,&end.minute,parsedUser);
Time* calculatedTime = calcTime(&start,&end);
parsedUser[removeColon(parsedUser,26)] = '\0';
for(int i = 0;i < totalUsers;i++){
if(!strcmp(users[i]->id,parsedUser)){
Time* addedTime = addTime(calculatedTime,&(users[i]->time));
users[i]->time.hour = addedTime->hour;
users[i]->time.minute = addedTime->minute;
return;
}
}
return;
}
void calcUserTime(FILE* file,User** users){
int lines = totalLines(file);
rewind(file);
/* using this function to skip the first line*/
int tUsers = totalUsers(file);
parseUsers(file,tUsers);
for(int i =0;i < lines;i++){
char* line = loadLine(file,100);
line = removeSpaces(line,strlen(line));
parseUserTime(line,users,tUsers);
}
return;
}
Comments
Post a Comment