A
arnuld
Program works fine. I know there are improvements left to be done but
they unidentified by as of now. Help will be appreciated as usual .
2nd, any strange values that can crash this program and how to avoid them.
/* A program to save input. Input is a character string of key=value
pairs separated by a separator. e.g "name=clc,messages=100".
* We will write a program which will save value when provided with a
belonging key.
*
* VERSION 0.0
*/
#include <stdio.h>
#include <string.h>
enum {
SIZE_VALUE = 20
};
int get_value_using_key(const char* s, const char* k, const int lenk,
char* v, const int lenv);
int main(void)
{
const char* arr = "group=,messages=";
const char* key = "group";
char value[SIZE_VALUE+1] = {0};
int ret;
printf("input: %s\n", arr);
ret = get_value_using_key(arr, key, strlen(key), value, SIZE_VALUE);
if(ret >= 0)
{
printf("%s: %s\n", key, value);
}
else
{
printf("Error finding values\n");
}
printf("ret: %d\n", ret);
return 0;
}
/* if input (fist argument) is not a character string then this program
will Segfault. Do not know how to avoid this */
int get_value_using_key(const char* s, const char* k, const int lenk,
char* v, const int lenv)
{
int idx;
char* p;
int bool_small_string = 0;
const char separator = ',';
p = strstr(s, k);
if(NULL == p)
{
printf("No match found\n");
return -1;
}
p = p + lenk + 1; /* +1 for equal character*/
printf("*p = %c\n", *p);
for(idx = 0; idx != lenv; ++idx)
{
if('\0' == *p || separator == *p)
{
bool_small_string = 1;
break;
}
*v++ = *p++;
}
if(bool_small_string)
{
printf("Input size is lesser than recommended. This is fine\n");
}
else if(('\0'== *p || separator == *p) && lenv == idx)
{
printf("Input Size exhausted completely\n");
}
else
{
printf("*p: %c, idx: %d, lenv: %d\n", *p, idx, lenv);
printf("*ERROR - size of input exceeded the allowed limit\n");
return -1;
}
return idx; /* return number of characters read */
}
================= OUTPUT ====================
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra save-values.c
[arnuld@dune programs]$ ./a.out
input: group=clc,messages=
*p = c
Input size is lesser than recommended. This is fine
group: clc
ret: 3
[arnuld@dune programs]$
they unidentified by as of now. Help will be appreciated as usual .
2nd, any strange values that can crash this program and how to avoid them.
/* A program to save input. Input is a character string of key=value
pairs separated by a separator. e.g "name=clc,messages=100".
* We will write a program which will save value when provided with a
belonging key.
*
* VERSION 0.0
*/
#include <stdio.h>
#include <string.h>
enum {
SIZE_VALUE = 20
};
int get_value_using_key(const char* s, const char* k, const int lenk,
char* v, const int lenv);
int main(void)
{
const char* arr = "group=,messages=";
const char* key = "group";
char value[SIZE_VALUE+1] = {0};
int ret;
printf("input: %s\n", arr);
ret = get_value_using_key(arr, key, strlen(key), value, SIZE_VALUE);
if(ret >= 0)
{
printf("%s: %s\n", key, value);
}
else
{
printf("Error finding values\n");
}
printf("ret: %d\n", ret);
return 0;
}
/* if input (fist argument) is not a character string then this program
will Segfault. Do not know how to avoid this */
int get_value_using_key(const char* s, const char* k, const int lenk,
char* v, const int lenv)
{
int idx;
char* p;
int bool_small_string = 0;
const char separator = ',';
p = strstr(s, k);
if(NULL == p)
{
printf("No match found\n");
return -1;
}
p = p + lenk + 1; /* +1 for equal character*/
printf("*p = %c\n", *p);
for(idx = 0; idx != lenv; ++idx)
{
if('\0' == *p || separator == *p)
{
bool_small_string = 1;
break;
}
*v++ = *p++;
}
if(bool_small_string)
{
printf("Input size is lesser than recommended. This is fine\n");
}
else if(('\0'== *p || separator == *p) && lenv == idx)
{
printf("Input Size exhausted completely\n");
}
else
{
printf("*p: %c, idx: %d, lenv: %d\n", *p, idx, lenv);
printf("*ERROR - size of input exceeded the allowed limit\n");
return -1;
}
return idx; /* return number of characters read */
}
================= OUTPUT ====================
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra save-values.c
[arnuld@dune programs]$ ./a.out
input: group=clc,messages=
*p = c
Input size is lesser than recommended. This is fine
group: clc
ret: 3
[arnuld@dune programs]$