From aa0f3ae2988d372b190b9bde2e75a6d17e744e93 Mon Sep 17 00:00:00 2001 From: Ian Jauslin Date: Sun, 14 Jun 2015 00:52:45 +0000 Subject: Initial commit --- src/istring.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/istring.c (limited to 'src/istring.c') diff --git a/src/istring.c b/src/istring.c new file mode 100644 index 0000000..663c06a --- /dev/null +++ b/src/istring.c @@ -0,0 +1,99 @@ +/* +Copyright 2015 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#include +#include "istring.h" + +char* str_concat(char* ptr, const char* str){ + char* str_ptr=(char*)str; + + while(*str_ptr!='\0'){ + *ptr=*str_ptr; + ptr++; + str_ptr++; + } + *ptr='\0'; + return(ptr); +} + +int str_concat_memorysafe(char** str_out, int pos, const char* str, int* memory){ + char* out_ptr; + + if(str_len((char*)str)+pos>=*memory){ + *memory=*memory+str_len((char*)str)+pos+1; + resize_str(str_out,*memory); + } + out_ptr=*str_out+pos; + str_concat(out_ptr,str); + return(0); +} + +int resize_str(char** out, int memory){ + char* tmp_str=calloc(memory,sizeof(char)); + char* tmp_ptr=tmp_str; + char* out_ptr=*out; + + for(;*out_ptr!='\0';out_ptr++,tmp_ptr++){ + *tmp_ptr=*out_ptr; + } + *tmp_ptr='\0'; + + free(*out); + *out=tmp_str; + return(0); +} + +char* str_addchar(char* ptr, const char c){ + *ptr=c; + ptr++; + *ptr='\0'; + return(ptr); +} + +int str_len(char* str){ + char* ptr=str; + int ret=0; + while(*ptr!='\0'){ret++;ptr++;} + return(ret); +} + +int str_cmp(char* str1, char* str2){ + char* ptr1=str1; + char* ptr2=str2; + while(*ptr1==*ptr2 && *ptr1!='\0' && *ptr2!='\0'){ + ptr1++; + ptr2++; + } + if(*ptr1=='\0' && *ptr2=='\0'){ + return(1); + } + else{ + return(0); + } +} + + +int str_cpy_noalloc(char* input, char* output){ + char* ptr; + char* out_ptr; + + for(ptr=input,out_ptr=output;*ptr!='\0';ptr++,out_ptr++){ + *out_ptr=*ptr; + } + *out_ptr='\0'; + return(0); +} + -- cgit v1.2.3-70-g09d2