A
arnuld
WANTED: To print the value of the key.
GOT: Weired printing on screen
Key is the +OK string and its value is separated by a space from it e.g.
+OK HTTP_REQUEST
I have a full sentence which contains many characters (even newlines) and
I want to take out the value of the key from it. Its printing almost a
mess. When I print an array it prints fine but if it is passed to another
function then it prints mess.
#include <stdio.h>
#include <string.h>
enum { SIZE_ARR = 50 };
int string_copy_till_newline(char dest[], char src[]);
int get_pointer_for_value_using_key(const char* recv_arr, char* id);
int main(void)
{
char arr_dest[SIZE_ARR+1] = {0};
char arr_src[] = "Content-Type: HTTP/response\nContent-Length: 41\n\n
+OK HTTP_REQUEST_ACCEPTED\n---END\n";
printf("arr_dest = %s\n", arr_dest);
printf("arr_src = %s\n", arr_src);
printf("\n------------------------------\n");
if(get_pointer_for_value_using_key(arr_src, arr_dest))
{
string_copy_till_newline(arr_dest, arr_src);
printf("arr_dest = %s\n", arr_dest);
/* printf("arr_src = %s\n", arr_src);*/
}
else
{
printf("get_pointer_for_value_using_key() returned NULL\n");
}
return 0;
}
int get_pointer_for_value_using_key(const char* recv_arr, char* id)
{
char* p = strstr(recv_arr, "+OK");
if(NULL == p || NULL == id)
{
printf("IN: %s - %d: ONe of the arguments is NULL. This is SERIOUS
ERROR\n", __func__, __LINE__);
printf("p = %p\nid = %p\n", (void*)p, (void*)id);
return 0;
}
else /* Skip first 4 characters (+OK and a space) to read the unique ID
*/
{
p += 4;
printf("p --> %s\n", p);
return string_copy_till_newline(id, p);
}
}
/* Will copy contents from SRC to DEST till a newline occurs, will not
include newline, puts a NULL character at the end.
returns number of characters copied, else -1 on error. Will write
beyond the array, size checking is user's responsibility */
int string_copy_till_newline(char dest[], char src[])
{
int idx;
if(NULL == dest || NULL == src)
{
printf("IN: %s at %d: One of the arguments is NULL\n", __func__,
__LINE__);
return -1;
}
printf("src =%s\n", src);
/*
for(idx = 0; (idx < SIZE_ARR) && src[idx] != '\0' src[idx] != '\n'; +
+idx)
{
printf("src[idx] = %c\n", src[idx]);
dest[idx] = src[idx];
}
dest[idx] = '\0';
*/
return idx;
}
===================== OUTPUT =============================
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra string-play.c
[arnuld@dune programs]$ ./a.out
arr_dest =
arr_src = Content-Type: HTTP/response
Content-Length: 41
+OK HTTP_REQUEST_ACCEPTED
---END
------------------------------
p --> HTTP_REQUEST_ACCEPTED
---END
src =HTTP_REQUEST_ACCEPTED
---END
src =Content-Type: HTTP/response
Content-Length: 41
+OK HTTP_REQUEST_ACCEPTED
---END
arr_dest =
GOT: Weired printing on screen
Key is the +OK string and its value is separated by a space from it e.g.
+OK HTTP_REQUEST
I have a full sentence which contains many characters (even newlines) and
I want to take out the value of the key from it. Its printing almost a
mess. When I print an array it prints fine but if it is passed to another
function then it prints mess.
#include <stdio.h>
#include <string.h>
enum { SIZE_ARR = 50 };
int string_copy_till_newline(char dest[], char src[]);
int get_pointer_for_value_using_key(const char* recv_arr, char* id);
int main(void)
{
char arr_dest[SIZE_ARR+1] = {0};
char arr_src[] = "Content-Type: HTTP/response\nContent-Length: 41\n\n
+OK HTTP_REQUEST_ACCEPTED\n---END\n";
printf("arr_dest = %s\n", arr_dest);
printf("arr_src = %s\n", arr_src);
printf("\n------------------------------\n");
if(get_pointer_for_value_using_key(arr_src, arr_dest))
{
string_copy_till_newline(arr_dest, arr_src);
printf("arr_dest = %s\n", arr_dest);
/* printf("arr_src = %s\n", arr_src);*/
}
else
{
printf("get_pointer_for_value_using_key() returned NULL\n");
}
return 0;
}
int get_pointer_for_value_using_key(const char* recv_arr, char* id)
{
char* p = strstr(recv_arr, "+OK");
if(NULL == p || NULL == id)
{
printf("IN: %s - %d: ONe of the arguments is NULL. This is SERIOUS
ERROR\n", __func__, __LINE__);
printf("p = %p\nid = %p\n", (void*)p, (void*)id);
return 0;
}
else /* Skip first 4 characters (+OK and a space) to read the unique ID
*/
{
p += 4;
printf("p --> %s\n", p);
return string_copy_till_newline(id, p);
}
}
/* Will copy contents from SRC to DEST till a newline occurs, will not
include newline, puts a NULL character at the end.
returns number of characters copied, else -1 on error. Will write
beyond the array, size checking is user's responsibility */
int string_copy_till_newline(char dest[], char src[])
{
int idx;
if(NULL == dest || NULL == src)
{
printf("IN: %s at %d: One of the arguments is NULL\n", __func__,
__LINE__);
return -1;
}
printf("src =%s\n", src);
/*
for(idx = 0; (idx < SIZE_ARR) && src[idx] != '\0' src[idx] != '\n'; +
+idx)
{
printf("src[idx] = %c\n", src[idx]);
dest[idx] = src[idx];
}
dest[idx] = '\0';
*/
return idx;
}
===================== OUTPUT =============================
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra string-play.c
[arnuld@dune programs]$ ./a.out
arr_dest =
arr_src = Content-Type: HTTP/response
Content-Length: 41
+OK HTTP_REQUEST_ACCEPTED
---END
------------------------------
p --> HTTP_REQUEST_ACCEPTED
---END
src =HTTP_REQUEST_ACCEPTED
---END
src =Content-Type: HTTP/response
Content-Length: 41
+OK HTTP_REQUEST_ACCEPTED
---END
arr_dest =