commit 6dc00a542d923dfc8fa4683da9ff2f50833f471d Author: Sara Date: Sun Sep 14 22:02:49 2025 +0200 feat: added legacy code diff --git a/a.out b/a.out new file mode 100755 index 0000000..d782067 Binary files /dev/null and b/a.out differ diff --git a/attrib.c b/attrib.c new file mode 100644 index 0000000..4ea876e --- /dev/null +++ b/attrib.c @@ -0,0 +1,53 @@ +#include "string.h" +#include "stdlib.h" +#include "ctype.h" +#include "stdio.h" +#include "futil.h" + +char* g_attribs=(char*)0; +const char** g_attrib_index=(const char**)0; +int g_attribs_count=0; +int g_attribs_len=0; + +/* + * load and index an attributes file + */ +int load_attrib_file(const char* file) { + FILE* stream=fopen(file,"r"); + g_attribs_len=file_len(stream); + g_attribs_count=fcountc(stream,',')+1; + g_attribs=calloc(g_attribs_len,sizeof(char)+1); + g_attrib_index=calloc(sizeof(char*),g_attribs_count); + const char** ind_writer = g_attrib_index; + int n='\0'; + char* writer=g_attribs; + *ind_writer=writer; + ++ind_writer; + while((n=fgetc(stream))!=EOF) { + char c=(char)n; + printf("%c",c); + switch(c) { + case ',': + *writer='\0'; + if(isspace((char)fpeek(stream))) + *ind_writer=writer+2; + else + *ind_writer=writer+1; + ++ind_writer; + break; + case '\t': + case '\n': + *writer=' '; + break; + default: + *writer=c; + break; + } + ++writer; + } + *writer='\0'; + for(int i=0;i0) { + int roll = rolld(d); + total += roll; + if(i!=(void*)0) + *(i++) = roll; + } + return total; +} + +int process_dice_format(const char* format,int* num,int* die) { + int dummy; // dummy int for if either of the out adresses is 0 + if(num==(void*)0)num=&dummy; + if(die==(void*)0)die=&dummy; + char buf[10]; + memset(buf,0,10); // clear buffer + char* bufw=buf; + const char* next=format; + while(*next!='\0') { + if(*next=='d') { // argument is d in d format + if(next==format) // if there is no die count given, assume 1 + *num=1; + else + *num=atoi(buf); // store buffered string as number + memset(buf,0,10); // clear buffer + bufw=buf; // reset buffer walker + next++; // skip + } + *bufw=*next; // buffer next character + bufw++;next++; // increment walker and next character + } + *die=atoi(buf); + return 0; +} + +void roll_and_print(int n, int d) { + int* out_rolls=malloc(sizeof(int)*n); + int result=rollnd(n,d,out_rolls); + printf(" %d (%dd%d) ",result,n,d); + // only print inbetween steps if there are any + if(n>1) { + for(int i=0;i=g_argc)return 0; + const char* arg=g_argv[argind]; + char buf[10]; + memset(buf,0,10); + if(strcmp(arg,"-i")==0) { + f_interactive=1; + return process_arg(argind+1); + } else if(strcmp(arg, "-a")==0) { + load_attrib_file(g_argv[argind+1]); + return process_arg(argind+2); + } else if(isdigit(arg[0])||arg[0]=='d') { + process_dice_format(arg,&f_num,&f_die); + return process_arg(argind+1); + } + return 0; +} diff --git a/flags.h b/flags.h new file mode 100644 index 0000000..eec8ade --- /dev/null +++ b/flags.h @@ -0,0 +1,12 @@ +#ifndef _flags_h +#define _flags_h + +extern int f_interactive; +extern int f_num; +extern int f_die; +extern int g_argc; +extern const char** g_argv; + +extern int process_arg(int argind); + +#endif /* _flags_h */ diff --git a/futil.c b/futil.c new file mode 100644 index 0000000..eb69e16 --- /dev/null +++ b/futil.c @@ -0,0 +1,32 @@ +#include "stdlib.h" +#include "stdio.h" + +long file_len(FILE* file) { + fpos_t pos; + fgetpos(file,&pos); + fseek(file,0,SEEK_END); + long len=ftell(file); + fsetpos(file,&pos); + return len; +} + +long fcountc(FILE* file, char c) { + int i = 0; + long t = 0; + fpos_t strt; + fgetpos(file,&strt); + while((i=fgetc(file))!=EOF) { + if((char)i==c) + ++t; + } + fsetpos(file, &strt); + return t; +} + +int fpeek(FILE* file) { + fpos_t p; + fgetpos(file,&p); + int v = fgetc(file); + fsetpos(file,&p); + return v; +} diff --git a/futil.h b/futil.h new file mode 100644 index 0000000..5808993 --- /dev/null +++ b/futil.h @@ -0,0 +1,22 @@ +#ifndef _futils_h +#define _futils_h + +#include "stdio.h" + +/* + * return the length of a text file + */ +long file_len(FILE* file); + +/* + * count the number of occurences of c in file + * starting from the current stream position + */ +long fcountc(FILE* file, char c); + +/* + * peek the next value of file + */ +int fpeek(FILE* file); + +#endif /* _futils_h */ diff --git a/interactive.c b/interactive.c new file mode 100644 index 0000000..928cab4 --- /dev/null +++ b/interactive.c @@ -0,0 +1,19 @@ +#include "stdio.h" +#include "dice.h" +#include "string.h" +#include "flags.h" + +int run_interactive() { + int running = 1; + char buf[24]; + while(running) { + printf("> "); + memset(buf,0,10); + scanf("%s",buf); + if(strcmp(buf,"quit")==0||strcmp(buf,"exit")==0||strcmp(buf,"q")==0) return 0; + process_dice_format(buf,&f_num,&f_die); + if(f_num!=0&&f_die!=0||buf[0]=='\0') + roll_and_print(f_num, f_die); + } + return 0; +} diff --git a/interactive.h b/interactive.h new file mode 100644 index 0000000..2badb87 --- /dev/null +++ b/interactive.h @@ -0,0 +1,6 @@ +#ifndef _interactive_h +#define _interactive_h + +extern int run_interactive(); + +#endif /* _interactive_h */ diff --git a/main.c b/main.c new file mode 100644 index 0000000..67a5d61 --- /dev/null +++ b/main.c @@ -0,0 +1,19 @@ +#include "time.h" +#include "stdlib.h" +#include "dice.h" +#include "flags.h" +#include "interactive.h" + +int main(int argc,const char* argv[]) { + srand(time(NULL)); + g_argv=argv; + g_argc=argc; + process_arg(1); + // if there are valid dice to roll + if(f_die>0&&f_num>0) { + roll_and_print(f_num,f_die); + } + if(f_interactive==1||argc==1) { + return run_interactive(); + } +} diff --git a/pc.c b/pc.c new file mode 100644 index 0000000..d9ca797 --- /dev/null +++ b/pc.c @@ -0,0 +1,8 @@ +#include "pc.h" +#include "memory.h" + +int new_character(pc_t* pc, const char* name) { + memset(pc,0,sizeof(pc_t)); + strncpy(pc->name,name,64); + return 0; +} diff --git a/pc.h b/pc.h new file mode 100644 index 0000000..69a9698 --- /dev/null +++ b/pc.h @@ -0,0 +1,13 @@ +#ifndef _pc_h +#define _pc_h + +typedef struct pc_t { + char name[64]; + int dexterity,strength,constitution,intelligence,wisdom; + const char* spells[64]; + const char* traits[64]; +} pc_t; + +int new_character(pc_t* pc, const char* name); + +#endif /* _pc_h */ diff --git a/test.csv b/test.csv new file mode 100644 index 0000000..00ad55d --- /dev/null +++ b/test.csv @@ -0,0 +1 @@ +firebolt, fireball, mage hand, magic shield