A
arnuld
WANTED: want to find whether a words exists inside a string or not and
convert it into int (its a number).
GOT: I got what I wanted
As usual, any suggestion to improve the program is welcomed
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
enum HELPER_VALUES {
VAL_SUCC = 0,
VAL_ERR = -1
};
enum BOOLEAN_VALUES {
VAL_TRUE = 1,
VAL_FALSE = 0
};
enum SIZES {
SIZE_VALUE = 10
};
enum {
RECV_TEXT_NO_MATCH = 10,
ERR_CONVERSION_CHAR_TO_INT = -10
};
int get_int_value_using_char_key(const char* t, const char* k);
void get_value_using_key(const char* t, const char* k, char* f);
int main(void)
{
int v_int = VAL_ERR;
char v_str[SIZE_VALUE+1] = {0};
const char* desc = "This is the value of Content: 123\n and this is
Size: 98";
char* k1 = "Content";
char* k2 = "Size";
get_value_using_key(desc, k1, v_str);
v_int = get_int_value_using_char_key(desc, k2);
printf("v_str = \"%s\"\n", v_str);
printf("v_int = %d\n", v_int);
return 0;
}
/* returns the integer value corresponding to the key mentioned as const
char* k */
int get_int_value_using_char_key(const char* t, const char* k)
{
int v = 0;
char value_str[SIZE_VALUE+1] = {0};
get_value_using_key(t, k, value_str);
if(value_str[0])
{
v = strtol(value_str, (char**) NULL, 10);
if(0 == v && ERANGE == errno) return ERR_CONVERSION_CHAR_TO_INT;
}
return v;
}
void get_value_using_key(const char* t, const char* k, char* fill_value)
{
int f_idx;
const int fill_size = SIZE_VALUE; /* can not use strlen(f) here as
fill_value is full of null chaacters */
char* p = NULL;
size_t len_of_key = strlen(k);
while(VAL_TRUE)
{
p = strstr(t, k);
if(NULL == p) return;
p += len_of_key;
if(*p == ':' && *(p+1) == ' ' )
{
p += 2; /* Removing the colon and next space after colon */
for(f_idx = 0; (f_idx != fill_size) && (*p != '\n'); ++p, +
+f_idx)
{
fill_value[f_idx] = *p;
}
break;
}
else
{
t = p;
}
}
}
======================= OUTPUT =====================
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra get-value-from-
key.c
[arnuld@dune programs]$ ./a.out
v_str = "123"
v_int = 98
[arnuld@dune programs]$
convert it into int (its a number).
GOT: I got what I wanted
As usual, any suggestion to improve the program is welcomed
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
enum HELPER_VALUES {
VAL_SUCC = 0,
VAL_ERR = -1
};
enum BOOLEAN_VALUES {
VAL_TRUE = 1,
VAL_FALSE = 0
};
enum SIZES {
SIZE_VALUE = 10
};
enum {
RECV_TEXT_NO_MATCH = 10,
ERR_CONVERSION_CHAR_TO_INT = -10
};
int get_int_value_using_char_key(const char* t, const char* k);
void get_value_using_key(const char* t, const char* k, char* f);
int main(void)
{
int v_int = VAL_ERR;
char v_str[SIZE_VALUE+1] = {0};
const char* desc = "This is the value of Content: 123\n and this is
Size: 98";
char* k1 = "Content";
char* k2 = "Size";
get_value_using_key(desc, k1, v_str);
v_int = get_int_value_using_char_key(desc, k2);
printf("v_str = \"%s\"\n", v_str);
printf("v_int = %d\n", v_int);
return 0;
}
/* returns the integer value corresponding to the key mentioned as const
char* k */
int get_int_value_using_char_key(const char* t, const char* k)
{
int v = 0;
char value_str[SIZE_VALUE+1] = {0};
get_value_using_key(t, k, value_str);
if(value_str[0])
{
v = strtol(value_str, (char**) NULL, 10);
if(0 == v && ERANGE == errno) return ERR_CONVERSION_CHAR_TO_INT;
}
return v;
}
void get_value_using_key(const char* t, const char* k, char* fill_value)
{
int f_idx;
const int fill_size = SIZE_VALUE; /* can not use strlen(f) here as
fill_value is full of null chaacters */
char* p = NULL;
size_t len_of_key = strlen(k);
while(VAL_TRUE)
{
p = strstr(t, k);
if(NULL == p) return;
p += len_of_key;
if(*p == ':' && *(p+1) == ' ' )
{
p += 2; /* Removing the colon and next space after colon */
for(f_idx = 0; (f_idx != fill_size) && (*p != '\n'); ++p, +
+f_idx)
{
fill_value[f_idx] = *p;
}
break;
}
else
{
t = p;
}
}
}
======================= OUTPUT =====================
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra get-value-from-
key.c
[arnuld@dune programs]$ ./a.out
v_str = "123"
v_int = 98
[arnuld@dune programs]$