1*63eb84d1Schristos /* Determine the number of screen columns needed for a character.
2*63eb84d1Schristos    Copyright (C) 2006 Free Software Foundation, Inc.
3*63eb84d1Schristos 
4*63eb84d1Schristos    This program is free software; you can redistribute it and/or modify
5*63eb84d1Schristos    it under the terms of the GNU General Public License as published by
6*63eb84d1Schristos    the Free Software Foundation; either version 2, or (at your option)
7*63eb84d1Schristos    any later version.
8*63eb84d1Schristos 
9*63eb84d1Schristos    This program is distributed in the hope that it will be useful,
10*63eb84d1Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
11*63eb84d1Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*63eb84d1Schristos    GNU General Public License for more details.
13*63eb84d1Schristos 
14*63eb84d1Schristos    You should have received a copy of the GNU General Public License
15*63eb84d1Schristos    along with this program; if not, write to the Free Software Foundation,
16*63eb84d1Schristos    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17*63eb84d1Schristos 
18*63eb84d1Schristos #ifndef _gl_WCWIDTH_H
19*63eb84d1Schristos #define _gl_WCWIDTH_H
20*63eb84d1Schristos 
21*63eb84d1Schristos #if HAVE_WCHAR_T
22*63eb84d1Schristos 
23*63eb84d1Schristos /* Get wcwidth if available, along with wchar_t.  */
24*63eb84d1Schristos # if HAVE_WCHAR_H
25*63eb84d1Schristos /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
26*63eb84d1Schristos    <wchar.h>.
27*63eb84d1Schristos    BSD/OS 4.1 has a bug: <stdio.h> and <time.h> must be included before
28*63eb84d1Schristos    <wchar.h>.  */
29*63eb84d1Schristos #  include <stdio.h>
30*63eb84d1Schristos #  include <time.h>
31*63eb84d1Schristos #  include <wchar.h>
32*63eb84d1Schristos # endif
33*63eb84d1Schristos 
34*63eb84d1Schristos /* Get iswprint.  */
35*63eb84d1Schristos # if HAVE_WCTYPE_H
36*63eb84d1Schristos #  include <wctype.h>
37*63eb84d1Schristos # endif
38*63eb84d1Schristos # if !defined iswprint && !HAVE_ISWPRINT
39*63eb84d1Schristos static inline int
40*63eb84d1Schristos #  if HAVE_WINT_T
iswprint(wint_t wc)41*63eb84d1Schristos iswprint (wint_t wc)
42*63eb84d1Schristos #  else
43*63eb84d1Schristos iswprint (int wc)
44*63eb84d1Schristos #  endif
45*63eb84d1Schristos {
46*63eb84d1Schristos   return (wc >= 0 && wc < 128
47*63eb84d1Schristos 	  ? wc >= ' ' && wc <= '~'
48*63eb84d1Schristos 	  : 1);
49*63eb84d1Schristos }
50*63eb84d1Schristos #  define iswprint iswprint
51*63eb84d1Schristos # endif
52*63eb84d1Schristos 
53*63eb84d1Schristos # ifndef HAVE_DECL_WCWIDTH
54*63eb84d1Schristos "this configure-time declaration test was not run"
55*63eb84d1Schristos # endif
56*63eb84d1Schristos # ifndef wcwidth
57*63eb84d1Schristos #  if !HAVE_WCWIDTH
58*63eb84d1Schristos 
59*63eb84d1Schristos /* wcwidth doesn't exist, so assume all printable characters have
60*63eb84d1Schristos    width 1.  */
61*63eb84d1Schristos static inline int
62*63eb84d1Schristos wcwidth (wchar_t wc)
63*63eb84d1Schristos {
64*63eb84d1Schristos   return wc == 0 ? 0 : iswprint (wc) ? 1 : -1;
65*63eb84d1Schristos }
66*63eb84d1Schristos 
67*63eb84d1Schristos #  elif !HAVE_DECL_WCWIDTH
68*63eb84d1Schristos 
69*63eb84d1Schristos /* wcwidth exists but is not declared.  */
70*63eb84d1Schristos extern
71*63eb84d1Schristos #   ifdef __cplusplus
72*63eb84d1Schristos "C"
73*63eb84d1Schristos #   endif
74*63eb84d1Schristos int wcwidth (int /* actually wchar_t */);
75*63eb84d1Schristos 
76*63eb84d1Schristos #  endif
77*63eb84d1Schristos # endif
78*63eb84d1Schristos 
79*63eb84d1Schristos #endif /* HAVE_WCHAR_H */
80*63eb84d1Schristos 
81*63eb84d1Schristos #endif /* _gl_WCWIDTH_H */
82