/* util.c: Some utility functions. * * 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 */ #include #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 tod 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. * * Arguements: 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; }