Commit b65b77ff authored by saeed's avatar saeed
Browse files

more modification

parent 59304851
......@@ -4,7 +4,10 @@
#include <netinet/in.h> /* struct sockaddr_in, struct sockaddr */
#include <netdb.h> /* struct hostent, gethostbyname */
#include"strcomp.h"
#define N (1024)
#include"splitstring.h"
#include"strtoint.h"
#define N (2048)
int main(int argc,char *argv[])
{
......@@ -79,26 +82,26 @@ int main(int argc,char *argv[])
//received = 0;
//bytes = 0;
do {
while((bytes = recv(sockfd, response, N, 0)) > 0){
// HANDLE RESPONSE CHUCK HERE BY, FOR EXAMPLE, SAVING TO A FILE.
//memset(response, 0, sizeof(response));
fprintf(fl,"%s",response);
for (int i=0; i < N; ++i) {
response[i] = 0;
*(response+i) = 0;
}
bytes = recv(sockfd, response, N-1, 0);
response[N-1] = 0;
if (bytes > 0 ){
//bytes = recv(sockfd, response, N-1, 0);
//if (bytes > 0 ){
// response[bytes] = '\0';
//printf("%s", response);
fprintf(fl,"%s",response);
}
if (bytes < 0 )return 1;
if (bytes == 0)break;
//}
//if (bytes < 0 )return 1;
//if (bytes == 0)break;
//received+=bytes;
//printf("here \n");
} while(1);
}; //while(1);
//close(sockfd);
//fclose(fl);
//fl = fopen("output.dat","r");
......@@ -119,14 +122,49 @@ int main(int argc,char *argv[])
il += 1;
}
printf("%s",content);
//close(sockfd);
fclose(fl);
int ir = 0;
for (long int i=0L; i < numc;++i){
if (content[i] == '\r') ir =i;
}
//printf("%c\n",content[ir]);
//printf("%c\n",content[ir+1]);
//for (int i=(ir+3); i < (numc-1);++i){
// printf("%c",content[i]);
//}
printf("\n");
printf("%ld\n",numc - ir - 2);
char *st = &content[ir+3];
int icom = 0, iicom = 0;
char **cout =split(st, ',', &icom);
char **ccout = NULL;
for (int i=0; i < (icom); ++i){
ccout = split(cout[i], ':', &iicom);
rtrim(ccout[2],'}');
ltrim(ccout[0],'\"');
rtrim(ccout[0],'\"');
printf("%d\t%s\n",stoi(ccout[0]),ccout[2]);
free(ccout);
}
free(content);
free(cout);
//printf("%s",content);
/* process response */
//printf("Response:\n%s\n",response);
fclose(fl);
return 0;
}
......
#ifndef SPLISTRING_H
#define SPLISTRING_H
char **split(char *st, char delim, int *NF){
int nc= 0;
char *temp = st;
while(*temp != '\0'){
if ( *temp == delim){
nc++;
temp++;
}
else{
temp++;
}
}
*NF = (nc);
char **cout = NULL;
int ni = 0;
int nj = -1;
char *tt = st;
if (nc > 0){
cout = (char **) malloc(sizeof(char *)*(nc+1));
temp = st;
while(*temp != '\0'){
if ( *temp == delim){
nj++;
cout[nj] = (char *)malloc(sizeof(char) *(ni+1));
for (int i=0; i <ni;++i){
cout[nj][i] = *tt;
tt++;
}
cout[nj][ni] = '\0';
ni=0;
temp++;
tt++;
}
else{
temp++;
ni++;
}
}
}
temp = st;
nj = 0;
ni = 0;
while(*temp != '\0'){
if ( *temp == delim){
nj++;
if (nj == nc){
tt = temp;
ni = 0;
tt++;
while(*tt != '\0'){
tt++;
ni++;
}
cout[nc] = (char*)malloc(sizeof(char)*(ni+1));
tt = temp;
ni = 0;
tt++;
while(*tt != '\0'){
cout[nc][ni] = *tt;
tt++;
ni++;
}
cout[nc][ni] = '\0';
}
}
temp++;
}
return cout;
}
#endif
#ifndef STRCOMP_H
#define STRCOMP_H
int Strlen(char *sa){
int len = 0;
while(*sa != '\0'){
len += 1;
sa++;
}
return len;
}
int Strcmp(char *sa, char *sb){
if (Strlen(sa) != Strlen(sb)){
return 0;
}
while ((*sa == *sb) && *sa != '\0' && *sb != '\0' ){
sa++;
sb++;
}
if (*sa == '\0'){
return 1;
}
else{
return 0;
}
}
void ltrim(char *st, char delim){
while(*st != '\0'){
if (*st == delim) break;
st++;
}
char *tm = st;
while( *tm != '\0'){
*st = *(tm+1);
st++;
tm++;
}
}
void rtrim(char *st, char delim){
while(*st != '\0'){
if (*st == delim) *st = '\0';
st++;
}
}
#endif
#ifndef STRTOINT_H
#define STRTOINT_H
int pow10(int n){
int d = 1;
if (n > 0){
for (int i=0; i < n;++i) d *= 10;
}
return d;
}
int stoi(char *st){
int sign = 0;
int nc=0;
char *temp = st;
while (*temp != '\0' ){
if ( *temp != ' ') nc++;
temp++;
}
nc--;
int d = 0;
int c = 1;
while (*st != '\0'){
if (*st == '-'){
c = -1;
nc--;
st++;
sign += 1;
if (sign > 1 ) return 0;
}
else if (*st == '+'){
c = 1;
nc--;
st++;
sign += 1;
if (sign > 1 ) return 0;
}
else if( *st != ' ') {
d += ((int)(*st - '0')*pow10(nc));
st++;
nc--;
}
else {
st++;
}
}
return (c*d);
}
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment