/* * Copyright (C) 2004-2005 Vadim Berezniker * http://www.kryptolus.com * * 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, 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 GNU Make; see the file COPYING. If not, write to * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. * http://www.gnu.org/copyleft/gpl.html * */ #include "stdafx.h" #include "alloc.h" /* * Returns a pointer to the first non-whitespace character in the given string. * If the entire string contains whitespace characters, the pointer will point to the * terminating null. * * buffer: string to search in */ char *string_ignore_whitespce(char *buffer) { while(buffer[0] != 0 && (buffer[0] == ' ' || buffer[0] == '\t')) buffer++; return buffer; } /* * Returns a substring of the string pointed to by buffer containg the string * between the beginning character and the first occurance of the character 'until'. * If the character 'until' is not present and 'null_if_no_separator' is TRUE, a NULL value is returned. * If 'null_if_no_separator' is TRUE, a copy of the passed string is returned. * * This function moves the pointer that buffer points to as it searches for the 'until' character. * If 'until' is found, the pointer pointed to by buffer will point to the character after 'until'. * If it's not found, the pointer pointed to by buffer will point to the null character. * * The returned string must be freed. */ char *string_read_token(char **buffer, char until, gboolean null_if_no_separator) { char *rv; int i = 0; // forward until we find 'until' or until we reach a null while((*buffer)[i] != 0 && (*buffer)[i] != until) i++; // if we reached a null and 'null_if_no_superator' is true or the given string starts with a NULL, then we return NULL if((*buffer)[i] == 0 && (null_if_no_separator || i == 0)) return NULL; rv = (char *) kry_malloc(i+1); rv[i] = 0; strncpy(rv, *buffer, i); // usually add (i+1) so that the string points to after the separator // however, if the separator is not present, we only add i *buffer += (i + ((*buffer)[i] > 0 ? 1 : 0)); return rv; } /* * Returns a copy of the string pointed to by buffer. * Forwards the pointer pointed to by buffer to the terminating null. */ char *string_read_token_end(char **buffer) { char *rv = kry_strdup(*buffer); *buffer += strlen(rv); return rv; } int string_compare(char *a, char *b) { return strcmp(a, b); } char *string_replace(char *str, char *target, char *replacement) { int num_matches = 0; GList *matches = NULL; int len = strlen(str); int len_target = strlen(target); for(long i = 0; i < len; i++) { if(!strncmp(str + i, target, len_target)) { num_matches++; matches = g_list_append(matches, (gpointer) i); i += len_target; i--; } } if(!matches) return kry_strdup(str); int last_pos = 0; int offset = 0; char *new_str = (char *) kry_malloc(len + (strlen(replacement) - len_target) * num_matches + 1); GList *ptr = matches; while(TRUE) { if(ptr == NULL) { if(last_pos < len) { memcpy(new_str + offset, str + last_pos, len - last_pos); offset += (len - last_pos); new_str[offset] = 0; } break; } long match_idx = (long) ptr->data; if(last_pos < match_idx) { memcpy(new_str + offset, str + last_pos, match_idx - last_pos); offset += (match_idx - last_pos); } last_pos = match_idx + len_target; memcpy(new_str + offset, replacement, strlen(replacement)); offset += strlen(replacement); ptr = g_list_next(ptr); } return new_str; }