473,480 Members | 2,592 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

isprint() equivalent for ISO-8859-15?

Hi,

I need to be able to tell if a character in the ISO-8859-15 codeset
(i.e. 8-bit ASCII, incorporating things like accented 'a' or euro currency
symbol) is printable. Ordinarily I'd use isprint(), but obviously this is
only going to work for 7-bit "true ASCII" characters.

I've thought of running the program in a different locale, but I want to
catch *all* printable characters, not only those used by a specific
locale. It'd be nice for the code to be as portable as possible, too.

I could just write a macro to range-check the character (it's not like the
standard's going to change), but I'd prefer a cleaner way if possible!

All help gratefully appreciated.

Cheers
- Ian

--
Ian Chard <ia*@tanagra.demon.co.uk>
"Resting" Unix systems administrator and RHCE
near Linlithgow, central Scotland

Nov 13 '05 #1
9 4440
"Ian Chard" <ia*@tanagra.demon.co.uk> writes:
I need to be able to tell if a character in the ISO-8859-15 codeset
(i.e. 8-bit ASCII, incorporating things like accented 'a' or euro currency
symbol) is printable.
#define IS_ISO_8859_15(c) \
(((unsigned char)(c) >= 0x20 && (unsigned char)(c) < 0x7F) \
|| ((unsigned char)(c) >= 0xA0 && (unsigned char)(c) <= 0xFF))

or the function equivalent if a macro is not acceptable.
Ordinarily I'd use isprint(), but obviously this is only going to work
for 7-bit "true ASCII" characters.
Actually, it is locale dependent how isprint behaves.
I've thought of running the program in a different locale, but I want to
catch *all* printable characters, not only those used by a specific
locale. It'd be nice for the code to be as portable as possible, too.
If you restrict yourself to a specific character encoding (ISO-8859-15),
the code will obviously not run correctly on systems with incompatible
character encodings. Therefore, it is by definition not portable.

If you want the most portable solution, use `isprint'.
I could just write a macro to range-check the character (it's not like the
standard's going to change), but I'd prefer a cleaner way if possible!


If you want to check for a printable character in a portable way, use
`isprint'. If you want to check for a character printable in ISO-8859-15
encoding, use something like the macro above.

Martin
Nov 13 '05 #2
"Ian Chard" <ia*@tanagra.demon.co.uk> wrote in message
news:pa****************************@tanagra.demon. co.uk...
I need to be able to tell if a character in the ISO-8859-15 codeset [snip] I've thought of running the program in a different locale, but I want to
catch *all* printable characters, not only those used by a specific
locale.


A printable character in one locale may not be in another. In other words,
the concept of a printable character is inherently locale-specific.

You seem to be asking for the logical "or" of isprint() return values for
all possible locales - this does not make sense at all. Perhaps I
misunderstand what you are asking.

Alex
Nov 13 '05 #3
In <bp*************@news.t-online.com> Martin Dickopp <ex****************@zero-based.org> writes:
"Ian Chard" <ia*@tanagra.demon.co.uk> writes:
I need to be able to tell if a character in the ISO-8859-15 codeset
(i.e. 8-bit ASCII, incorporating things like accented 'a' or euro currency
symbol) is printable.


#define IS_ISO_8859_15(c) \
(((unsigned char)(c) >= 0x20 && (unsigned char)(c) < 0x7F) \
|| ((unsigned char)(c) >= 0xA0 && (unsigned char)(c) <= 0xFF))


The casts to unsigned char are wrong, drop them. They make
IS_ISO_8859_15(288) return true on any implementation with
UCHAR_MAX == 256.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #4
Da*****@cern.ch (Dan Pop) writes:
In <bp*************@news.t-online.com> Martin Dickopp <ex****************@zero-based.org> writes:
"Ian Chard" <ia*@tanagra.demon.co.uk> writes:
I need to be able to tell if a character in the ISO-8859-15 codeset
(i.e. 8-bit ASCII, incorporating things like accented 'a' or euro currency
symbol) is printable.


#define IS_ISO_8859_15(c) \
(((unsigned char)(c) >= 0x20 && (unsigned char)(c) < 0x7F) \
|| ((unsigned char)(c) >= 0xA0 && (unsigned char)(c) <= 0xFF))


The casts to unsigned char are wrong, drop them. They make
IS_ISO_8859_15(288) return true on any implementation with
UCHAR_MAX == 256.


Why does that make them wrong? I would expect the above macro to
be documented similar to "expects a char [or unsigned char] as
argument", in which case a caller giving an argument of 288
should /expect/ undefined results in the situation you've
described.

--
Micah J. Cowan
mi***@cowan.name
Nov 13 '05 #5
In article <m3************@localhost.localdomain> Micah Cowan <mi***@cowan.name> writes:
Da*****@cern.ch (Dan Pop) writes: ....
#define IS_ISO_8859_15(c) \
(((unsigned char)(c) >= 0x20 && (unsigned char)(c) < 0x7F) \
|| ((unsigned char)(c) >= 0xA0 && (unsigned char)(c) <= 0xFF))


The casts to unsigned char are wrong, drop them. They make
IS_ISO_8859_15(288) return true on any implementation with
UCHAR_MAX == 256.


Why does that make them wrong? I would expect the above macro to
be documented similar to "expects a char [or unsigned char] as
argument",


That is *not* the wording for "isprint" and friends. There the argument is
an int with a value that is representable as unsigned char, or is EOF.
IS_ISO_8859_15(EOF) most likely will yield true.
in which case a caller giving an argument of 288
should /expect/ undefined results in the situation you've
described.


Note that "isascii" is a common extension which expects an arbitrary
integer. Your macro more resembles isprint. And wouldn't it be easier
to just use "setlocale"?
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 13 '05 #6
In <m3************@localhost.localdomain> Micah Cowan <mi***@cowan.name> writes:
Da*****@cern.ch (Dan Pop) writes:
In <bp*************@news.t-online.com> Martin Dickopp <ex****************@zero-based.org> writes:
>"Ian Chard" <ia*@tanagra.demon.co.uk> writes:
>
>> I need to be able to tell if a character in the ISO-8859-15 codeset
>> (i.e. 8-bit ASCII, incorporating things like accented 'a' or euro currency
>> symbol) is printable.
>
>#define IS_ISO_8859_15(c) \
> (((unsigned char)(c) >= 0x20 && (unsigned char)(c) < 0x7F) \
> || ((unsigned char)(c) >= 0xA0 && (unsigned char)(c) <= 0xFF))


The casts to unsigned char are wrong, drop them. They make
IS_ISO_8859_15(288) return true on any implementation with
UCHAR_MAX == 256.


Why does that make them wrong? I would expect the above macro to
be documented similar to "expects a char [or unsigned char] as
argument", in which case a caller giving an argument of 288
should /expect/ undefined results in the situation you've
described.


Since I haven't seen the documentation of IS_ISO_8859_15, I would expect
it to do the job advertised by its name.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #7
"Dik T. Winter" <Di********@cwi.nl> writes:
In article <m3************@localhost.localdomain> Micah Cowan <mi***@cowan.name> writes:
> Da*****@cern.ch (Dan Pop) writes: ...
> > >#define IS_ISO_8859_15(c) \
> > > (((unsigned char)(c) >= 0x20 && (unsigned char)(c) < 0x7F) \
> > > || ((unsigned char)(c) >= 0xA0 && (unsigned char)(c) <= 0xFF))
> >
> > The casts to unsigned char are wrong, drop them. They make
> > IS_ISO_8859_15(288) return true on any implementation with
> > UCHAR_MAX == 256.

>
> Why does that make them wrong? I would expect the above macro to
> be documented similar to "expects a char [or unsigned char] as
> argument",


That is *not* the wording for "isprint" and friends. There the argument is
an int with a value that is representable as unsigned char, or is EOF.
IS_ISO_8859_15(EOF) most likely will yield true.


I didn't say it was. The above is not isprint(). I do agree that
it should probably act the same way as isprint()--but it wouldn't
have to. Handling 288 correctly, though (assuming it's out of
range for an unsigned char) is not something one should expect of
isprint(), either.
> in which case a caller giving an argument of 288
> should /expect/ undefined results in the situation you've
> described.


Note that "isascii" is a common extension which expects an arbitrary
integer. Your macro more resembles isprint. And wouldn't it be easier
to just use "setlocale"?


Not my macro. And I agree about setlocale().

--
Micah J. Cowan
mi***@cowan.name
Nov 13 '05 #8
Da*****@cern.ch (Dan Pop) writes:
In <m3************@localhost.localdomain> Micah Cowan <mi***@cowan.name> writes:
Why does that make them wrong? I would expect the above macro to
be documented similar to "expects a char [or unsigned char] as
argument", in which case a caller giving an argument of 288
should /expect/ undefined results in the situation you've
described.


Since I haven't seen the documentation of IS_ISO_8859_15, I would expect
it to do the job advertised by its name.


Fair enough: perhaps this should serve as a reminder to all that,
no matter how small their example, they should always specify
exactly *how* it is meant be used, even if you think it should be
obvious from reading.

--
Micah J. Cowan
mi***@cowan.name
Nov 13 '05 #9
in comp.lang.c i read:
I need to be able to tell if a character in the ISO-8859-15 codeset
(i.e. 8-bit ASCII, incorporating things like accented 'a' or euro currency
symbol) is printable. Ordinarily I'd use isprint(), but obviously this is
only going to work for 7-bit "true ASCII" characters.
isprint will work fine if the locale is set appropriately.
I've thought of running the program in a different locale, but I want to
catch *all* printable characters, not only those used by a specific
locale.


on most systems a uni/single-byte character sequence cannot compose all
possible printable characters, char having a range well below that
necessary for even the small number of unique characters in the iso-8859-x
repertoires. for this to be possible you need multi-byte character
sequences.

yet if your stream contains mbcs then isprint is totally inappropriate, you
need to convert to a wchar_t or wint_t and use iswprint, and the conversion
demands that the locale be set appropriately.

--
a signature
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
7210
by: John | last post by:
Is there an equivalent of COM on Linux that I can get through Python. My need is to have some sort of language independent component framework. I can think of CORBA but I have to have a server...
2
3264
by: Michael Foord | last post by:
Please pardon my ignorance on this one - but I'm not certain how the sign bt is treated in python bitwise operators. I've trying to convert a javascript DES encryption routine into python. ...
3
2266
by: Robert Dodier | last post by:
Hello, Here's a thought that I'm sure has already occurred to someone else, I just can't find any record of it yet. An XML document is just a more verbose and clumsy representation of an...
2
4162
by: SunRise | last post by:
Hi I am creating a C Program , to extract only-Printable-characters from a file ( any type of file) and display them. OS: Windows-XP Ple help me to fix the Errors & Warnings and explain...
7
3700
by: Tim Conner | last post by:
Hi, I am an ex-delphi programmer, and I having a real hard time with the following simple code (example ): Which is the equivalent to the following code ? var chars : PChar; sBack, s :...
5
9101
by: Jaga | last post by:
1.) I am using C#. Is there any similar function to isprint in C? 2.) If I trap the event OnKeyDown in a Control, I become a System.Windows.Forms.KeyEventArgs. How can I determine whether...
10
7300
by: karch | last post by:
How would this C# contruct be represented in C++/CLI? Or is it even possible? PolicyLevel level = (enumerator->Current) as PolicyLevel; Thanks, Karch
9
4027
by: Alan Silver | last post by:
Hello, I'm converting some old VB6 code to use with ASP.NET and have come unstuck with the Asc() function. This was used in the old VB6 code to convert a character to its ASCII numeric...
14
2517
by: grid | last post by:
Hi, I have a certain situation where a particular piece of code works on a particular compiler but fails on another proprietary compiler.It seems to have been fixed but I just want to confirm if...
0
7055
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7060
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6760
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5365
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4501
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3013
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3004
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.