OK, so in the spirit of Code-Golf, I\'m trying out something new here: Code-Bowling.
In golf, you try to get the lowest score (smallest application, mos
#include
#include
#include
#include
// Forward declarations
struct HandleObj;
typedef struct HandleObj *Handle;
char* GetErrorString(Handle failed);
void DestroyObject(Handle obj);
Handle CreateTimeOfDayProvider(void);
Handle GetTimeOfDay(Handle timeOfDayProvider);
char* GetTimeOfDayName(Handle timeOfDay);
// Program
int main(int argc, char *argv[])
{
int argi = 0;
Handle todph, day;
char *name;
for(argi = 1; argi < argc; argi++) {
if(!strcmp(argv[argi], "-h") || !strcmp(argv[argi], "--help")) {
printf("Usage: ./greet [-h] [-v] [locale]\n");
printf(" -h, --help: Displays this help text.\n");
printf(" -v, --version: Displays version information\n");
return 0;
}
if(!strcmp(argv[argi], "-v") || !strcmp(argv[argi], "--version")) {
printf("0.1.0a\n");
return 0;
}
printf("Error: Unknown option \"%s\"\n", argv[argi]);
return 1;
}
if(!(todph = CreateTimeOfDayProvider())) {
printf("Could not create Time of Day Provider!\n");
printf("Reason: %s\n", GetErrorString(todph));
return 2;
}
if(!(day = GetTimeOfDay(todph))) {
printf("Could not get Time of Day!\n");
printf("Reason: %s\n", GetErrorString(day));
return 3;
}
if(!(name = GetTimeOfDayName(day))) {
printf("Could not get Time of Day object name!\n");
printf("Reason: %s\n", GetErrorString(day));
return 4;
}
printf("Good %s", name);
DestroyObject(todph);
DestroyObject(day);
return 0;
}
// Implementation Details
#define DAY_HANDLE 1
#define TIME_OF_DAY_PROVIDER_HANDLE 2
struct HandleObj
{
int objType;
void *objPTR;
char *error;
void (*Destructor)(void);
};
Handle CreateHandle(int objtype, void *objptr, void(*dtor)(void))
{
Handle obj = (Handle) malloc(sizeof(struct HandleObj));
obj->objType = objtype;
obj->objPTR = objptr;
obj->error = 0;
obj->Destructor = dtor;
}
void DestroyObject(Handle h)
{
if(h->Destructor) {
h->Destructor();
}
free(h->objPTR);
free(h);
}
#define OBJECT(type, code, handle, output) \
do { \
if(handle->objType != code) { \
handle->error = "Invalid operation for this handle"; \
return NULL; \
} \
output = (type*) handle->objPTR; \
} while(0);
typedef struct {
int timeIndex;
} DayObject;
typedef struct {
int seeed;
} TimeOfDayProviderObject;
Handle CreateTimeOfDayProvider(void)
{
TimeOfDayProviderObject *obj = (TimeOfDayProviderObject*) malloc(sizeof(TimeOfDayProviderObject));
obj->seeed = time(NULL) * 26 - 30;
return CreateHandle(TIME_OF_DAY_PROVIDER_HANDLE, obj, NULL);
}
Handle CreateTime(int timeIndex)
{
DayObject *time = malloc(sizeof(DayObject));
time->timeIndex = timeIndex;
return CreateHandle(DAY_HANDLE, time, NULL);
}
char *names[] = {"Morning", "Afternoon", "Evening", "Night"};
char* GetTimeOfDayName(Handle h)
{
DayObject *obj;
OBJECT(DayObject, DAY_HANDLE, h, obj);
return names[obj->timeIndex];
}
Handle GetTimeOfDay(Handle h)
{
TimeOfDayProviderObject *obj;
OBJECT(TimeOfDayProviderObject, TIME_OF_DAY_PROVIDER_HANDLE, h, obj);
srand(obj->seeed);
int value = rand();
obj->seeed = value;
return CreateTime(value % 4);
}
char* GetErrorString(Handle failed)
{
return failed->error;
}