Teacup rim text: Difference between revisions
m
C code modified to use GLib I/O functions to load the dictionary
m (added a bullet glyph in the task's preamble.) |
m (C code modified to use GLib I/O functions to load the dictionary) |
||
Line 113:
#include <stdlib.h>
#include <glib.h>
while ((c = getc(in)) != EOF) {▼
++count;▼
if (c == '\n')▼
break;▼
}▼
int string_compare(gconstpointer p1, gconstpointer p2) {
Line 132 ⟶ 120:
}
GPtrArray* load_dictionary(const char* file, GError** error_ptr) {
GIOChannel* channel = g_io_channel_new_file(file, "r", &error);
if (in == 0) {▼
if (channel ==
g_propagate_error(error_ptr, error);
return NULL;
}
GPtrArray* dict = g_ptr_array_new_full(1024, g_free);
GString* line = g_string_sized_new(64);
gsize term_pos;
while (g_io_channel_read_line_string(channel, line, &term_pos,
g_ptr_array_add(dict, g_strdup(line->str));▼
&error) == G_IO_STATUS_NORMAL) {
g_ptr_array_sort(dict, string_compare);▼
char* word = g_strdup(line->str);
word[term_pos] = '\0';
▲ }
g_string_free(line, TRUE);
g_propagate_error(error_ptr, error);
g_ptr_array_free(dict, TRUE);
}
▲ g_ptr_array_sort(dict, string_compare);
return dict;
}
Line 203 ⟶ 202:
return EXIT_FAILURE;
}
GError* error = NULL;
fprintf(stderr, "Cannot load dictionary file '%s': %s\n",
argv[1], error->message);
g_error_free(error);
return EXIT_FAILURE;
}
find_teacup_words(dictionary);
g_ptr_array_free(dictionary, TRUE);
|