-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
204 lines (165 loc) · 6.08 KB
/
main.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <curl/curl.h>
#include "utils.h"
#include "stringsDef.h"
#include "errorsConstants.h"
static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp);
struct MemoryStruct {
char *memory;
size_t size;
};
int main(void)
{
CURL *curl_handle;
CURLcode res;
char *townName = (char *) secure_malloc(1);
char readKeyBoard_char=' ';
size_t townName_size=1;
int choice=TOWN_NAME;
char *name_validity_pointer=NULL;
int query_strin_Size=strlen(initValue_queryValue)+1;
char *query_string=(char *)secure_malloc(query_strin_Size);
struct MemoryStruct chunk;
secure_strcpy(query_string,initValue_queryValue);
////////////////////////////////begin big while LOOP
while(1)
{
switch(choice)
{
case PRINT_TEMPERATURE:
system("cls");
print_temp(chunk.memory);
printf("\nPress anything to go back..");
_getch();
break;
case PRINT_HUMIDITY:
system("cls");
print_humidity(chunk.memory);
printf("\nPress anything to back..");
_getch();
break;
case TOWN_NAME:
name_validity_pointer= NULL;
while(name_validity_pointer==NULL){
chunk.memory =(char*) secure_malloc(1); /* will be grown as needed by the realloc above */
chunk.size = 0; /* no data at this point */
curl_global_init(CURL_GLOBAL_ALL);
/* init the curl session */
curl_handle = curl_easy_init();
/* specify URL to get */
printf("\nplease enter townName:");
if(townName)
{
free(townName);
townName=NULL;
}
townName_size=1;
readKeyBoard_char=' ';
townName=(char *) secure_malloc(1);
while(1)
{
readKeyBoard_char=_getch();
if((int)readKeyBoard_char == ASCII_Code_ENTER)
break;
printf("%c",readKeyBoard_char);
townName[townName_size-1]=readKeyBoard_char;
townName[townName_size]='\0';
townName_size++;
townName=(char *) realloc(townName,townName_size+1);
}
townName[townName_size]='\0';
secure_strcpy(query_string,initValue_queryValue);
query_strin_Size = strlen(initValue_queryValue)+townName_size+strlen(lastPart_queryValue)+1;
query_string=(char *) realloc(query_string,query_strin_Size);
strcat_s(query_string,townName_size,townName);
strcat_s(query_string,strlen(lastPart_queryValue),lastPart_queryValue);
query_string[query_strin_Size]='\0';
curl_easy_setopt(curl_handle, CURLOPT_URL, query_string);
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
/* some servers don't like requests that are made without a user-agent
field, so we provide one */
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
/* get it! */
res = curl_easy_perform(curl_handle);
/* check for errors */
if(res != CURLE_OK) {
fprintf(stderr, "Connection to the server failed: %s\n", curl_easy_strerror(res));
}
else {
/*
* Now, our chunk.memory points to a memory block that is chunk.size
* bytes big and contains the remote file.*/
name_validity_pointer=strstr(chunk.memory,"Error: Not found city");
if(name_validity_pointer!=NULL)
{
printf("please enter a valid town name ,you have entered %s\n",townName);
free_dynamicVar(chunk.memory);
name_validity_pointer=NULL;
}
else{
name_validity_pointer = (char *) !NULL;
}
}
}
printf("\nThe town name %s is accepted",townName);
Sleep(SECOND);
break;
case ABOUT_APPLICATION:
system("cls");
printAbout();
printf("\nPress anything to back..");
_getch();
break;
case EXIT:
printf("Please enter any key to exit the Weather Application..");
_getch();
break;
}
if(choice == EXIT)
break;
system("cls");
printf("Welcome to Embedded Weather Station !\n");
printf("************The Menu ***************\n");
printf("1) Consult the temperature\n");
printf("2) Consult the humidity\n");
printf("3) change the town's name\n");
printf("4) About the application\n");
printf("5) exit the application\n");
printf("\nPlease enter a valid choice: (between 1 and 5):");
choice = DEFAULT;
choice = (int)_getch() - ASCII_Code_0;
while((choice < PRINT_TEMPERATURE) || (choice > EXIT))
{
choice=(int)getchar()-ASCII_Code_0;
}
}
///////////////////////////////end big while
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
free_dynamicVar(chunk.memory);
free_dynamicVar(query_string);
/* we're done with libcurl, so clean it up */
curl_global_cleanup();
return 0;
}
static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
mem->memory =(char *) realloc(mem->memory, mem->size + realsize + 1);
if(mem->memory == NULL) {
/* out of memory! */
printf("not enough memory (realloc returned NULL)\n");
return 0;
}
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}