// Read lines from the standard input and split them into words using
// the is_word_start and is_word_end attributes as set by Pango.
// 
// Compile with:
// gcc wordbreak.c -Wall -o wordbreak `pkg-config --cflags --libs pango`

#include <locale.h>
#include <unistd.h>
#include <pango/pango.h>


// write each word to the standard output on a separate line
void
show_word_breaks(GString *buffer) {

  PangoLogAttr *log_attrs;
  glong nlog_attrs, i, word_start;
  gchar *word;

  /* get logical attributes */
 
  nlog_attrs = g_utf8_strlen(buffer->str, -1) + 1;
  log_attrs = g_new(PangoLogAttr, nlog_attrs);

  pango_get_log_attrs(buffer->str, buffer->len, -1,
		      pango_language_get_default(), log_attrs, nlog_attrs);

  /* iterate over the log_attrs array and find word boundaries */

  word_start = -1;

  for (i=0; i < nlog_attrs; i++) {

    if (log_attrs[i].is_word_start == 1) {
      word_start = i;
    }

    /* if we have found a start and an end, print characters in between */
    if (log_attrs[i].is_word_end == 1 && word_start > -1) {

      word = g_utf8_substring(buffer->str, word_start, i);
      g_print("> %s <\n", word);
      g_free(word);

    }
  }

  /* clean up */

  g_free(log_attrs);
}



int main(gint argc, char* argv[]) {

  GIOChannel *channel;
  GString *buffer;
  
  setlocale(LC_ALL, "");	/* set locale according to env */
  g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL);

  /* open standard input */
  channel = g_io_channel_unix_new(STDIN_FILENO);

  /* read stdin one line at a time */

  buffer = g_string_new("");

  while (g_io_channel_read_line_string
	 (channel, buffer, NULL, NULL) != G_IO_STATUS_EOF) {

    if (g_utf8_validate(buffer->str, -1, NULL) == FALSE)
      g_critical("invalid utf8\n");

    /* show word breaks */
    show_word_breaks(buffer);
  }
  
  g_print("\n");

  /* exit */

  g_io_channel_shutdown(channel, TRUE, NULL);
  g_string_free(buffer, TRUE);
  return 0;
}
