#include "StdAfx.h" #include "StrArray.h" //************************************************************************** StrArray::StrArray(void) { m_str=m_i=n_i=n_str=0; str=NULL; ind=NULL; incremento_char=4056; incremento_str=30; } //************************************************************************** StrArray::~StrArray(void) { borra(); } //************************************************************************** void StrArray::cuida_memoria( int n,int ni ) { //miramos si entra str mas if (n_i+ni>=m_i) { if (ni>incremento_str) m_i+=ni; else m_i+=incremento_str; if (ind) { ind=(int (*)[])realloc(ind,sizeof(int)*m_i); } else { ind=(int (*)[])malloc(sizeof(int)*m_i); } } if (n_str+n>=m_str) { if (n>incremento_char) m_str+=n; else m_str+=incremento_char; if (str) { str=(char*)realloc(str,m_str); } else { str=(char*)malloc(m_str); } } } //************************************************************************** void StrArray::borra() { m_str=m_i=n_i=n_str=0; if (str) free(str); str=NULL; if(ind) free(ind); ind=NULL; } //************************************************************************** char* StrArray::get( int i ) { if (i<0 || i>=n_i) return NULL; return &str[(*ind)[i]]; } //************************************************************************** int StrArray::size() { return n_i; } //************************************************************************** void StrArray::add( char*c ) { int n=(int)strlen(c)+1; cuida_memoria(n); (*ind)[n_i]=n_str; n_i++; strcpy(&str[n_str],c); n_str+=n; } //************************************************************************** void StrArray::add( StrArray* st ) { //pedimos memoria---------------------- cuida_memoria(st->n_str,st->n_i); //copiamos chars----------------------- memcmp(&str[n_str], str,st->n_str); //ponemos indices---------------------- for (int i=0; in_i; i++) { (*ind)[n_i+i]=n_str+(*st->ind)[i]; } //monemos posiciones como llenas------- n_i=st->n_i; n_str+=st->n_str; } void StrArray::add( char*c, int ncharReser ) { char ss[1]; ss[0]=0; if(!c) c = ss; int n=max((int)strlen(c)+1, ncharReser); cuida_memoria(n); (*ind)[n_i]=n_str; n_i++; strcpy(&str[n_str],c); n_str+=n; } //************************************************************************** void StrArray::compacta() { int i=0; for (int j=0; j=n_i) return; if ((i+1)add(st); } return buf; } //**************************************************************************