Download parse_log_entry.h
//SMALL FUNCTIONS IN THIS FILE FOR PARSING STRING
//DATA FROM USER.
////Brian Smith 2022-04-10
/*Copyright (C) 2022 Brian R. Smith
* Contact: briansmith2865@gmail.com
**The source code in this file is free software: you can
**redistribute it and/or modify it under the terms of the GNU
**General Public License (GNU GPL) as published by the Free Software
**foundation, either version 3 of the License, or (at your option)
**any later version. The code is distributed WITHOUT ANY WARRANTY;
**without even the implied warranty of MERCHANTABILITY or FITNESS
**FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
**
**As additional permission under GNU GPL version 3 section 7, you
**may distribute non-source (e.g., minimized or compacted) forms of
**that code without the copy of the GNU GPL normally required by
**section 4, provided you include this license notice and a URL
**through which recipients can access the Corresponding Source.
**
** @licend The above is the entire license notice
**for the source code in this file.
*/
#ifndef PARSE_LOG_ENTRY_H
#define PARSE_LOG_ENTRY_H
#include"log_entry_def.h"
//Next func gets a line from stdin -- newline replaced
//by null terminator.
int getmaline(char buff[]) {
char* pt = buff;
int count = 0;
for(; (*pt = getchar()) != '\n'; ++pt)
++count;
*pt = '\0';
return count;
}
//I wrote this function to behave like C++ getline.
//C has a getline, but it's a bit convoluted ... I do
//also use C getline for reading from file.
//Remove leading and trailing space from string in s[].
//Returns a pointer to first non blank char in s. s[] is also modified:
//null terminator is placed at first blank char after last non-blank.
char* strip(char s[]){
char* pt = s;
for(;*pt == ' '; ++pt);
char* pt2 = pt;
for(; *pt2 != '\0'; ++pt2); --pt2;
for(; *pt2 == ' '; --pt2); ++pt2;
*pt2 = '\0';
return pt;
}
//Next func: iterates through data in buff, finds colon and
//replaces it with null terminator. Returns a pointer to byte
//after colon. char pointed to by no_colon set to binary 1
//if no colon found.
char* toColon(char* no_colon, char buff[]) {
char* pt = buff;
*no_colon = 0;
for(; *pt!= ':' && *pt != '\0'; ++pt);
if(*pt == '\0') {
*no_colon = 1;
return buff;
}
*pt = '\0'; ++pt;
return pt;
}
//NOTE: this function has a functionality similar to
//strtok() in string.h, which I should have used
// -- wasn't aware of strtok() when I wrote this one.
// Guess I should read the standard libraries :o
//Breaks on given character c ... not using this one .. I think.
//If I use it I should change it to match previous.
char* toChar(char c, char buff[]) {
char* pt = buff;
for(; *pt!= ' ' && *pt != '\0'; ++pt);
if(*pt == '\0') {
strcpy(buff, "NO_COLON");
return buff;
}
*pt = '\0'; ++pt;
return pt;
}
//Given date string yy-mm-dd, fill structure of type mydate.
int getdate(struct mydate* date, char* s) {
date -> year = 0; date -> month = 0; date -> day = 0;
char* pt = s; char* p0 = pt;
for(; *pt != '-' && *pt != '\0'; ++pt);
if(*pt = '\0')
return 1;
*pt = 0; ++pt;
date -> year = atoi(p0);
p0 = pt;
for(; *pt != '-' && *pt != '\0'; ++pt);
if(*pt = '\0')
return 1;
*pt = 0; ++pt;
date -> month = atoi(p0);
date -> day = atoi(pt);
if(date->year<=0 || date->month <= 0 || date->day <= 0 || date->month > 12 || date->day > 31)
return 1;
else
return 0;
}
//Given time string hh:mm, fill out structure mytime.
int gettime(struct mytime* time, char* s) {
time -> hour = 0; time -> minute = 0;
char* pt = s;
for(; *pt != ':' && *pt != '\0'; ++pt);
if(*pt = '\0')
return 1;
*pt = 0; ++pt;
time -> hour = atoi(s);
time -> minute = atoi(pt);
if(time->hour > 24 || time->minute > 60)
return 1;
else
return 0;
}
#endif