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 205
|
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BEGIN '{'
#define SEP ':'
#define BUF_SIZE 128
#define FILENAME "perso.txt"
#define END '}'
typedef struct
{
char * name;
char * address;
char * phone;
int age;
}
contact_infos;
typedef enum
{
NAME,
AGE,
ADDRESS,
PHONE,
NB_TAG
}
contact_tags;
static const char *fields[] = { "name", "age", "address", "phone" };
static char * str_dup (const char * str)
{
char * dup = NULL;
if (str != NULL)
{
size_t size = strlen (str) + 1;
dup = malloc (size);
if (dup != NULL)
{
memcpy (dup, str, size);
}
}
return dup;
}
static void str_finalize (char * str)
{
char * p = strchr (str, '\n');
if (p != NULL)
{
*p = 0;
}
}
static void contact_infos_free (contact_infos ** p)
{
if (p != NULL && *p != NULL)
{
free ((*p)->name);
free ((*p)->address);
free ((*p)->phone);
free (*p);
*p = NULL;
}
}
static void get (contact_infos * p, char * s, contact_tags tag)
{
char buff [BUF_SIZE];
if (s != NULL)
{
switch (tag)
{
case NAME:
{
p->name = str_dup (s);
}
case AGE:
{
p->age = strtol (s, NULL, 10);
}
break;
case ADDRESS:
{
p->address = str_dup (s);
}
break;
case PHONE:
{
p->phone = str_dup (s);
}
break;
default:
break;
}
}
}
static void fillInfo (contact_infos *info, char *buff)
{
char * s = NULL;
int i;
s = strchr (buff, SEP);
if (s != NULL)
{
*s = '\0';
i = 0;
while (buff[i] != '\0' && (buff[i] == ' ') || (buff[i] == '\t'))
i++;
buff += i;
for (i=0; i < NB_TAG; i++)
{
if (strcmp (buff, fields[i]) == 0)
{
s++;
str_finalize (s);
get (info, s, i);
break;
}
}
}
}
static contact_infos * get_info (const char * contact_name)
{
contact_infos * ret = NULL;
contact_infos tmp;
FILE * file = NULL;
char buff [BUF_SIZE];
int opened = 0;
file = fopen (FILENAME, "r");
if (file != NULL)
{
while ((fgets (buff, BUF_SIZE, file) != NULL))
{
if (buff[0] == BEGIN)
{
memset (&tmp, 0, sizeof (tmp));
while ((fgets (buff, BUF_SIZE, file) != NULL))
{
fillInfo (&tmp, buff);
if (buff[0] == END)
{
break;
}
}
if (strcmp (contact_name, tmp.name) == 0)
{
ret = malloc (sizeof (*ret));
memcpy (ret, &tmp, sizeof (tmp));
break;
}
free (tmp.name);
free (tmp.address);
free (tmp.phone);
}
}
fclose (file);
}
return ret;
}
int main (void)
{
contact_infos * info = get_info ("Franck.H");
printf (
"%s\n%d\n%s\n%s\n\n",
info->name, info->age, info->address, info->phone
);
contact_infos_free (& info);
return EXIT_SUCCESS;
} |
Partager