-
-
Save emphursis/ff282e007bf0d682bb19 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//pijarvis | |
//copyright (C) 2013 Thomas Daniels (http://pijarvis.blogspot.co.uk/) | |
//This program is free software: you can redistribute it and/or modify | |
// it under the terms of the GNU General Public License as published by | |
// the Free Software Foundation, either version 3 of the License, or | |
// (at your option) any later version. | |
// | |
// This program is distributed in the hope that it will be useful, | |
// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
// GNU General Public License for more details. | |
// | |
// You should have received a copy of the GNU General Public License | |
// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
#include <stdio.h> | |
#include <sys/wait.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <sys/types.h> | |
#include <signal.h> | |
#include <fcntl.h> | |
#include <sys/stat.h> | |
#include <time.h> | |
#include <string.h> | |
char *args[256]; | |
int errors(int flag, int fd, int status) //add more here, same as from sched.c | |
{ | |
if(flag == 1) //checks the file opens | |
{ | |
if(fd == -1) | |
{ | |
printf("Error opening file\n"); | |
exit(1); | |
} | |
} | |
if(flag == 2) //checks the file can be read | |
{ | |
if(status == -1) | |
{ | |
printf("Error reading file\n"); | |
exit(1); | |
} | |
} | |
} | |
int reading(char ybuff[20]) //to read the xml file | |
{ | |
int fd, status, i, x = 0, n = 0; | |
int bytesread = 0, eof=0; | |
char buffer[256]; | |
char weatherdata[100000]; //probably slighty too large, but it does need to read in the entire file | |
char todayweather[1000]; //again, probably too large | |
fd = open("weather.xml", O_RDONLY, 0); //opens the file in read only | |
errors(1, fd, 0); //checks the file opened correctly | |
while(!eof) | |
{ | |
status = read(fd, buffer, sizeof(buffer)); //reads in the file 256 chars at a time | |
errors(2, fd, status); //more error checking, this time that it can read the file | |
bytesread += status; //running tally of how large the file is | |
if(status == 0) | |
{ | |
eof = 1; | |
} | |
else | |
{ | |
for(i=0;i<status;i++) | |
{ | |
weatherdata[x] = buffer[i]; //copies the buffer to the array | |
x++; | |
} | |
} | |
} | |
weatherdata[x] = '\0'; //terminates the array | |
close(fd); //housekeeping | |
x = 0; | |
for(i=0;i<=bytesread;i++) //finds the correct day | |
{ | |
if(weatherdata[i] == ybuff[0] && weatherdata[i+1] == ybuff[1] && weatherdata[i+2] == ybuff[2] && weatherdata[i+3] == ybuff[3] && weatherdata[i+4] == ybuff[4] && weatherdata[i+5] == ybuff[5] && weatherdata[i+6] == ybuff[6] && weatherdata[i+7] == ybuff[7] && weatherdata[i+8] == ybuff[8] && weatherdata[i+9] == ybuff[9] && weatherdata[i+10] == 'Z') | |
{ | |
x = i+17; | |
for(x;weatherdata[x]!='>';x++) | |
{ | |
todayweather[n] = weatherdata[x]; | |
n++; | |
} | |
todayweather[n] = '\0'; | |
} | |
} | |
for(i=0;i<=sizeof(todayweather);i++) //searches for any " and replaces with -, makes strtok simpler | |
{ | |
if(todayweather[i] == '"') | |
{ | |
todayweather[i] = '-'; | |
} | |
} | |
weather(todayweather); | |
return 1; | |
} | |
int weather(char todayweather[1000]) //to parse the data | |
{ | |
char *tokres, *tokres2; | |
char string[1000]; | |
char *delim = " ", *delim2 = "-"; | |
char *content[10], *content2; | |
int weathcode; | |
int i = 0; | |
switch(todayweather[4]) //checks the wind direction before strtok breaks the string | |
{ | |
case 'N': | |
args[8] = ": The wind is coming from the north"; | |
break; | |
case 'E': | |
args[8] = ": The wind is coming from the east"; | |
break; | |
case 'S': | |
args[8] = ": The wind is coming from the south"; | |
break; | |
case 'W': | |
args[8] = ": The wind is coming from the west"; | |
break; | |
default: | |
args[8] = "I'm sorry, I don't know the wind direction"; | |
} | |
strcpy(string, todayweather); //splits the string into each chunk of weather data | |
tokres = strtok(string, delim); | |
while(tokres !=NULL) | |
{ | |
content[i] = tokres; | |
tokres = strtok(NULL, delim); | |
i++; | |
} | |
content[i] = '\0'; | |
tokres2 = strtok(content[8], delim2); //to get the weather code | |
tokres2 = strtok(NULL, delim2); //dont want anything before the first - so strtok is run again | |
content2 = tokres2; | |
for(i=0;i<=sizeof(content2);i++) | |
{ | |
if(content2[i] == '-') | |
{ | |
content2[i] = '\0'; | |
} | |
} | |
weathcode = atoi(content2); //converts the code into an int | |
switch(weathcode) //checks the weather code against the weather types and sets the speech accordingly | |
{ | |
case 1: | |
args[4] = "Today will be sunny"; | |
break; | |
case 3: | |
args[4] = "Today will be partially cloudy"; | |
break; | |
case 5: | |
args[4] = "Today will be misty"; | |
break; | |
case 6: | |
args[4] = "Today will be foggy"; | |
break; | |
case 7: | |
args[4] = "Today will be cloudy"; | |
break; | |
case 8: | |
args[4] = "Today will be overcast"; | |
break; | |
case 10: | |
args[4] = "Today there will light rain showers"; | |
break; | |
case 11: | |
args[4] = "Today will be drizzley"; | |
break; | |
case 12: | |
args[4] = "Today there wil be light rain"; | |
break; | |
case 14: | |
args[4] = "Today there will be heavy rain shower"; | |
break; | |
case 15: | |
args[4] = "Today there will be heavy rain"; | |
break; | |
case 17: | |
args[4] = "Today there will be sleet shower"; | |
break; | |
case 18: | |
args[4] = "Today will be sleety"; | |
break; | |
case 20: | |
args[4] = "Today there will be hail showers"; | |
break; | |
case 21: | |
args[4] = "Today there will be hail"; | |
break; | |
case 23: | |
args[4] = "Today there will be light snow showers"; | |
break; | |
case 24: | |
args[4] = "Today there will be light snow"; | |
break; | |
case 26: | |
args[4] = "Today there will be heavy snow showers"; | |
break; | |
case 27: | |
args[4] = "Today there will be heavy snow"; | |
break; | |
case 29: | |
args[4] = "Today there will be thundery showers"; | |
break; | |
case 30: | |
args[4] = "Today will be thundery"; | |
break; | |
default: | |
args[4] = "I'm sorry, I don't know the weather"; | |
} | |
tokres2 = strtok(content[6], delim2); //to get the temperature | |
tokres2 = strtok(NULL, delim2); //same as before, to strip the data from before the - | |
args[5] = "with a maximum temperature of "; | |
args[6] = tokres2; | |
args[7] = "degrees"; | |
tokres2 = strtok(content[4], delim2); //to get the wind speed | |
tokres2 = strtok(NULL, delim2); | |
args[9] = "at a speed of"; | |
args[10] = tokres2; | |
args[11] = "miles per hour"; | |
} | |
int exec(void) //to exectue the script | |
{ | |
pid_t pid; | |
int status; | |
pid = fork(); | |
if(pid == -1) | |
{ | |
printf("Error with forking\n"); | |
exit(1); | |
} | |
if(pid == 0) | |
{ | |
execvp(args[0], args); | |
printf("command not recognised\n"); | |
return 0; | |
} | |
else | |
{ | |
waitpid(pid, &status, 0); | |
} | |
} | |
int main(void) | |
{ | |
pid_t pid; | |
time_t myTime; | |
struct tm * locTime; | |
char ybuff[20]; //stores the year | |
char dbuff[20]; //stores the day | |
args[0] = "speech.sh"; | |
args[1] = "Good morning Tom. Today is "; | |
myTime = time(NULL); | |
locTime = localtime(&myTime); | |
strftime (ybuff, sizeof(ybuff), "%F", locTime); //gets the year | |
strftime (dbuff, sizeof(dbuff), "%a", locTime); //gets the date | |
if(dbuff[0] != 'S') | |
{ | |
switch(dbuff[0]) | |
{ | |
case 'M': | |
args[2] = "Monday:"; | |
break; | |
case 'T': | |
if(dbuff[1] == 'u') | |
{ | |
args[2] = "Tuesday:"; | |
} | |
else | |
{ | |
args[2] = "Thursday:"; | |
} | |
break; | |
case 'W': | |
args[2] = "Wednesday:"; | |
break; | |
case 'F': | |
args[2] = "Friday"; | |
break; | |
} | |
args[3] = "And the time is six twenty ai em:"; | |
reading(ybuff); | |
args[12] = '\0'; | |
exec(); | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment