| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Jiri Zarevucky
|
|---|
| 3 | * Copyright (c) 2011 Petr Koupy
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | /** @addtogroup libposix
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /** @file Character classification.
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #define LIBPOSIX_INTERNAL
|
|---|
| 37 |
|
|---|
| 38 | #include "ctype.h"
|
|---|
| 39 |
|
|---|
| 40 | // TODO: propose for inclusion in libc
|
|---|
| 41 |
|
|---|
| 42 | /**
|
|---|
| 43 | * Checks whether character is a hexadecimal digit.
|
|---|
| 44 | *
|
|---|
| 45 | * @param c Character to inspect.
|
|---|
| 46 | * @return Non-zero if character match the definition, zero otherwise.
|
|---|
| 47 | */
|
|---|
| 48 | int posix_isxdigit(int c)
|
|---|
| 49 | {
|
|---|
| 50 | return isdigit(c) ||
|
|---|
| 51 | (c >= 'a' && c <= 'f') ||
|
|---|
| 52 | (c >= 'A' && c <= 'F');
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | /**
|
|---|
| 56 | * Checks whether character is a word separator.
|
|---|
| 57 | *
|
|---|
| 58 | * @param c Character to inspect.
|
|---|
| 59 | * @return Non-zero if character match the definition, zero otherwise.
|
|---|
| 60 | */
|
|---|
| 61 | int posix_isblank(int c)
|
|---|
| 62 | {
|
|---|
| 63 | return c == ' ' || c == '\t';
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | /**
|
|---|
| 67 | * Checks whether character is a control character.
|
|---|
| 68 | *
|
|---|
| 69 | * @param c Character to inspect.
|
|---|
| 70 | * @return Non-zero if character match the definition, zero otherwise.
|
|---|
| 71 | */
|
|---|
| 72 | int posix_iscntrl(int c)
|
|---|
| 73 | {
|
|---|
| 74 | return c < 0x20 || c == 0x7E;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | /**
|
|---|
| 78 | * Checks whether character is any printing character except space.
|
|---|
| 79 | *
|
|---|
| 80 | * @param c Character to inspect.
|
|---|
| 81 | * @return Non-zero if character match the definition, zero otherwise.
|
|---|
| 82 | */
|
|---|
| 83 | int posix_isgraph(int c)
|
|---|
| 84 | {
|
|---|
| 85 | return posix_isprint(c) && c != ' ';
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | /**
|
|---|
| 89 | * Checks whether character is a printing character.
|
|---|
| 90 | *
|
|---|
| 91 | * @param c Character to inspect.
|
|---|
| 92 | * @return Non-zero if character match the definition, zero otherwise.
|
|---|
| 93 | */
|
|---|
| 94 | int posix_isprint(int c)
|
|---|
| 95 | {
|
|---|
| 96 | return posix_isacsii(c) && !posix_iscntrl(c);
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | /**
|
|---|
| 100 | * Checks whether character is a punctuation.
|
|---|
| 101 | *
|
|---|
| 102 | * @param c Character to inspect.
|
|---|
| 103 | * @return Non-zero if character match the definition, zero otherwise.
|
|---|
| 104 | */
|
|---|
| 105 | int posix_ispunct(int c)
|
|---|
| 106 | {
|
|---|
| 107 | return !isspace(c) && !isalnum(c) && posix_isprint(c);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | /**
|
|---|
| 111 | * Checks whether character is ASCII. (obsolete)
|
|---|
| 112 | *
|
|---|
| 113 | * @param c Character to inspect.
|
|---|
| 114 | * @return Non-zero if character match the definition, zero otherwise.
|
|---|
| 115 | */
|
|---|
| 116 | extern int posix_isascii(int c)
|
|---|
| 117 | {
|
|---|
| 118 | return c >= 0 && c < 128;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | /**
|
|---|
| 122 | * Converts argument to a 7-bit ASCII character. (obsolete)
|
|---|
| 123 | *
|
|---|
| 124 | * @param c Character to convert.
|
|---|
| 125 | * @return Coverted character.
|
|---|
| 126 | */
|
|---|
| 127 | extern int posix_toascii(int c)
|
|---|
| 128 | {
|
|---|
| 129 | return c & 0x7F;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | /** @}
|
|---|
| 133 | */
|
|---|