From aafd29405b1d81fcdaf60611c6cfab2915cea337 Mon Sep 17 00:00:00 2001 From: Nathan Vance Date: Tue, 27 Jan 2015 16:12:39 -0500 Subject: First commit --- 7w | Bin 0 -> 31912 bytes 7w.h | 33 +++++ Makefile | 4 + README | 1 + cards.c | 461 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ io.c | 68 ++++++++++ main.c | 22 +++ util.c | 118 ++++++++++++++++ 8 files changed, 707 insertions(+) create mode 100755 7w create mode 100644 7w.h create mode 100644 Makefile create mode 100644 README create mode 100644 cards.c create mode 100644 io.c create mode 100644 main.c create mode 100644 util.c diff --git a/7w b/7w new file mode 100755 index 0000000..b13ae5e Binary files /dev/null and b/7w differ diff --git a/7w.h b/7w.h new file mode 100644 index 0000000..76df6a8 --- /dev/null +++ b/7w.h @@ -0,0 +1,33 @@ +#define NUMRESOURCES 8 +#define WOOD 0 +#define COLORWOOD 32 +#define STONE 1 +#define COLORSTONE 0 +#define CLAY 2 +#define COLORCLAY +#define ORE 3 +#define CLOTH 4 +#define GLASS 5 +#define PAPER 6 +#define GOLD 7 + +#define NUMPRODUCTS 13 //includes resources +#define COMPASS 8 +#define GEAR 9 +#define TABLET 10 +#define SHIELD 11 +#define VP 12 + +#define RESOURCE 0 +#define INDUSTRY 1 +#define STRUCTURE 2 +#define COMMERCIAL 4 +#define MILITARY 5 +#define SCIENTIFIC 6 +#define GUILD 7 + +#define LEFT 0 +#define RIGHT 1 +#define UP 2 +#define DOWN 3 +#define ENTER 4 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f81a424 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +7wbin: + gcc -ggdb -o 7w *.c *.h -lncurses -lm +clean: + rm -f 7w diff --git a/README b/README new file mode 100644 index 0000000..d401f16 --- /dev/null +++ b/README @@ -0,0 +1 @@ +# 7w diff --git a/cards.c b/cards.c new file mode 100644 index 0000000..113e7f1 --- /dev/null +++ b/cards.c @@ -0,0 +1,461 @@ +#include "7w.h" + +char* get_chararray(int size); +int* get_intarray(int size); + +//era, card#, resource cost / resource production / coupons / couponed by / card name +#define CARDSPERERA 30 +static int cards[3][CARDSPERERA][60]; + +void cards_setcost(int era, int card, int res, int num) +{ + cards[era][card][res+1] = num; +} + +int* cards_getcost(int era, int card) +{ + int *ret = get_intarray(NUMRESOURCES); + int i; + for(i = 0; i < NUMRESOURCES; i++) ret[i] = cards[era][card][i+1]; + return ret; +} + +void cards_setproduction(int era, int card, int res, int num) +{ + cards[era][card][res+1+NUMRESOURCES] = num; +} + +int* cards_getproduction(int era, int card) +{ + int *ret = get_intarray(NUMPRODUCTS); + int i; + for(i = 0; i < NUMPRODUCTS; i++) + ret[i] = cards[era][card][i+1+NUMRESOURCES]; + return ret; +} + +void cards_settype(int era, int card, int type) +{ + cards[era][card][0] = type; +} + +int cards_gettype(int era, int card) +{ + return cards[era][card][0]; +} + +void cards_setcoupons(int era, int card, int era1, int card1, int era2, int card2) +{ + int i = 1+NUMRESOURCES+NUMPRODUCTS; + cards[era][card][i++] = era1; + cards[era][card][i++] = card1; + cards[era][card][i++] = era2; + cards[era][card][i++] = card2; +} + +void cards_updatecoupons() +{ + int i, j, k; + for(i = 0; i <= 1; i++) { + for(j = 0; j < CARDSPERERA; j++) { + for(k = 1+NUMRESOURCES+NUMPRODUCTS; k <= 3+NUMRESOURCES+NUMPRODUCTS; k += 2) { + if(cards[i][j][k] != 0) { + cards[cards[i][j][k]][cards[i][j][k+1]][k+4] = i; + cards[cards[i][j][k]][cards[i][j][k+1]][k+5] = j; + } + } + } + } +} + +void cards_setname(int era, int card, char *name, int type) +{ + int i; + for(i = 0; (cards[era][card][i+1+NUMRESOURCES+NUMPRODUCTS+4] = name[i]) != '\0'; i++); + cards_settype(era, card, type); +} + +char* cards_getname(int era, int card) +{ + char *ret = get_chararray(26); + int i; + for(i = 0; (ret[i] = cards[era][card][i+1+NUMRESOURCES+NUMPRODUCTS+4]) != '\0'; i++); + return ret; +} + +void cards_init() +{ + //Era 1 + cards_setproduction(0, 0, WOOD, 1); + cards_setname(0, 0, "Lumber Yard", RESOURCE); + + cards_setproduction(0, 1, STONE, 1); + cards_setname(0, 1, "Stone Pit", RESOURCE); + + cards_setproduction(0, 2, CLAY, 1); + cards_setname(0, 2, "Clay Pool", RESOURCE); + + cards_setproduction(0, 3, ORE, 1); + cards_setname(0, 3, "Ore Vein", RESOURCE); + + cards_setcost(0, 4, GOLD, 1); + cards_setproduction(0, 4, WOOD, 1); + cards_setproduction(0, 4, CLAY, 1); + cards_setname(0, 4, "Tree Farm", RESOURCE); + + cards_setcost(0, 5, GOLD, 1); + cards_setproduction(0, 5, STONE, 1); + cards_setproduction(0, 5, CLAY, 1); + cards_setname(0, 5, "Excavation", RESOURCE); + + cards_setcost(0, 6, GOLD, 1); + cards_setproduction(0, 6, CLAY, 1); + cards_setproduction(0, 6, ORE, 1); + cards_setname(0, 6, "Clay Pit", RESOURCE); + + cards_setcost(0, 7, GOLD, 1); + cards_setproduction(0, 7, STONE, 1); + cards_setproduction(0, 7, WOOD, 1); + cards_setname(0, 7, "Timber Yard", RESOURCE); + + cards_setcost(0, 8, GOLD, 1); + cards_setproduction(0, 8, WOOD, 1); + cards_setproduction(0, 8, ORE, 1); + cards_setname(0, 8, "Forest Cave", RESOURCE); + + cards_setcost(0, 9, GOLD, 1); + cards_setproduction(0, 9, ORE, 1); + cards_setproduction(0, 9, STONE, 1); + cards_setname(0, 9, "Mine", RESOURCE); + + cards_setproduction(0, 10, CLOTH, 1); + cards_setname(0, 10, "Loom", INDUSTRY); + + cards_setproduction(0, 11, GLASS, 1); + cards_setname(0, 11, "Glassworks", INDUSTRY); + + cards_setproduction(0, 12, PAPER, 1); + cards_setname(0, 12, "Press", INDUSTRY); + + cards_setproduction(0, 13, VP, 3); + cards_setname(0, 13, "Pawnshop", STRUCTURE); + + cards_setcost(0, 14, STONE, 1); + cards_setproduction(0, 14, VP, 3); + cards_setcoupons(0, 14, 1, 7, 0, 0); + cards_setname(0, 14, "Baths", STRUCTURE); + + cards_setproduction(0, 15, VP, 2); + cards_setcoupons(0, 15, 1, 8, 0, 0); + cards_setname(0, 15, "Altar", STRUCTURE); + + cards_setproduction(0, 16, VP, 2); + cards_setcoupons(0, 16, 1, 9, 0, 0); + cards_setname(0, 16, "Theater", STRUCTURE); + + cards_setproduction(0, 17, GOLD, 5); + cards_setname(0, 17, "Tavern", COMMERCIAL); + + cards_setcoupons(0, 18, 1, 11, 0, 0); + cards_setname(0, 18, "East Trading Post", COMMERCIAL); + + cards_setcoupons(0, 19, 1, 11, 0, 0); + cards_setname(0, 19, "West Trading Post", COMMERCIAL); + + cards_setcoupons(0, 20, 1, 12, 0, 0); + cards_setname(0, 20, "Marketplace", COMMERCIAL); + + cards_setcost(0, 21, WOOD, 1); + cards_setproduction(0, 21, SHIELD, 1); + cards_setname(0, 21, "Stockade", MILITARY); + + cards_setcost(0, 22, ORE, 1); + cards_setproduction(0, 22, SHIELD, 1); + cards_setname(0, 22, "Barracks", MILITARY); + + cards_setcost(0, 23, CLAY, 1); + cards_setproduction(0, 23, SHIELD, 1); + cards_setname(0, 23, "Guard Tower", MILITARY); + + cards_setcost(0, 24, CLOTH, 1); + cards_setproduction(0, 24, COMPASS, 1); + cards_setcoupons(0, 24, 1, 17, 1, 19); + cards_setname(0, 24, "Apothecary", SCIENTIFIC); + + cards_setcost(0, 25, GLASS, 1); + cards_setproduction(0, 25, GEAR, 1); + cards_setcoupons(0, 25, 1, 20, 1, 18); + cards_setname(0, 25, "Workshop", SCIENTIFIC); + + cards_setcost(0, 26, PAPER, 1); + cards_setproduction(0, 26, TABLET, 1); + cards_setcoupons(0, 26, 1, 10, 1, 21); + cards_setname(0, 26, "Scriptorium", SCIENTIFIC); + + //Era 2 + cards_setcost(1, 0, GOLD, 1); + cards_setproduction(1, 0, WOOD, 2); + cards_setname(1, 0, "Sawmill", RESOURCE); + + cards_setcost(1, 1, GOLD, 1); + cards_setproduction(1, 1, STONE, 2); + cards_setname(1, 1, "Quarry", RESOURCE); + + cards_setcost(1, 2, GOLD, 1); + cards_setproduction(1, 2, CLAY, 2); + cards_setname(1, 2, "Brickyard", RESOURCE); + + cards_setcost(1, 3, GOLD, 1); + cards_setproduction(1, 3, ORE, 2); + cards_setname(1, 3, "Foundry", RESOURCE); + + cards_setproduction(1, 4, CLOTH, 1); + cards_setname(1, 4, "Loom", INDUSTRY); + + cards_setproduction(1, 5, GLASS, 1); + cards_setname(1, 5, "Glassworks", INDUSTRY); + + cards_setproduction(1, 6, PAPER, 1); + cards_setname(1, 6, "Press", INDUSTRY); + + cards_setcost(1, 7, STONE, 3); + cards_setproduction(1, 7, VP, 5); + cards_setname(1, 7, "Aqueduct", STRUCTURE); + + cards_setcost(1, 8, WOOD, 1); + cards_setcost(1, 8, CLAY, 1); + cards_setcost(1, 8, GLASS, 1); + cards_setproduction(1, 8, VP, 3); + cards_setcoupons(1, 8, 2, 0, 0, 0); + cards_setname(1, 8, "Temple", STRUCTURE); + + cards_setcost(1, 9, ORE, 2); + cards_setcost(1, 9, WOOD, 1); + cards_setproduction(1, 9, VP, 4); + cards_setcoupons(1, 9, 2, 1, 0, 0); + cards_setname(1, 9, "Statue", STRUCTURE); + + cards_setcost(1, 10, CLAY, 2); + cards_setcost(1, 10, CLOTH, 1); + cards_setproduction(1, 10, VP, 4); + cards_setname(1, 10, "Courthouse", STRUCTURE); + + cards_setcost(1, 11, CLAY, 2); + cards_setproduction(1, 11, CLOTH, 1); + cards_setproduction(1, 11, GLASS, 1); + cards_setproduction(1, 11, PAPER, 1); + cards_setcoupons(1, 11, 2, 5, 0, 0); + cards_setname(1, 11, "Forum", COMMERCIAL); + + cards_setcost(1, 12, WOOD, 2); + cards_setproduction(1, 12, CLAY, 1); + cards_setproduction(1, 12, STONE, 1); + cards_setproduction(1, 12, ORE, 1); + cards_setproduction(1, 12, WOOD, 1); + cards_setcoupons(1, 12, 2, 6, 0, 0); + cards_setname(1, 12, "Caravansery", COMMERCIAL); + + cards_setname(1, 13, "Vineyard", COMMERCIAL); + + cards_setname(1, 14, "Bazar", COMMERCIAL); + + cards_setcost(1, 15, STONE, 3); + cards_setproduction(1, 15, SHIELD, 2); + cards_setcoupons(1, 15, 2, 9, 0, 0); + cards_setname(1, 15, "Walls", MILITARY); + + cards_setcost(1, 16, ORE, 2); + cards_setcost(1, 16, WOOD, 1); + cards_setproduction(1, 16, SHIELD, 2); + cards_setcoupons(1, 16, 2, 10, 0, 0); + cards_setname(1, 16, "Training Ground", MILITARY); + + cards_setcost(1, 17, ORE, 1); + cards_setcost(1, 17, CLAY, 1); + cards_setcost(1, 17, WOOD, 1); + cards_setproduction(1, 17, SHIELD, 2); + cards_setname(1, 17, "Stables", MILITARY); + + cards_setcost(1, 18, ORE, 1); + cards_setcost(1, 18, WOOD, 2); + cards_setproduction(1, 18, SHIELD, 2); + cards_setname(1, 18, "Archery Range", MILITARY); + + cards_setcost(1, 19, GLASS, 1); + cards_setcost(1, 19, ORE, 2); + cards_setproduction(1, 19, COMPASS, 1); + cards_setcoupons(1, 19, 2, 13, 2, 8); + cards_setname(1, 19, "Dispensary", SCIENTIFIC); + + cards_setcost(1, 20, PAPER, 1); + cards_setcost(1, 20, CLAY, 2); + cards_setproduction(1, 20, GEAR, 1); + cards_setcoupons(1, 20, 2, 14, 2, 12); + cards_setname(1, 20, "Laboratory", SCIENTIFIC); + + cards_setcost(1, 21, CLOTH, 1); + cards_setcost(1, 21, STONE, 2); + cards_setproduction(1, 21, TABLET, 1); + cards_setcoupons(1, 21, 2, 4, 2, 15); + cards_setname(1, 21, "Library", SCIENTIFIC); + + cards_setcost(1, 22, PAPER, 1); + cards_setcost(1, 22, WOOD, 1); + cards_setproduction(1, 22, TABLET, 1); + cards_setcoupons(1, 22, 2, 16, 2, 17); + cards_setname(1, 22, "School", SCIENTIFIC); + + //Era 3 + cards_setcost(2, 0, CLAY, 2); + cards_setcost(2, 0, ORE, 1); + cards_setcost(2, 0, PAPER, 1); + cards_setcost(2, 0, CLOTH, 1); + cards_setcost(2, 0, GLASS, 1); + cards_setproduction(2, 0, VP, 7); + cards_setname(2, 0, "Pantheon", STRUCTURE); + + cards_setcost(2, 1, CLAY, 2); + cards_setcost(2, 1, WOOD, 1); + cards_setproduction(2, 1, VP, 5); + cards_setname(2, 1, "Gardens", STRUCTURE); + + cards_setcost(2, 2, STONE, 2); + cards_setcost(2, 2, ORE, 1); + cards_setcost(2, 2, GLASS, 1); + cards_setproduction(2, 2, VP, 6); + cards_setname(2, 2, "Town Hall", STRUCTURE); + + cards_setcost(2, 3, WOOD, 1); + cards_setcost(2, 3, ORE, 1); + cards_setcost(2, 3, CLAY, 1); + cards_setcost(2, 3, STONE, 1); + cards_setcost(2, 3, GLASS, 1); + cards_setcost(2, 3, PAPER, 1); + cards_setcost(2, 3, CLOTH, 1); + cards_setproduction(2, 3, VP, 8); + cards_setname(2, 3, "Palace", STRUCTURE); + + cards_setcost(2, 4, WOOD, 2); + cards_setcost(2, 4, ORE, 1); + cards_setcost(2, 4, STONE, 1); + cards_setproduction(2, 4, VP, 6); + cards_setname(2, 4, "Senate", STRUCTURE); + + cards_setcost(2, 5, ORE, 1); + cards_setcost(2, 5, WOOD, 1); + cards_setcost(2, 5, CLOTH, 1); + cards_setname(2, 5, "Haven", COMMERCIAL); + + cards_setcost(2, 6, STONE, 1); + cards_setcost(2, 6, GLASS, 1); + cards_setname(2, 6, "Lighthouse", COMMERCIAL); + + cards_setcost(2, 7, CLAY, 2); + cards_setcost(2, 7, PAPER, 1); + cards_setname(2, 7, "Chamber Of Commerce", COMMERCIAL); + + cards_setcost(2, 8, STONE, 2); + cards_setcost(2, 8, ORE, 1); + cards_setname(2, 8, "Arena", COMMERCIAL); + + cards_setcost(2, 9, ORE, 3); + cards_setcost(2, 9, STONE, 1); + cards_setproduction(2, 9, SHIELD, 3); + cards_setname(2, 9, "Fortifications", MILITARY); + + cards_setcost(2, 10, STONE, 3); + cards_setcost(2, 10, ORE, 1); + cards_setproduction(2, 10, SHIELD, 3); + cards_setname(2, 10, "Circus", MILITARY); + + cards_setcost(2, 11, ORE, 1); + cards_setcost(2, 11, WOOD, 2); + cards_setcost(2, 11, CLOTH, 1); + cards_setproduction(2, 11, SHIELD, 3); + cards_setname(2, 11, "Arsenal", MILITARY); + + cards_setcost(2, 12, CLAY, 3); + cards_setcost(2, 12, WOOD, 1); + cards_setproduction(2, 12, SHIELD, 3); + cards_setname(2, 12, "Siege Workshop", MILITARY); + + cards_setcost(2, 13, CLAY, 2); + cards_setcost(2, 13, CLOTH, 1); + cards_setcost(2, 13, PAPER, 1); + cards_setproduction(2, 13, COMPASS, 1); + cards_setname(2, 13, "Lodge", SCIENTIFIC); + + cards_setcost(2, 14, ORE, 2); + cards_setcost(2, 14, GLASS, 1); + cards_setcost(2, 14, CLOTH, 1); + cards_setproduction(2, 14, GEAR, 1); + cards_setname(2, 14, "Observatory", SCIENTIFIC); + + cards_setcost(2, 15, PAPER, 1); + cards_setcost(2, 15, GLASS, 1); + cards_setcost(2, 15, WOOD, 2); + cards_setproduction(2, 15, TABLET, 1); + cards_setname(2, 15, "University", SCIENTIFIC); + + cards_setcost(2, 16, GLASS, 1); + cards_setcost(2, 16, STONE, 3); + cards_setproduction(2, 16, COMPASS, 1); + cards_setname(2, 16, "Academy", SCIENTIFIC); + + cards_setcost(2, 17, WOOD, 1); + cards_setcost(2, 17, PAPER, 1); + cards_setcost(2, 17, CLOTH, 1); + cards_setproduction(2, 17, GEAR, 1); + cards_setname(2, 17, "Study", SCIENTIFIC); + + cards_setcost(2, 18, ORE, 2); + cards_setcost(2, 18, CLAY, 1); + cards_setcost(2, 18, STONE, 1); + cards_setcost(2, 18, WOOD, 1); + cards_setname(2, 18, "Workers Guild", GUILD); + + cards_setcost(2, 19, ORE, 2); + cards_setcost(2, 19, STONE, 2); + cards_setname(2, 19, "Craftmens Guild", GUILD); + + cards_setcost(2, 20, CLOTH, 1); + cards_setcost(2, 20, PAPER, 1); + cards_setcost(2, 20, GLASS, 1); + cards_setname(2, 20, "Traders Guild", GUILD); + + cards_setcost(2, 21, CLAY, 3); + cards_setcost(2, 21, CLOTH, 1); + cards_setcost(2, 21, PAPER, 1); + cards_setname(2, 21, "Philosophers Guild", GUILD); + + cards_setcost(2, 22, CLAY, 3); + cards_setcost(2, 22, GLASS, 1); + cards_setname(2, 22, "Spy Guild", GUILD); + + cards_setcost(2, 23, ORE, 2); + cards_setcost(2, 23, STONE, 1); + cards_setcost(2, 23, CLOTH, 1); + cards_setname(2, 23, "Strategy Guild", GUILD); + + cards_setcost(2, 24, WOOD, 3); + cards_setcost(2, 24, PAPER, 1); + cards_setcost(2, 24, GLASS, 1); + cards_setname(2, 24, "Shipowners Guild", GUILD); + + cards_setcost(2, 25, WOOD, 2); + cards_setcost(2, 25, ORE, 2); + cards_setcost(2, 25, CLOTH, 1); + cards_setname(2, 25, "Scientists Guild", GUILD); + + cards_setcost(2, 26, WOOD, 3); + cards_setcost(2, 26, STONE, 1); + cards_setcost(2, 26, CLOTH, 1); + cards_setname(2, 26, "Magistrates Guild", GUILD); + + cards_setcost(2, 27, STONE, 2); + cards_setcost(2, 27, CLAY, 2); + cards_setcost(2, 27, GLASS, 1); + cards_setname(2, 27, "Builders Guild", GUILD); + + cards_updatecoupons(); +} diff --git a/io.c b/io.c new file mode 100644 index 0000000..0edd6ae --- /dev/null +++ b/io.c @@ -0,0 +1,68 @@ +#include +#include +#include "7w.h" + +char* cards_getname(int era, int card); +int* cards_getcost(int era, int card); +int cards_gettype(int era, int card); +int* cards_getproduction(int era, int card); + +void io_init() +{ + initscr(); + clear(); + noecho(); + cbreak(); + curs_set(0); + keypad(stdscr, TRUE); + start_color(); + use_default_colors(); + +//This may seem slightly conveluted, but it's the curses way. + init_pair(31, COLOR_RED, -1); + init_pair(32, COLOR_GREEN, -1); + init_pair(33, COLOR_YELLOW, -1); + init_pair(34, COLOR_BLUE, -1); + init_pair(35, COLOR_MAGENTA, -1); + init_pair(36, COLOR_CYAN, -1); + init_pair(37, COLOR_WHITE, -1); +} + +int io_getkey() +{ +int c; + switch(c = getch()) { + case KEY_LEFT: + return LEFT; + break; + case KEY_RIGHT: + return RIGHT; + break; + case KEY_UP: + return UP; + break; + case KEY_DOWN: + return DOWN; + break; + case KEY_ENTER: + case '\n': + case '\r': + return ENTER; + break; + default: + return c; + break; + } +} + +void io_printcard(int x, int y, int era, int card) +{ + mvprintw(y++, x, " ______________________"); + mvprintw(y++, x, "| %20s |", cards_getname(era, card)); + mvprintw(y++, x, "|Cost: Produces: |"); + int *costs = cards_getcost(era, card); + int i; + for(i = 0; i < NUMRESOURCES; i++) + + mvprintw(y++, x, "|______________________|"); +} diff --git a/main.c b/main.c new file mode 100644 index 0000000..9d17a80 --- /dev/null +++ b/main.c @@ -0,0 +1,22 @@ +#include "7w.h" +#include + +void io_init(); +void cards_init(); +void io_printcard(int x, int y, int era, int card); + +void halt() +{ + endwin(); + exit(0); +} + +main() +{ + io_init(); + cards_init(); + io_printcard(0, 0, 0, 0); + io_printcard(0, 10, 0, 1); + io_getkey(); + halt(); +} diff --git a/util.c b/util.c new file mode 100644 index 0000000..829e7d1 --- /dev/null +++ b/util.c @@ -0,0 +1,118 @@ +#include +#include +#include +#include + +static void replace(char a[], char b[], int x, int y); + +#define POOLSIZE 10000 +static int ipool[POOLSIZE]; +static int *ipoolp = ipool; +int* get_intarray(int size) +{ + if(ipoolp + size > ipool + POOLSIZE) ipoolp = ipool; + int* ret = ipoolp; + ipoolp += size; + return ret; +} + +static char cpool[POOLSIZE]; +static char *cpoolp = cpool; +char* get_chararray(int size) +{ + if(cpoolp + size > cpool + POOLSIZE) cpoolp = cpool; + char* ret = cpoolp; + cpoolp += size; + return ret; +} + +/* In string a[], replace nth occerence of x[] with y[], + or all occerences if n == 0. Note '.' is wild and + the last . replaced will be returned. */ +char util_strreplace(char a[], char x[], char y[], int n) { + int replaceAll = ! n; + int i, j, lengthY; + char dot = '\0'; + for(lengthY = 0; y[lengthY] != '\0'; lengthY++); + for(i = 0; a[i] != '\0'; i++) { + for(j = 0;; j++) { + if(a[i+j] != x[j] && x[j] != '\0' && x[j] != '.') // '.' is wild + break; + if (x[j] == '.') dot = a[i+j]; + if (x[j] == '\0') { //it's a match + n--; + if(replaceAll) { //replace it + replace(a, y, i, j); + i += lengthY - 1; + } + else if(n == 0) { //replace it + replace(a, y, i, j); + return dot; + } + break; + } + if(a[i+j] == '\0') //end of string + return dot; + } + } +} + +/* In string a[], replace characters x - y with b[] */ +static void replace(char a[], char b[], int x, int y) +{ + int i, j; + for(i = 0; a[i] != '\0'; i++); + char save[i-y+1]; + for(i = x+y, j = 0; (save[j] = a[i]) != '\0'; i++, j++); + for(i = x, j = 0; (a[i] = b[j]) != '\0'; i++, j++); + for(i = i, j = 0; (a[i] = save[j]) != '\0'; i++, j++); +} + +/* Concatinates a and b and returns a different + string. Only use when returned string can be recycled. */ +char* cat(char a[], char b[]) { + int len = strlen(a) + strlen(b) + 1; +/* if(len > RET) return "error"; + if(len + retindex > RET) retindex = 0; + char* p = ret + retindex; +*/ + char *p = get_chararray(len); + int i, j; + i = j = 0; + while((p[i++] = a[j++]) != '\0'); + i--; + j = 0; + while((p[i++] = b[j++]) != '\0'); + return p; +} + +char* itoa(int i) +{ + char *num = get_chararray(5); + int j; + for(j = 0; j < 5; num[j++] = ' '); + num[3] = '0'; + int o = 3; + int negative = 0; + if(i < 0) { + negative = 1; + i *= -1; + } + while(o >= 0 && i > 0) { + num[o--] = i % 10 + '0'; + i /= 10; + } + if(o >= 0 && negative) num[o] = '-'; + num[4] = '\0'; + for(i = 0; num[i] == ' '; i++); + return &num[i]; +} + +//sleep in tenths of a second +int bettersleep(int ds) +{ + struct timespec tim, tim2; + tim.tv_sec = ds/10; + tim.tv_nsec = (ds%10)*100000000L; + return nanosleep(&tim , &tim2); +} -- cgit