Textonyms: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: changed the output wording.)
m (C code modified to use GLib I/O functions to load the dictionary)
Line 226: Line 226:
g_string_set_size(text, word->len);
g_string_set_size(text, word->len);
for (size_t i = 0; i < word->len; ++i) {
for (size_t i = 0; i < word->len; ++i) {
char c = text_char(word->str[i]);
char c = text_char(g_ascii_tolower(word->str[i]));
if (c == 0)
if (c == 0)
return false;
return false;
Line 275: Line 275:
print_words(t->words);
print_words(t->words);
}
}
}

bool get_line(FILE* in, GString* line) {
int c, count = 0;
g_string_truncate(line, 0);
while ((c = getc(in)) != EOF) {
++count;
if (c == '\n')
break;
g_string_append_c(line, g_ascii_tolower(c));
}
return count > 0;
}
}


Line 293: Line 281:
}
}


bool find_textonyms(const char* filename) {
bool find_textonyms(const char* filename, GError** error_ptr) {
FILE* in = fopen(filename, "r");
GError* error = NULL;
GIOChannel* channel = g_io_channel_new_file(filename, "r", &error);
if (in == NULL) {
perror(filename);
if (channel == NULL) {
g_propagate_error(error_ptr, error);
return false;
return false;
}
}
Line 304: Line 293:
GString* text = g_string_sized_new(64);
GString* text = g_string_sized_new(64);
guint count = 0;
guint count = 0;
gsize term_pos;
while (get_line(in, word)) {
while (g_io_channel_read_line_string(channel, word, &term_pos,
&error) == G_IO_STATUS_NORMAL) {
g_string_truncate(word, term_pos);
if (!text_string(word, text))
if (!text_string(word, text))
continue;
continue;
Line 315: Line 307:
++count;
++count;
}
}
g_io_channel_unref(channel);
g_string_free(word, TRUE);
g_string_free(word, TRUE);
g_string_free(text, TRUE);
g_string_free(text, TRUE);
if (error != NULL) {
g_propagate_error(error_ptr, error);
g_hash_table_destroy(ht);
return false;
}


GArray* words = g_array_new(FALSE, FALSE, sizeof(textonym_t));
GArray* words = g_array_new(FALSE, FALSE, sizeof(textonym_t));
Line 323: Line 321:
g_hash_table_iter_init(&iter, ht);
g_hash_table_iter_init(&iter, ht);
while (g_hash_table_iter_next(&iter, &key, &value)) {
while (g_hash_table_iter_next(&iter, &key, &value)) {
GPtrArray* v = (GPtrArray*)value;
GPtrArray* v = value;
if (v->len > 1) {
if (v->len > 1) {
textonym_t textonym;
textonym_t textonym;
Line 354: Line 352:
g_array_free(words, TRUE);
g_array_free(words, TRUE);
g_hash_table_destroy(ht);
g_hash_table_destroy(ht);
fclose(in);
return true;
return true;
}
}
Line 363: Line 360:
return EXIT_FAILURE;
return EXIT_FAILURE;
}
}
GError* error = NULL;
if (!find_textonyms(argv[1]))
if (!find_textonyms(argv[1], &error)) {
if (error != NULL) {
fprintf(stderr, "%s: %s\n", argv[1], error->message);
g_error_free(error);
}
return EXIT_FAILURE;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
return EXIT_SUCCESS;
}</lang>
}</lang>