static void set_style(struct game_state *s, const char *name, const char *def) { char *n = my_strlower(strdup(name)); /* For case-insensitivity */ const char *value = ini_get(game_ini, s->name, n, NULL); if(!value) value = ini_get(game_ini, "styles", n, def); ht_insert(s->styles, n, (char*)value); free(n); }
static void set_style_gt0(struct game_state *s, const char *name, const char *def) { /* For styles that must be greater than 0 */ char *n = my_strlower(strdup(name)); const char *value = ini_get(game_ini, s->name, n, NULL); if(!value) value = ini_get(game_ini, "styles", n, def); if(atoi(value) < 0) ht_insert(s->styles, n, "0"); else ht_insert(s->styles, n, (char*)value); free(n); }
const char *get_style(struct game_state *s, const char *name) { char *n = my_strlower(strdup(name)); char *v = ht_find(s->styles, n); free(n); return v; }
int main(int argc, char *argv[]) { // if(testme()) // return 0; if (argc == 2 && (strcmp(argv[0], "-h") == 0 || strcmp(argv[0], "--help") == 0)) { printUsage(argv[0]); return EXIT_SUCCESS; } if (argc != 3) { printUsage(argv[0]); return EXIT_FAILURE; } FILE * f; if ((f = fopen(argv[2], "r")) == NULL) { printf("Unable to open file '%s', aborting!\n", argv[2]); return EXIT_FAILURE; } FILE * fptrout = stdout; int line_count = 0; char line_buffer[LINE_SIZE]; while (fgets(line_buffer, LINE_SIZE, f) != NULL) { line_count++; } int file_length = ftell(f); rewind(f); char * * src = malloc(sizeof(char *) * line_count); char * * copy = malloc(sizeof(char *) * line_count); int i; for (i = 0; i < line_count; i++) { if (feof(f)) { printf("Not enough lines in file!\n"); fclose(f); return EXIT_FAILURE; } copy[i] = malloc(sizeof(char) * LINE_SIZE); fgets(copy[i], LINE_SIZE, f); trim(copy[i]); src[i] = strdup(copy[i]); } fclose(f); char * buffer = malloc(sizeof(char) * file_length); buffer[0] = '\0'; //Partitioning outputs const char * command = argv[1]; if (strcmp(command, "my_strlen") == 0) { for (i = 0; i < line_count; i++) { fprintf(fptrout, "length: %d\n", my_strlen(copy[i])); } } else if (strcmp(command, "my_countchar") == 0) { for (i = 0; i < line_count; i++) { fprintf(fptrout, "count(%c): %d\n", copy[i][0], my_countchar(copy[i], copy[i][0])); } } else if (strcmp(command, "my_strupper") == 0) { for (i = 0; i < line_count; i++) { my_strupper(copy[i]); fprintf(fptrout, "uppercase: %s\n", copy[i]); } } else if (strcmp(command, "my_strlower") == 0) { for (i = 0; i < line_count; i++) { my_strlower(copy[i]); fprintf(fptrout, "lowercase: %s\n", copy[i]); } } else if (strcmp(command, "my_strcat") == 0) { for (i = 0; i < line_count; i++) { my_strcat(copy[i], " "); my_strcat(copy[i], src[i]); fprintf(fptrout, "%s\n", copy[i]); } } else if (strcmp(command, "my_strncat") == 0) { for (i = 0; i < line_count; i++) { my_strncat(copy[i], " ", 2); my_strncat(copy[i], "........", 1); fprintf(fptrout, "%s\n", copy[i]); } } else if (strcmp(command, "my_strcpy") == 0) { for (i = 0; i < line_count; i++) { my_strcpy(copy[i], "Copying this String."); fprintf(fptrout, "%s\n", copy[i]); } } else if (strcmp(command, "my_strncpy") == 0) { for (i = 0; i < line_count; i++) { my_strncpy(copy[i], src[i], 5); fprintf(fptrout, "%s\n", copy[i]); } } else if (strcmp(command, "my_strstr") == 0) { for (i = 0; i < line_count; i++) { fprintf(fptrout, "%d\n", my_strstr(copy[i], src[i]) != NULL); } } else if (strcmp(command, "my_strinsert") == 0) { for (i = 0; i < line_count; i++) { my_strinsert(buffer, "\n", 0); my_strinsert(buffer, src[i], 0); } fprintf(fptrout, "[%s]\n", buffer); } else if (strcmp(command, "my_strdelete") == 0) { for (i = 0; i < line_count; i++) { my_strinsert(buffer, "\n", 0); my_strinsert(buffer, src[i], 0); my_strdelete(buffer, 0, 10); } fprintf(fptrout, "[%s]\n", buffer); } for (i = 0; i < line_count; i++) { free(copy[i]); free(src[i]); } free(src); free(copy); free(buffer); fclose(fptrout); return EXIT_SUCCESS; }