168 lines
3.2 KiB
C++
168 lines
3.2 KiB
C++
#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; i<st->n_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; j++)
|
|
{
|
|
if ((*ind)[j]==i)
|
|
{
|
|
i+=(int)strlen(get(j))+1;
|
|
}
|
|
else
|
|
{
|
|
strcpy(&str[i],get(j));
|
|
(*ind)[j]=i;
|
|
i+=(int)strlen(&str[i])+1;
|
|
}
|
|
}
|
|
n_str=i;
|
|
str=(char*)realloc(str, n_str);
|
|
|
|
m_str=n_str;
|
|
ind=(int (*)[])realloc(ind, n_i*sizeof(int));
|
|
m_i=n_i;
|
|
if (!str || !ind)
|
|
{
|
|
m_i=n_i=n_str=m_str=0;
|
|
}
|
|
|
|
}
|
|
//**************************************************************************
|
|
void StrArray::borra( int i )
|
|
{
|
|
if (i<0 || i>=n_i)
|
|
return;
|
|
if ((i+1)<n_i)
|
|
memcpy(&(*ind)[i],&(*ind)[i+1], sizeof(int)*(n_i-(i+1)));
|
|
n_i--;
|
|
}
|
|
//**************************************************************************
|
|
StrArray* StrArray::filtra( StrArray*buf, char *filter )
|
|
{
|
|
char *st;
|
|
for(int i =0; i<n_i; i++)
|
|
{
|
|
st = get(i);
|
|
if(strstr(st,filter))
|
|
buf->add(st);
|
|
}
|
|
return buf;
|
|
}
|
|
//**************************************************************************
|