root/maint/gnulib/lib/unicase/u8-casefold.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. read_file
  2. main

   1 /* Casefolding mapping for UTF-8 strings (locale dependent).
   2    Copyright (C) 2009-2021 Free Software Foundation, Inc.
   3    Written by Bruno Haible <bruno@clisp.org>, 2009.
   4 
   5    This file is free software.
   6    It is dual-licensed under "the GNU LGPLv3+ or the GNU GPLv2+".
   7    You can redistribute it and/or modify it under either
   8      - the terms of the GNU Lesser General Public License as published
   9        by the Free Software Foundation; either version 3, or (at your
  10        option) any later version, or
  11      - the terms of the GNU General Public License as published by the
  12        Free Software Foundation; either version 2, or (at your option)
  13        any later version, or
  14      - the same dual license "the GNU LGPLv3+ or the GNU GPLv2+".
  15 
  16    This file is distributed in the hope that it will be useful,
  17    but WITHOUT ANY WARRANTY; without even the implied warranty of
  18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19    Lesser General Public License and the GNU General Public License
  20    for more details.
  21 
  22    You should have received a copy of the GNU Lesser General Public
  23    License and of the GNU General Public License along with this
  24    program.  If not, see <https://www.gnu.org/licenses/>.  */
  25 
  26 #include <config.h>
  27 
  28 /* Specification.  */
  29 #include "unicase.h"
  30 
  31 #define FUNC u8_casefold
  32 #define UNIT uint8_t
  33 #define U_CT_CASEFOLD u8_ct_casefold
  34 #include "u-casefold.h"
  35 
  36 
  37 #ifdef TEST
  38 
  39 #include <locale.h>
  40 #include <stdio.h>
  41 #include <stdlib.h>
  42 #include <string.h>
  43 
  44 /* Read the contents of an input stream, and return it, terminated with a NUL
  45    byte. */
  46 char *
  47 read_file (FILE *stream)
     /* [previous][next][first][last][top][bottom][index][help] */
  48 {
  49 #define BUFSIZE 4096
  50   char *buf = NULL;
  51   int alloc = 0;
  52   int size = 0;
  53   int count;
  54 
  55   while (! feof (stream))
  56     {
  57       if (size + BUFSIZE > alloc)
  58         {
  59           alloc = alloc + alloc / 2;
  60           if (alloc < size + BUFSIZE)
  61             alloc = size + BUFSIZE;
  62           buf = realloc (buf, alloc);
  63           if (buf == NULL)
  64             {
  65               fprintf (stderr, "out of memory\n");
  66               exit (1);
  67             }
  68         }
  69       count = fread (buf + size, 1, BUFSIZE, stream);
  70       if (count == 0)
  71         {
  72           if (ferror (stream))
  73             {
  74               perror ("fread");
  75               exit (1);
  76             }
  77         }
  78       else
  79         size += count;
  80     }
  81   buf = realloc (buf, size + 1);
  82   if (buf == NULL)
  83     {
  84       fprintf (stderr, "out of memory\n");
  85       exit (1);
  86     }
  87   buf[size] = '\0';
  88   return buf;
  89 #undef BUFSIZE
  90 }
  91 
  92 int
  93 main (int argc, char * argv[])
     /* [previous][next][first][last][top][bottom][index][help] */
  94 {
  95   setlocale (LC_ALL, "");
  96   if (argc == 1)
  97     {
  98       /* Display the case folded input string.  */
  99       char *input = read_file (stdin);
 100       int length = strlen (input);
 101       size_t output_length;
 102       uint8_t *output =
 103         u8_casefold ((uint8_t *) input, length, uc_locale_language (),
 104                     NULL,
 105                     NULL, &output_length);
 106 
 107       fwrite (output, 1, output_length, stdout);
 108 
 109       return 0;
 110     }
 111   else
 112     return 1;
 113 }
 114 
 115 #endif /* TEST */

/* [previous][next][first][last][top][bottom][index][help] */