Atlas - TABLE.C

Home / systems / IBM-PC / DataWorks / src Lines: 3 | Size: 6023 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* C89 Standard only */ 2 3#include <stdio.h> 4#include <string.h> 5#include <stdlib.h> 6 7#include "DW.H" 8 9static int dw_alloc_cell(char **out_cell) 10{ 11 *out_cell = (char *)malloc((size_t)DW_MAX_CELL); 12 if (*out_cell == NULL) { 13 return 0; 14 } 15 (*out_cell)[0] = '\0'; 16 return 1; 17} 18 19static void dw_fill_header_names(DWTable *table, int start_col) 20{ 21 int c; 22 char temp[32]; 23 24 for (c = start_col; c < table->cols; ++c) { 25 if (table->cells[0][c] != NULL && table->cells[0][c][0] == '\0') { 26 sprintf(temp, "COL_%d", c); 27 dw_copy_cell(table->cells[0][c], temp); 28 } 29 } 30} 31 32static int dw_ensure_size(DWTable *table, int rows, int cols) 33{ 34 int r; 35 int c; 36 int old_rows; 37 char ***new_rows; 38 char **new_row; 39 40 if (rows > DW_MAX_ROWS || cols > DW_MAX_COLS || rows < 1 || cols < 1) { 41 return 0; 42 } 43 44 old_rows = table->rows; 45 46 if (rows > table->rows) { 47 new_rows = (char ***)realloc(table->cells, (size_t)rows * sizeof(char **)); 48 if (new_rows == NULL) { 49 return 0; 50 } 51 table->cells = new_rows; 52 for (r = table->rows; r < rows; ++r) { 53 table->cells[r] = NULL; 54 } 55 table->rows = rows; 56 } 57 58 if (table->cols > 0 && table->rows > old_rows) { 59 for (r = old_rows; r < table->rows; ++r) { 60 table->cells[r] = (char **)malloc((size_t)table->cols * sizeof(char *)); 61 if (table->cells[r] == NULL) { 62 return 0; 63 } 64 for (c = 0; c < table->cols; ++c) { 65 table->cells[r][c] = NULL; 66 if (!dw_alloc_cell(&table->cells[r][c])) { 67 return 0; 68 } 69 } 70 } 71 } 72 73 if (cols > table->cols) { 74 for (r = 0; r < table->rows; ++r) { 75 new_row = (char **)realloc(table->cells[r], (size_t)cols * sizeof(char *)); 76 if (new_row == NULL) { 77 return 0; 78 } 79 table->cells[r] = new_row; 80 for (c = table->cols; c < cols; ++c) { 81 table->cells[r][c] = NULL; 82 if (!dw_alloc_cell(&table->cells[r][c])) { 83 return 0; 84 } 85 } 86 } 87 table->cols = cols; 88 dw_fill_header_names(table, 0); 89 } 90 91 return 1; 92} 93 94void dw_init_table(DWTable *table) 95{ 96 table->rows = 0; 97 table->cols = 0; 98 table->cells = NULL; 99 100 if (!dw_ensure_size(table, 1, 1)) { 101 return; 102 } 103 104 dw_copy_cell(table->cells[0][0], "COL_0"); 105} 106 107void dw_free_table(DWTable *table) 108{ 109 int r; 110 int c; 111 112 if (table->cells != NULL) { 113 for (r = 0; r < table->rows; ++r) { 114 if (table->cells[r] != NULL) { 115 for (c = 0; c < table->cols; ++c) { 116 free(table->cells[r][c]); 117 } 118 free(table->cells[r]); 119 } 120 } 121 free(table->cells); 122 } 123 124 table->rows = 0; 125 table->cols = 0; 126 table->cells = NULL; 127} 128 129int dw_load_file(const char *path, DWTable *table) 130{ 131 FILE *fp; 132 char line[DW_MAX_LINE]; 133 int row; 134 int col; 135 int pos; 136 int i; 137 char delim; 138 char cell[DW_MAX_CELL]; 139 140 dw_init_table(table); 141 fp = fopen(path, "rb"); 142 if (fp == NULL) { 143 return 1; 144 } 145 146 row = 0; 147 while (fgets(line, sizeof(line), fp) != NULL) { 148 dw_trim(line); 149 delim = (strchr(line, DW_SEP) != NULL) ? (char)DW_SEP : ','; 150 151 col = 0; 152 pos = 0; 153 cell[0] = '\0'; 154 155 for (i = 0;; ++i) { 156 if (line[i] == delim || line[i] == '\0') { 157 cell[pos] = '\0'; 158 dw_trim(cell); 159 160 if (!dw_ensure_size(table, row + 1, col + 1)) { 161 fclose(fp); 162 dw_free_table(table); 163 return 0; 164 } 165 dw_copy_cell(table->cells[row][col], cell); 166 167 ++col; 168 pos = 0; 169 cell[0] = '\0'; 170 171 if (line[i] == '\0' || col >= DW_MAX_COLS) { 172 break; 173 } 174 } else if (pos < DW_MAX_CELL - 1) { 175 cell[pos++] = line[i]; 176 } 177 } 178 179 ++row; 180 if (row >= DW_MAX_ROWS) { 181 break; 182 } 183 } 184 185 if (row > 0) { 186 table->rows = row; 187 } 188 dw_fill_header_names(table, 0); 189 fclose(fp); 190 return 1; 191} 192 193int dw_save_file(const char *path, const DWTable *table) 194{ 195 FILE *fp; 196 int r; 197 int c; 198 199 fp = fopen(path, "wb"); 200 if (fp == NULL) { 201 return 0; 202 } 203 204 for (r = 0; r < table->rows; ++r) { 205 for (c = 0; c < table->cols; ++c) { 206 if (table->cells[r][c] != NULL) { 207 fputs(table->cells[r][c], fp); 208 } 209 if (c < table->cols - 1) { 210 fputc(DW_SEP, fp); 211 } 212 } 213 fputc('\n', fp); 214 } 215 216 fclose(fp); 217 return 1; 218} 219 220void dw_print_table(const DWTable *table) 221{ 222 int r; 223 int c; 224 int i; 225 int len; 226 int pad; 227 int widths[DW_MAX_COLS]; 228 229 for (c = 0; c < table->cols; ++c) { 230 widths[c] = 0; 231 } 232 233 for (r = 0; r < table->rows; ++r) { 234 for (c = 0; c < table->cols; ++c) { 235 len = 0; 236 if (table->cells[r][c] != NULL) { 237 len = (int)strlen(table->cells[r][c]); 238 } 239 if (len > widths[c]) { 240 widths[c] = len; 241 } 242 } 243 } 244 245 for (r = 0; r < table->rows; ++r) { 246 for (c = 0; c < table->cols; ++c) { 247 if (table->cells[r][c] == NULL || table->cells[r][c][0] == '\0') { 248 printf("~"); 249 } else { 250 printf("%s", table->cells[r][c]); 251 } 252 if (c < table->cols - 1) { 253 len = 0; 254 if (table->cells[r][c] != NULL) { 255 len = (int)strlen(table->cells[r][c]); 256 } 257 if (len == 0) { 258 len = 1; 259 } 260 pad = (widths[c] - len) + 4; 261 for (i = 0; i < pad; ++i) { 262 putchar(' '); 263 } 264 } 265 } 266 printf("\n"); 267 } 268} 269 270int dw_insert(DWTable *table, const char *value, int data_row, int col) 271{ 272 int real_row; 273 274 real_row = data_row + 1; 275 if (!dw_ensure_size(table, real_row + 1, col + 1)) { 276 return 0; 277 } 278 dw_copy_cell(table->cells[real_row][col], value); 279 return 1; 280} 281 282int dw_del_col(DWTable *table, int col_index) 283{ 284 int r; 285 int c; 286 287 if (col_index < 0 || col_index >= table->cols || table->cols <= 0) { 288 return 0; 289 } 290 291 for (r = 0; r < table->rows; ++r) { 292 for (c = col_index; c < table->cols - 1; ++c) { 293 dw_copy_cell(table->cells[r][c], table->cells[r][c + 1]); 294 } 295 if (table->cells[r][table->cols - 1] != NULL) { 296 table->cells[r][table->cols - 1][0] = '\0'; 297 } 298 } 299 table->cols -= 1; 300 return 1; 301} 302 303int dw_set_col_name(DWTable *table, int col_index, const char *name) 304{ 305 if (col_index < 0 || col_index >= table->cols) { 306 return 0; 307 } 308 dw_copy_cell(table->cells[0][col_index], name); 309 return 1; 310} 311
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.