first commit
This commit is contained in:
commit
8d5478c6bb
9 changed files with 143 additions and 0 deletions
22
Makefile
Normal file
22
Makefile
Normal file
|
@ -0,0 +1,22 @@
|
|||
CC = gcc
|
||||
CFLAGS = -Wall -Wextra -pedantic-errors -std=c99 -Wno-format
|
||||
OUTFILE = cards
|
||||
|
||||
SRCDIR = src
|
||||
OBJDIR = obj
|
||||
INCDIR = src/include
|
||||
|
||||
SRC = $(wildcard $(SRCDIR)/*.c)
|
||||
INC = $(wildcard $(INCDIR)/*.h)
|
||||
_OBJ = $(SRC:.c=.o)
|
||||
OBJ = $(subst $(SRCDIR),$(OBJDIR),$(_OBJ))
|
||||
|
||||
$(OUTFILE): $(OBJ)
|
||||
$(CC) $(CFLAGS) -o $(OUTFILE) $(OBJ)
|
||||
|
||||
$(OBJDIR)/%.o: $(SRCDIR)/%.c $(INCDIR)/%.h
|
||||
$(CC) $(CFLAGS) -o $@ -c $<
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
-rm $(OUTFILE) $(OBJ)
|
BIN
cards
Executable file
BIN
cards
Executable file
Binary file not shown.
5
example.csv
Normal file
5
example.csv
Normal file
|
@ -0,0 +1,5 @@
|
|||
the,quick
|
||||
brown,fox
|
||||
jumped,over
|
||||
the,lazy
|
||||
dog,foobar
|
|
BIN
obj/card.o
Normal file
BIN
obj/card.o
Normal file
Binary file not shown.
BIN
obj/main.o
Normal file
BIN
obj/main.o
Normal file
Binary file not shown.
43
src/card.c
Normal file
43
src/card.c
Normal file
|
@ -0,0 +1,43 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "include/main.h"
|
||||
#include "include/card.h"
|
||||
|
||||
CARD new_card(char *o, char *r) {
|
||||
CARD c;
|
||||
c.obverse = o;
|
||||
c.reverse = r;
|
||||
return c;
|
||||
}
|
||||
|
||||
CARD* card_parse_csv(char *f, size_t *ct) {
|
||||
FILE *fp;
|
||||
char *l, *o, *r;
|
||||
const char *delims = ",\0";
|
||||
CARD *ca;
|
||||
|
||||
if ((fp = fopen(f, "r")) == NULL)
|
||||
die("failed to open file (null pointer)");
|
||||
|
||||
*ct = 0;
|
||||
|
||||
while ((l = fgetl(fp)) != NULL) {
|
||||
|
||||
if ((o = strtok(l, delims)) == NULL)
|
||||
die("improper csv format");
|
||||
if ((r = strtok(NULL, delims)) == NULL)
|
||||
die("improper csv format");
|
||||
|
||||
/* printf("o: `%s`, r: `%s`\n", o, r); */
|
||||
|
||||
ca = (CARD *)realloc((void *)ca, (sizeof(CARD) * ++(*ct)));
|
||||
ca[(*ct) - 1] = new_card(strdup_(o), strdup_(r));
|
||||
|
||||
free(l);
|
||||
l = NULL;
|
||||
}
|
||||
|
||||
return ca;
|
||||
}
|
13
src/include/card.h
Normal file
13
src/include/card.h
Normal file
|
@ -0,0 +1,13 @@
|
|||
#ifndef CARD_H
|
||||
#define CARD_H
|
||||
|
||||
typedef struct card_T {
|
||||
char *obverse;
|
||||
char *reverse;
|
||||
} CARD;
|
||||
|
||||
CARD new_card(char *, char *);
|
||||
|
||||
CARD* card_parse_csv(char *, size_t *);
|
||||
|
||||
#endif
|
8
src/include/main.h
Normal file
8
src/include/main.h
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef MAIN_H
|
||||
#define MAIN_H
|
||||
|
||||
void die(char *);
|
||||
char *fgetl(FILE *);
|
||||
char *strdup_(char *);
|
||||
|
||||
#endif
|
52
src/main.c
Normal file
52
src/main.c
Normal file
|
@ -0,0 +1,52 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "include/main.h"
|
||||
#include "include/card.h"
|
||||
|
||||
void die(char *s) {
|
||||
fprintf(stderr, "fatal: %s\n", s);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
char *fgetl(FILE *stream) {
|
||||
size_t s_sz = 1; // +1 for null byte
|
||||
char *s = NULL;
|
||||
char c;
|
||||
|
||||
while ((c = fgetc(stream)) != '\n') {
|
||||
if (c == EOF)
|
||||
return NULL;
|
||||
|
||||
s = (char *)realloc((void *)s, ++s_sz);
|
||||
s[s_sz - 2] = c;
|
||||
}
|
||||
|
||||
if (s != NULL)
|
||||
s[s_sz - 1] = '\0';
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
char *strdup_(char *s) {
|
||||
char *r;
|
||||
r = (char *)malloc(strlen(s) + 1);
|
||||
strcpy(r, s);
|
||||
return r;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
CARD *cards;
|
||||
size_t cards_ct;
|
||||
|
||||
if (argc < 1)
|
||||
die("filename required");
|
||||
|
||||
cards = card_parse_csv(argv[1], &cards_ct);
|
||||
|
||||
for (size_t i = 0; i < cards_ct; i++)
|
||||
printf("o: `%s`, r: `%s`\n", cards[i].obverse, cards[i].reverse);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue