[go: up one dir, main page]

Menu

[r6]: / util.c  Maximize  Restore  History

Download this file

141 lines (128 with data), 3.8 kB

  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
136
137
138
139
/* util.c: Some utility functions.
*
* $Id$
*
* This file is part of tabler.
* Copyright (C) 2007 Heath Caldwell
*
* 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; see the file COPYING. If not, write to
* The Free Software Foundation, Inc.
* 59 Temple Place, Suite 330
* Boston, MA 02111 USA
*
* You may contact the author by:
* e-mail: Heath Caldwell <hncaldwell@csupomona.edu>
*/
#include <stdlib.h>
#include <stdio.h>
#include <regex.h>
#include <math.h>
#include "conf.h"
#include "util.h"
/* replace_backslashed
*
* Replaces, within the given string, certain backslashed characters with the
* actual character that they represent, destructively.
*
* Arguments: string: Pointer to string in which to do the replacement.
* NOTE: string will get clobbered.
* Returns: Pointer to string.
*/
char *
replace_backslashed(char *string)
{
unsigned int i, c;
for(i=0, c=0; string[i]; i++, c++) {
if(string[i] == '\\') {
switch(string[i+1]) {
case 'n':
string[c] = '\n';
i++;
break;
case 't':
string[c] = '\t';
i++;
break;
default:
string[c] = string[i];
break;
}
} else {
string[c] = string[i];
}
}
string[c] = '\0';
return string;
}
/* digits
*
* Finds the number of digits of a given integer in a given base.
*
* Arguments: n: Integer to count digits of.
* base: Base to count digits in.
* Returns: Number of digits n has in base base.
* If n is negative, returns the number of digits of
* the absolute value of n plus 1 (for the minus sign).
* If base is not above 1, returns 0.
*/
int
digits(int n, int base)
{
if(base <= 1) return 0;
return n ? (floor(log(fabs(n)) / log(base) + 1) + (n < 0 ? 1 : 0)) : 1;
}
/* strregtok
*
* Breaks a string up into tokens separated by a regex separator.
* Works just like strtok but you use a regex_t for the separator
* instead of a string of single character delimiters.
*
* Takes: string: Pointer to the string to be tokenized.
* NOTE: This string will get clobbered.
* separator: regex_t of a compiled regex to use
* as the separator.
*
* Returns: Pointer to next token or null if there are no more.
* See documentation for strtok.
*
* Internal Variables:
* _strregtok_previous_end: Stores end of previous token.
*/
char *
strregtok(char *string, const regex_t *seperator)
{
char error_string[ERROR_STRING_LENGTH];
regmatch_t match[1]; /* To store the matching part. */
int error = 0;
/* Return zero for the call after the last token. */
if(!string && !_strregtok_previous_end) return 0;
if(string)
_strregtok_previous_end = string;
else
string = _strregtok_previous_end;
error = regexec(seperator, string, 1, match, 0);
if(!error) {
_strregtok_previous_end = string + match[0].rm_eo;
*(string + match[0].rm_so) = '\0';
return string;
} else if(error == REG_NOMATCH) {
/* This is the last token. */
_strregtok_previous_end = 0;
return string;
} else {
regerror(error, seperator, error_string, ERROR_STRING_LENGTH);
fprintf(stderr, "Error: %s\n", error_string);
exit(EXIT_FAILURE);
}
}