File: util.c

package info (click to toggle)
tkdesk 2.0-12
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,596 kB
  • sloc: tcl: 20,764; ansic: 16,262; sh: 359; makefile: 233
file content (135 lines) | stat: -rw-r--r-- 3,212 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* ============================================================================
 *
 * File:	util.c
 * Project:	TkDesk
 * Started:	12.07.96
 * Changed:	12.07.96
 *
 * Provides several utility C subroutines.
 *
 * Copyright (C) 1996  Christian Bolik
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 * See the file "COPYING" in the base directory of this distribution
 * for more.
 *
 * ========================================================================= */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "libdesk.h"


/* ---------------------------------------------------------------------------
 * escape_chars:
 * Precedes each char of chars in str with a backslash. Puts the result into
 * buf, which must be big enough.
 */
char *escape_chars (str, chars, buf)
char *str;
char *chars;
char *buf;
{
    char *bp;

    bp = buf;
    while (*str) {
	if (strchr (chars, *str)) {
	    *bp++ = '\\';
	}
	*bp++ = *str++;
	    
    }
    *bp = '\0';

    return buf;
} /* escape_chars */

/* ---------------------------------------------------------------------------
 * unescape_chars:
 * Removes all backslashes from str, except before }. \t is replaced with
 * a real tab. Puts result at buf.
 */
char *unescape_chars (str, buf)
char *str;
char *buf;
{
    char *bp;

    bp = buf;
    while (*str) {
	if (*str != '\\') {
	    *bp++ = *str++;
	} else {
	    switch (*(str + 1)) {
	    case 't':
		str += 2; /* points just past t now */
		*bp++ = '\t';
		break;
#ifdef UNDEFINED
	    case '}':
		*bp++ = *str++; /* copy backslash in this case */
		break;
#endif
	    default:
		str++;
	    }
	}
    }
    *bp = '\0';

    return buf;
} /* unescape_chars */


/* ============================================================================
 * Name: itoa
 * Desc: Converts an unsigned long integer to a string.
 * In  : val - integer
 * Out : pointer to static string
 * ------------------------------------------------------------------------- */

char *itoa(val)
long val;
{
    static char str[16];
    int i = 0, r;

#define I2CHAR(a)                     \
    if (val >= (a)) {                 \
	r = val / (a);                \
	str[i++] = '0' + r;           \
	val -= r * (a);               \
    } else if (i > 0)                 \
        str[i++] = '0';
   
    I2CHAR(1000000000l);
    I2CHAR(100000000l);
    I2CHAR(10000000l);
    I2CHAR(1000000l);
    I2CHAR(100000l);
    I2CHAR(10000l);
    I2CHAR(1000l);
    I2CHAR(100l);
    I2CHAR(10l);
   
    str[i++] = '0' + (val % 10);
    str[i] = '\0';

    return str;
}