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
| #ifdef __cplusplus
#error This file requires a C compiler
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern int fpurge(FILE*);
extern int fclean(char*, FILE*);
struct class
{
int classID;
char * className;
};
char *mystrdup(char const *str)
{
char *new = NULL;
if(str != NULL)
new = malloc(strlen(str)+1);
if(new != NULL)
strcpy(new, str);
return new;
}
#define NBCLASS 10
#define CLASSNAME_SIZE 64
#define TEMPLATENAME_SIZE 32
int saveTemplate(struct class const *classes, size_t n)
{
(void)classes;
(void)n;
/* ... */
return -1;
}
int main(void)
{
struct class template[NBCLASS];
size_t i;
int err=0;
for(i=0 ; i<NBCLASS ; i++)
{
template[i].classID = -1;
template[i].className = NULL;
}
printf("--- Document Template entry ---\n");
printf("A document template can hold up to %d classes\n", NBCLASS);
for(i=0 ; i<NBCLASS && !err ; i++)
{
int scanned;
int id;
do
{
printf("Enter id for class #%d :", (int)i);
fflush(stdout);
scanned = scanf("%d", &id);
if(fpurge(stdin) < 0) break;
} while(scanned != 1);
if(scanned != 1)
{
err = 1;
break;
}
if(id < 0) break;
{
struct class * class = &template[i];
char className[CLASSNAME_SIZE];
printf("Enter class name (max. %d chars) :", CLASSNAME_SIZE-1);
fflush(stdout);
fgets(className, CLASSNAME_SIZE, stdin);
fclean(className, stdin);
//Create the class
class->classID = id;
class->className = mystrdup(className);
}
}
if(!err)
{
err = saveTemplate(template, i);
}
for(i=0 ; i<NBCLASS ; i++)
{
free(template[i].className), template[i].className=NULL;
}
return (err ? EXIT_FAILURE : EXIT_SUCCESS);
} |