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>
 
bool get_line(FILE* in, GString* line) {
int c, count = 0;
g_string_set_size(line, 0);
while ((c = getc(in)) != EOF) {
++count;
if (c == '\n')
break;
g_string_append_c(line, c);
}
return count > 0;
}
 
int string_compare(gconstpointer p1, gconstpointer p2) {
Line 132 ⟶ 120:
}
 
GPtrArray* load_dictionary(const char* file, GError** error_ptr) {
FILEGError* inerror = fopen(file, "r")NULL;
GIOChannel* channel = g_io_channel_new_file(file, "r", &error);
if (in == 0) {
if (channel == perror(fileNULL); {
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 (get_line(in, line))
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_ptr_array_add(dict, g_strdup(line->str)word);
}
g_string_free(line, TRUE);
fcloseg_io_channel_unref(inchannel);
if (cerror =!= '\n'NULL) {
g_propagate_error(error_ptr, error);
g_ptr_array_free(dict, TRUE);
return breakNULL;
}
g_ptr_array_sort(dict, string_compare);
return dict;
}
Line 203 ⟶ 202:
return EXIT_FAILURE;
}
GError* error = NULL;
GPtrArray* dictionary = load_dictionary(argv[1]);
ifGPtrArray* (dictionary == NULLload_dictionary(argv[1], &error);
if (indictionary == 0NULL) {
while ((c = getc if (in))error != EOFNULL) {
fprintf(stderr, "Cannot load dictionary file '%s': %s\n",
argv[1], error->message);
g_error_free(error);
++count;}
return EXIT_FAILURE;
}
find_teacup_words(dictionary);
g_ptr_array_free(dictionary, TRUE);
1,777

edits