/*\ * libLB - The LBPP support library * Copyright (C) 2001 Anthony Liguori * * libLB is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * libLB 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA \*/ #include #include #include "lb.h" /* * As you probably can tell, LBStrings are modelled closly after C++ * strings. This is not because C++ strings are better or because I * am C++ centric but rather because C++ strings are much more like * the native LB string so it is much easier and clearer to use a * C++ like string. Implementing LBStrings in terms of C strings * would probably not be nearly as efficent and would require a large * amount of code. The biggest benefit is that in the future, we may * decide to make use of the inline abilities of GCC to make LBStrings * super fast. * * A. Liguori */ // String constructors/destructors LBString lbstr_dup(LBString str) { str->rc++; return str; } LBString lbstr_cstr(char *str) { LBString ret = (LBString)malloc(sizeof(LBStringType)); ret->size = strlen(str); ret->data = str; ret->rc = 1; ret->constant = 1; return ret; } LBString lbstr_str(char *str) { LBString ret = (LBString)malloc(sizeof(LBStringType)); ret->size = strlen(str); ret->data = malloc(ret->size + 1); memcpy(ret->data, str, ret->size); ret->data[ret->size] = 0; ret->rc = 1; ret->constant = 0; return ret; } LBString lbstr_empty(int size) { LBString ret = (LBString)malloc(sizeof(LBStringType)); ret->size = size; ret->data = malloc(size + 1); ret->data[0] = 0; ret->rc = 1; ret->constant = 0; return ret; } LBString lbstr_nstr(char *str, int size) { LBString ret = (LBString)malloc(sizeof(LBStringType)); ret->size = size; ret->data = malloc(size + 1); memcpy(ret->data, str, size); ret->data[size] = 0; ret->rc = 1; ret->constant = 0; return ret; } void lbstr_destroy(LBString str) { str->rc--; if (!str->rc) { if (!str->constant) { free(str->data); } free(str); } } // string modifiers LBString lbstr_dirty(LBString str) { LBString ret; if ((str->rc == 1) && !str->constant) { ret = str; } else { ret = lbstr_str(str->data); } return ret; } void lbstr_set(LBString *rhs, LBString lhs) { lbstr_destroy(*rhs); *rhs = lbstr_dup(lhs); } // const string functions LBString lbstr_cat(LBString rhs, LBString lhs) { LBString ret = lbstr_dirty(rhs); ret->data = realloc(ret->data, ret->size + lhs->size + 1); memcpy(&(ret->data[ret->size]), lhs->data, lhs->size); ret->size += lhs->size; ret->data[ret->size] = 0; lbstr_destroy(lhs); return ret; } int lbstr_cmp(LBString rhs, LBString lhs) { int results = strcmp(rhs->data, lhs->data); if (results < 0) { results = -1; } else if (results > 0) { results = 1; } lbstr_destroy(rhs); lbstr_destroy(lhs); return results; } char *lb_skip_space(char *buf) { char *ptr = buf; while (isspace(*ptr)) { ptr++; } return ptr; } char *lb_skip_symbol(char *buf) { char *ptr = buf; while (isalnum(*ptr)) { ptr++; } return ptr; }