-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost.c
115 lines (115 loc) · 4.11 KB
/
post.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*Domenic Bianchi*/
/*CIS 2750 Assignment 2*/
/*February 19 , 2017*/
/*This program gets user input to create a new post*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#include "stream.h"
#include "addauthor.h"
struct postEntry {
void(*postEntrysubmitPost)();
void(*postEntrygetTimeAndDatec)(char*);
userPost*(*postEntryformatEntrycccc)(char*,char*,char*,char*);
void(*postEntryreadInputci)(char*,int);
};
void postEntryreadInputci ( char input[] , int option ) {
/*Get input for stream name*/
if ( option == 1 ) {
printf ( "stream: " ) ;
fgets ( input , 256 , stdin ) ;
input[strlen ( input ) -1] = '\0' ;
}
/*Get input for post text*/
else {
char input2[512] ; clearArray ( input2 , 512 ) ;
printf ( "Enter text: " ) ;
while ( fgets ( input2 , 79 , stdin ) != NULL ) {
/*If there is a new line character at the end of the string , that means fgets has read in all of the input*/
if ( input2[strlen ( input2 ) -1] == '\n' ) {
strcat ( input , input2 ) ;
clearArray ( input2 , 512 ) ;
printf ( "- " ) ;
}
else {
strcat ( input , input2 ) ;
strcat ( input , "\n" ) ;
}
}
printf ( "ctrl-d\n" ) ;
}
}
userPost * postEntryformatEntrycccc ( char name[] , char stream[] , char text[] , char dateAndTime[] ) {
userPost * current ;
/*Malloc memory for the Post node*/
current = malloc ( sizeof ( userPost ) ) ;
/*If the malloc fails , do not create the node*/
if ( current == NULL ) {
free ( current ) ;
return NULL ;
}
current->username = name ;
current->streamname = stream ;
current->date = dateAndTime ;
current->text = text ;
return current ;
}
void postEntrygetTimeAndDatec ( char string[] ) {
time_t currentTime ;
struct tm * timeStruct ;
/*Get the current data and time from the machine this program is being run on*/
currentTime = time ( NULL ) ;
timeStruct = localtime ( ¤tTime ) ;
/*Format the time string in order to be parsed correctly in the python script*/
strftime ( string , 256 , "%b. %d, %Y %H:%M\n" , timeStruct ) ;
}
void postEntrysubmitPost ( userPost * post ) {
/*updateStream is a library function*/
updateStream ( post ) ;
free ( post ) ;
}
void constructorpostEntry (struct postEntry * ptr) {
ptr->postEntrysubmitPost = postEntrysubmitPost;
ptr->postEntrygetTimeAndDatec = postEntrygetTimeAndDatec;
ptr->postEntryreadInputci = postEntryreadInputci;
ptr->postEntryformatEntrycccc = postEntryformatEntrycccc;
}
int main ( int argc , char * argv[] ) {
if ( argc == 1 ) {
printf ( "%s\n" , "Username not included." ) ;
return 1 ;
}
struct postEntry post ;
constructorpostEntry(&post);
userPost * fullPost ;
char streamName[256] ;
char username[256] ;
char text[512] ;
char dateAndTime[256] ;
int i = 0 ;
clearArray ( streamName , 256 ) ;
clearArray ( text , 512 ) ;
clearArray ( dateAndTime , 256 ) ;
clearArray ( username , 256 ) ;
/*Prompt for user input*/
post.postEntryreadInputci ( streamName , 1 ) ;
post.postEntryreadInputci ( text , 2 ) ;
/*Get the time and date*/
post.postEntrygetTimeAndDatec ( dateAndTime ) ;
/*Loop through all arguments after the first one*/
for ( i = 1 ; argv[i] != NULL ; i++ ) {
/*Add argument to username string*/
strcat ( username , argv[i] ) ;
/*Multiple username arguments mean the username is multiple words so seperarte the arguments/words by a space*/
if ( argv[i+1] != NULL ) {
strcat ( username , " " ) ;
}
}
/*Format all the inputed information into a struct that can be sent to the stream library*/
fullPost = post.postEntryformatEntrycccc ( username , streamName , text , dateAndTime ) ;
/*Send struct to stream library*/
post.postEntrysubmitPost ( fullPost ) ;
return 0 ;
}