#include <stdlib.h>
#include <string.h>
#include <stdio.h>
/*
*Ejemplos,Técnica y C
*http://xchemax.blogspot.com.es/
*/
typedef enum E_info{nombre1,nombre2,apellido1,
apellido2,cedula,provincia,ciudad,canton,LAST_INFO}E_info;
typedef struct info_votante{
char *votante[LAST_INFO];
char buf[50 *LAST_INFO];
}info_votante;
typedef struct DataBase{
info_votante **ptr;
info_votante **end;
info_votante **data;
}DataBase;
typedef struct Info_{
int id;
char *name;
}Info_;
Info_ Info[LAST_INFO]= {
{nombre1,"nombre1"},
{nombre2, "nombre2"},
{apellido1,"apellido1"},
{apellido2,"apellido2"},
{cedula,"cedula"},
{provincia,"provincia"},
{ciudad,"ciudad"},
{canton,"canton"}
};
char *leyen[] ={
"SUPERBASE 1.1\nEl Sistema de informacion Electoral "
"Numero 1 del mundo mundial\n"
"\nOpciones\nIniciar 1\nsalir 2\n",
"\n------\nOpciones \nVer Ultima entrada 1\nListar"
"base 2\nsalir 3\n Cualquier otra tecla continua....\n"
};
DataBase *dbase;
void PrintInfo(info_votante *iv){
int i;
for( i = 0; i<LAST_INFO; i++){
printf("%s.....\t%s\n",Info[i].name, iv->votante[i]);
}
}
void InitDataBase(int n){
dbase = malloc( sizeof(DataBase));
dbase->data = malloc(n * sizeof(info_votante*));
dbase->ptr = dbase->data ;
dbase->end = (dbase->data +n);
}
void AddToDbase(info_votante *iv){
int size;
if(dbase->ptr >= dbase->end){
size = dbase->end - dbase->data;
dbase->data = realloc(dbase->data,(size + size)* sizeof(info_votante*) );
dbase->ptr = dbase->data + size ;
dbase->end = dbase->data + (size + size);
}
*dbase->ptr = iv;
dbase->ptr++;
}
void ListDBase(){
info_votante **raw;
raw = dbase->data;
while(raw < dbase->ptr){
PrintInfo(*raw);
raw++;
printf("\n*********\n" );
}
}
void FreeAll(){
info_votante **tmp ;
info_votante *iv;
int i;
int size;
size = dbase->ptr - dbase->data;
tmp = dbase->data;
for ( i = 0; i < size; i++){
iv = *tmp;
free(iv);
tmp++;
}
free(dbase->data);
free(dbase);
}
int Init(void){
int i;
char c;
char buf[50];
int size;
int len;
int stop = 0;
info_votante *iv;
memset(buf, 0,50);
while(1){
/*
*el bucle for() es reentrante.
*una forma de "limpiar" la ultima linea y evitar
*que la primera lectura de fgets() en el bucle for() sea falsa.
*/
fgets(buf,50,stdin);
iv = malloc(sizeof(info_votante));
AddToDbase(iv);
size = 0;
for( i = 0; i<LAST_INFO; i++){
printf(":%s.....\t", Info[i].name);
fgets(buf,50,stdin);
len = strlen(buf);
if(len >= 50 -2){
printf("Te has pasado de la raya\n" );
len = 1;
}
iv->votante[i] = iv->buf +size;
memcpy(iv->buf +size,buf,len-1);
size += len;
iv->buf[size]= 0x0;
}
printf("%s",leyen[1] );
c = getchar();
switch(c){
case '1':
PrintInfo(iv);
break;
case '2':
ListDBase();
break;
case '3':
stop = 1;
break;
default:
break;
}
if(stop == 1)
break;
}
FreeAll();
return 0;
}
int main(void){
char c;
printf("%s",leyen[0] );
c = getchar();
switch(c){
case '1':
InitDataBase(1);
Init();
break;
case '2':
break;
case '3':
break;
default:
break;
}
return 0;
}