/* Check if a string is hyphenated as expected, by passing the * expected hyphenation position array. * * @return 0 if the hyphenation is as expected and 1 otherwise. */ int check_hyphenation_pos(const char *tableList, const char *str, const char *expected) { widechar *inbuf; char *hyphens = NULL; int inlen = strlen(str); int retval = 0; inbuf = malloc(sizeof(widechar) * inlen); inlen = _lou_extParseChars(str, inbuf); if (!inlen) { fprintf(stderr, "Cannot parse input string.\n"); retval = 1; goto fail; } hyphens = calloc(inlen + 1, sizeof(char)); if (!lou_hyphenate(tableList, inbuf, inlen, hyphens, 0)) { fprintf(stderr, "Hyphenation failed.\n"); retval = 1; goto fail; } if (strcmp(expected, hyphens)) { fprintf(stderr, "Input: '%s'\n", str); fprintf(stderr, "Expected: '%s'\n", expected); fprintf(stderr, "Received: '%s'\n", hyphens); retval = 1; } fail: free(inbuf); free(hyphens); return retval; }
/** Check if a string is hyphenated as expected. * * @return 0 if the hyphenation is as expected and 1 otherwise. */ int check_hyphenation(const char *tableList, const char *str, const char *expected) { widechar *inbuf; widechar *hyphenatedbuf = NULL; uint8_t *hyphenated = NULL; char *hyphens = NULL; int inlen = strlen(str); size_t hyphenatedlen = inlen * 2; int retval = 0; inbuf = malloc(sizeof(widechar) * inlen); inlen = _lou_extParseChars(str, inbuf); if (!inlen) { fprintf(stderr, "Cannot parse input string.\n"); retval = 1; goto fail; } hyphens = calloc(inlen + 1, sizeof(char)); if (!lou_hyphenate(tableList, inbuf, inlen, hyphens, 0)) { fprintf(stderr, "Hyphenation failed.\n"); retval = 1; goto fail; } if (hyphens[0] != '0') { fprintf(stderr, "Unexpected output from lou_hyphenate.\n"); retval = 1; goto fail; } hyphenatedbuf = malloc(sizeof(widechar) * hyphenatedlen); int i = 0; int j = 0; hyphenatedbuf[i++] = inbuf[j++]; for (; j < inlen; j++) { if (hyphens[j] != '0') hyphenatedbuf[i++] = (widechar)'-'; hyphenatedbuf[i++] = inbuf[j]; } #ifdef WIDECHARS_ARE_UCS4 hyphenated = u32_to_u8(hyphenatedbuf, i, NULL, &hyphenatedlen); #else hyphenated = u16_to_u8(hyphenatedbuf, i, NULL, &hyphenatedlen); #endif if (!hyphenated) { fprintf(stderr, "Unexpected error during UTF-8 encoding\n"); free(hyphenatedbuf); retval = 2; goto fail; } if (strlen(expected) != hyphenatedlen || strncmp(expected, (const char *)hyphenated, hyphenatedlen)) { fprintf(stderr, "Input: '%s'\n", str); fprintf(stderr, "Expected: '%s'\n", expected); fprintf(stderr, "Received: '%.*s'\n", (int)hyphenatedlen, hyphenated); retval = 1; } free(hyphenatedbuf); free(hyphenated); fail: free(inbuf); free(hyphens); return retval; }
int main (int argc, char **argv) { widechar inbuf[BUFSIZE]; char hyphens[BUFSIZE]; int inlen; int k; int optc; set_program_name (argv[0]); while ((optc = getopt_long (argc, argv, "hv", longopts, NULL)) != -1) switch (optc) { /* --help and --version exit immediately, per GNU coding standards. */ case 'v': version_etc (stdout, program_name, PACKAGE_NAME, VERSION, AUTHORS, (char *) NULL); exit (EXIT_SUCCESS); break; case 'h': print_help (); exit (EXIT_SUCCESS); break; default: fprintf (stderr, "Try `%s --help' for more information.\n", program_name); exit (EXIT_FAILURE); break; } if (optind < argc) { /* Print error message and exit. */ fprintf (stderr, "%s: extra operand: %s\n", program_name, argv[optind]); fprintf (stderr, "Try `%s --help' for more information.\n", program_name); exit (EXIT_FAILURE); } validTable = NULL; mode = 0; while (1) { getCommands (); printf ("Type something, press enter, and view the results.\n"); printf ("A blank line returns to command entry.\n"); while (1) { inlen = getInput (); if (inlen == 0) break; for (k = 0; k < inlen; k++) inbuf[k] = inputBuffer[k]; if (!lou_hyphenate (table, inbuf, inlen, hyphens, mode)) { printf ("Hyphenation error\n"); continue; } printf ("Hyphenation mask: %s\n", hyphens); printf ("Hyphenated word: "); for (k = 0; k < inlen; k++) { if (hyphens[k] == '1') printf ("-"); printf ("%c", inbuf[k]); } printf ("\n"); } } lou_free (); return 0; }