|
| 1 | +/* cf.c |
| 2 | +
|
| 3 | + Some tests for CF. |
| 4 | +
|
| 5 | + Copyright (c) 2016 Miguel Garcia / FloppySoftware |
| 6 | +
|
| 7 | + This program is free software; you can redistribute it and/or modify it |
| 8 | + under the terms of the GNU General Public License as published by the |
| 9 | + Free Software Foundation; either version 3, or (at your option) any |
| 10 | + later version. |
| 11 | +
|
| 12 | + This program is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + GNU General Public License for more details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU General Public License |
| 18 | + along with this program; if not, write to the Free Software |
| 19 | + Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | +
|
| 21 | + Author's contact: |
| 22 | +
|
| 23 | + www.floppysoftware.es |
| 24 | + cpm-connections.blogspot.com |
| 25 | + floppysoftware@gmail.com |
| 26 | +
|
| 27 | + To compile with MESCC: |
| 28 | +
|
| 29 | + cc cf |
| 30 | + ccopt cf |
| 31 | + zsm cf |
| 32 | + hextocom cf |
| 33 | +
|
| 34 | + Usage: |
| 35 | +
|
| 36 | + cf |
| 37 | +
|
| 38 | + Revisions: |
| 39 | +
|
| 40 | + 06 Jul 2016 : Work begins. |
| 41 | +*/ |
| 42 | + |
| 43 | +/* Defines for MESCC libraries |
| 44 | + --------------------------- |
| 45 | + Leave only what you need. |
| 46 | +*/ |
| 47 | +//#define CC_STDIO // Support for stdin, stdout, stderr. |
| 48 | +//#define CC_REDIR // Support for command line redirection - needs CC_STDIO. |
| 49 | + |
| 50 | +//#define CC_CONIO_BIOS // Console I/O through BIOS instead of BDOS. |
| 51 | + |
| 52 | +//#define CC_FCX // Support for user number in filenames. |
| 53 | +//#define CC_FCX_DIR // Support for named user numbers - needs CC_FCX and DirToDrvUsr(). |
| 54 | + |
| 55 | +//#define CC_FILEIO_SMALL // Exclude fread(), fwrite() and fgets(). |
| 56 | + |
| 57 | +//#define CC_NO_MUL // Exclude support for MULTIPLICATION. |
| 58 | +//#define CC_NO_DIV // Exclude support for DIVISION and MODULUS. |
| 59 | +//#define CC_NO_SWITCH // Exclude support for SWITCH. |
| 60 | + |
| 61 | +#define CC_NO_ARGS // Exclude support for ARGC and ARGV. |
| 62 | + |
| 63 | +/* Standard MESCC library |
| 64 | + ---------------------- |
| 65 | + You must include this library; |
| 66 | + it contains some defs. and the runtime. |
| 67 | +*/ |
| 68 | +#include <mescc.h> |
| 69 | + |
| 70 | +/* Standard MESCC libraries |
| 71 | + ------------------------ |
| 72 | + Leave only what you need. |
| 73 | +*/ |
| 74 | +//#include <alloc.h> |
| 75 | +//#include <atexit.h> |
| 76 | +//#include <bsearch.h> |
| 77 | +//#include <clock.h> |
| 78 | +//#include <conio.h> |
| 79 | +//#include <cpm.h> |
| 80 | +//#include <ctype.h> |
| 81 | +//#include <fileio.h> |
| 82 | +//#include <fprintf.h> |
| 83 | +//#include <mem.h> |
| 84 | +#include <printf.h> |
| 85 | +//#include <qsort.h> |
| 86 | +//#include <rand.h> |
| 87 | +//#include <redir.h> |
| 88 | +//#include <setjmp.h> |
| 89 | +//#include <sprintf.h> |
| 90 | +//#include <string.h> |
| 91 | +//#include <xprintf.h> |
| 92 | +//#include <z80.h> |
| 93 | + |
| 94 | +/* Project libraries |
| 95 | + ----------------- |
| 96 | +*/ |
| 97 | +#include "cf.h" |
| 98 | +#include "cf_read.h" |
| 99 | +#include "cf_write.h" |
| 100 | +#include "cf_bool.h" |
| 101 | +#include "cf_int.h" |
| 102 | +#include "cf_uint.h" |
| 103 | +#include "cf_str.h" |
| 104 | + |
| 105 | +/* Globals |
| 106 | + ------- |
| 107 | +*/ |
| 108 | +CF *cf; |
| 109 | + |
| 110 | +/* Program entry |
| 111 | + ------------- |
| 112 | +*/ |
| 113 | +main() |
| 114 | +{ |
| 115 | + int k; |
| 116 | + |
| 117 | + printf("Creating CF\n\n"); |
| 118 | + |
| 119 | + if(!(cf = cf_create(6))) |
| 120 | + error("Can't create CF"); |
| 121 | + |
| 122 | + add_key("title", "That's cool!"); |
| 123 | + add_key("author", "Jim Brown"); |
| 124 | + |
| 125 | + // This should cause an error |
| 126 | + add_key("title", "This should cause an error: the key already exists"); |
| 127 | + |
| 128 | + add_key("year", "1969"); |
| 129 | + add_key("pages", "150"); |
| 130 | + add_key("summary", "\"This book, blah, blah, blah...\""); |
| 131 | + add_key("lent", "true"); |
| 132 | + |
| 133 | + // This should cause an error |
| 134 | + add_key("publisher", "This should cause an error: no more entries"); |
| 135 | + |
| 136 | + printf("\n"); |
| 137 | + pr_keys(cf); |
| 138 | + |
| 139 | + set_key("year", "1977"); |
| 140 | + |
| 141 | + // This should cause an error |
| 142 | + set_key("month", "This should cause an error: the key does not exists"); |
| 143 | + |
| 144 | + printf("\n"); |
| 145 | + pr_keys(cf); |
| 146 | + |
| 147 | + printf("Writting test.cf\n\n"); |
| 148 | + |
| 149 | + if(cf_write(cf, "test.cf")) |
| 150 | + error("Can't write test.cf"); |
| 151 | + |
| 152 | + printf("Destroying CF\n\n"); |
| 153 | + |
| 154 | + cf_destroy(cf); |
| 155 | + |
| 156 | + // ------------------------------------------- |
| 157 | + |
| 158 | + printf("Creating CF\n\n"); |
| 159 | + |
| 160 | + if(!(cf = cf_create(8))) |
| 161 | + error("Can't create CF"); |
| 162 | + |
| 163 | + printf("Reading test.cf into CF\n\n"); |
| 164 | + |
| 165 | + if(cf_read(cf, "test.cf")) |
| 166 | + error("Can't read test.cf"); |
| 167 | + |
| 168 | + pr_keys(cf); |
| 169 | + |
| 170 | + printf("Reading test_2.cf into CF\n\n"); |
| 171 | + |
| 172 | + if((k = cf_read(cf, "test_2.cf"))) { printf("!!%d!!\n", k); |
| 173 | + error("Can't read test_2.cf");} |
| 174 | + |
| 175 | + pr_keys(cf); |
| 176 | + |
| 177 | + printf("Title >> %s\n", cf_get_key(cf, "title")); |
| 178 | + printf("Author >> %s\n", cf_get_str(cf, "author", "unknown")); |
| 179 | + printf("Publisher >> %s\n", cf_get_str(cf, "publisher", "n/a")); |
| 180 | + printf("Year >> %d\n", cf_get_uint(cf, "year", 9999)); |
| 181 | + printf("Pages >> %d\n", cf_get_int(cf, "pages", 9999)); |
| 182 | + printf("Summary >> %s\n", cf_get_str(cf, "summary", "n/a")); |
| 183 | + printf("Lent >> %s\n", cf_get_bool(cf, "lent", 0) ? "Yes" : "No"); |
| 184 | + printf("To >> %s\n", cf_get_key(cf, "lent_to")); |
| 185 | + printf("Expires >> %s\n", cf_get_key(cf, "lend_expires")); |
| 186 | + |
| 187 | + printf("\n"); |
| 188 | + |
| 189 | + printf("Destroying CF\n\n"); |
| 190 | + |
| 191 | + cf_destroy(cf); |
| 192 | + |
| 193 | + // ------------------------------------------- |
| 194 | + |
| 195 | + printf("Done\n"); |
| 196 | +} |
| 197 | + |
| 198 | +add_key(key, value) |
| 199 | +char *key, *value; |
| 200 | +{ |
| 201 | + int result; |
| 202 | + |
| 203 | + result = cf_add_key(cf, key, value); |
| 204 | + |
| 205 | + printf("Add %s = %s%s\n", key, value, result ? " --> ERROR" : ""); |
| 206 | +} |
| 207 | + |
| 208 | +set_key(key, value) |
| 209 | +char *key, *value; |
| 210 | +{ |
| 211 | + int result; |
| 212 | + |
| 213 | + result = cf_set_key(cf, key, value); |
| 214 | + |
| 215 | + printf("Set %s = %s%s\n", key, value, result ? " --> ERROR" : ""); |
| 216 | +} |
| 217 | + |
| 218 | +pr_keys(cf) |
| 219 | +CF *cf; |
| 220 | +{ |
| 221 | + cf_pr_keys(cf); |
| 222 | + |
| 223 | + printf("\n"); |
| 224 | +} |
| 225 | + |
| 226 | +error(msg) |
| 227 | +char *msg; |
| 228 | +{ |
| 229 | + printf("ERROR: %s\n", msg); |
| 230 | + |
| 231 | + exit(-1); |
| 232 | +} |
| 233 | + |
0 commit comments