source: trunk/essentials/sys-apps/gawk/extension/ordchr.c

Last change on this file was 3076, checked in by bird, 18 years ago

gawk 3.1.5

File size: 2.4 KB
Line 
1/*
2 * ordchr.c - Builtin functions that provide ord() and chr() functions.
3 *
4 * Arnold Robbins
5 * arnold@skeeve.com
6 * 8/2001
7 * Revised 6/2004
8 */
9
10/*
11 * Copyright (C) 2001, 2004 the Free Software Foundation, Inc.
12 *
13 * This file is part of GAWK, the GNU implementation of the
14 * AWK Programming Language.
15 *
16 * GAWK is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * GAWK is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
29 */
30
31#include "awk.h"
32
33/* do_ord --- return numeric value of first char of string */
34
35static NODE *
36do_ord(tree)
37NODE *tree;
38{
39 NODE *str;
40 int ret = -1;
41
42 if (do_lint && get_curfunc_arg_count() > 1)
43 lintwarn("ord: called with too many arguments");
44
45 str = get_argument(tree, 0);
46 if (str != NULL) {
47 (void) force_string(str);
48 ret = str->stptr[0];
49 free_temp(str);
50 } else if (do_lint)
51 lintwarn("ord: called with no arguments");
52
53
54 /* Set the return value */
55 set_value(tmp_number((AWKNUM) ret));
56
57 /* Just to make the interpreter happy */
58 return tmp_number((AWKNUM) 0);
59}
60
61/* do_chr --- turn numeric value into a string */
62
63static NODE *
64do_chr(tree)
65NODE *tree;
66{
67 NODE *num;
68 unsigned int ret = 0;
69 AWKNUM val = 0.0;
70 char str[2];
71
72 str[0] = str[1] = '\0';
73
74 if (do_lint && get_curfunc_arg_count() > 1)
75 lintwarn("chr: called with too many arguments");
76
77 num = get_argument(tree, 0);
78 if (num != NULL) {
79 val = force_number(num);
80 ret = val; /* convert to int */
81 free_temp(num);
82 ret &= 0xff;
83 str[0] = ret;
84 str[1] = '\0';
85 } else if (do_lint)
86 lintwarn("chr: called with no arguments");
87
88
89 /* Set the return value */
90 set_value(tmp_string(str, 1));
91
92 /* Just to make the interpreter happy */
93 return tmp_number((AWKNUM) 0);
94}
95
96/* dlload --- load new builtins in this library */
97
98NODE *
99dlload(tree, dl)
100NODE *tree;
101void *dl;
102{
103 make_builtin("ord", do_ord, 1);
104 make_builtin("chr", do_chr, 1);
105
106 return tmp_number((AWKNUM) 0);
107}
Note: See TracBrowser for help on using the repository browser.