nvk_str.h
Publicado: 28 May 2014, 19:24
Esta es una pequeña unidad que falta ampliar, pero que inicialmente sirve para tratar con
operaciones de texto, por ejemplo esta es usada en mi proyecto ArachniTracer.
Es bastante optima y, a diferencia de algunas funciones estandarizadas de C no produce
errores ni desbordamientos(ademas es bastante rapida).
Mas abajo dejare una pequeña documentación que hice para que sepan como se usa.
Código:
Documentación:
operaciones de texto, por ejemplo esta es usada en mi proyecto ArachniTracer.
Es bastante optima y, a diferencia de algunas funciones estandarizadas de C no produce
errores ni desbordamientos(ademas es bastante rapida).
Mas abajo dejare una pequeña documentación que hice para que sepan como se usa.
Código:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
typedef unsigned int __uint;
typedef unsigned int* __puint;
typedef unsigned long __ulong;
typedef unsigned long* __pulong;
typedef unsigned short __ushort;
typedef unsigned char __byte;
typedef unsigned char* __pbyte;
typedef char* __pchar;
typedef void* __pvoid;
void salloc(char **p, int sizeof_blocks, int mem_blocks);
void sfree(char **p, int mem_blocks);
void __bsncpy(char *s, char *buf, char c, __uint sslen);
inline void bsncpy(char *s, char *buf, __uint sslen);
void fncpy(char *s, int f, int t, char *__out);
__uint base(__uint a);
__uint cn(const char *s, const char c);
__uint chrp(char *s, char c);
void srev(char *s, char *__out);
void strlast(char *str, int last_n, char *output_buf);
void __ssplt(char *s, char delim, char **p, __uint max);
inline void ssplt(char *s, char delim, char **p);
int splt(char *s, char delim, char **p);
void salloc(char **p, int sizeof_blocks, int mem_blocks)
{
int m;
for(m=0;m<mem_blocks;m++)
p[m]= (char**)malloc(sizeof_blocks);
}
void sfree(char **p, int mem_blocks)
{
int m;
for(m=0;m<mem_blocks;m++)
if(p[m]!=NULL)
free( p[m] );
}
__uint base(__uint a)
{
__uint __b10 = 0;
// dividir su base en 10 - fragmentar el valor.
while ((a!=0) ? (a/=10) : (0))
__b10= __b10++ *10;
return(__b10*10);
}
void __bsncpy(char *s, char *buf, char c, __uint sslen)
{
int slen = (s) ? strlen(s) : 0;
if ( slen < buf && slen )
{
__uint x=0, ps = sslen;
for (;x<=slen && *s!=c;x++ && ps--)
{
if( ps<=0 )
break;
*buf++ = *s++;
}
*buf= '\0';
}
}
inline void bsncpy(char *s, char *buf, __uint sslen)
{
__bsncpy(s, buf, 0x00, sslen);
}
// Copiar desde hasta
void fncpy(char *s, int f, int t, char *__out)
{
int __f= 0, __t= strlen(s)-1;
if ( s )
{
while( 1 )
{
if (__f++==f)
{
__bsncpy(s, __out, 0x00, t);
__out[t] = '\0';
break;
}
if (*s++=='\0')
break;
}
}
}
// contar el numero de caracteres <char c> que se repiten en un string
__uint cn(const char *s, const char c)
{
__uint m= 0;
if ( s )
while ( *s++ != '\0' )
if (*s==c)
m++;
return m;
}
__uint chrp(char *s, char c)
{
__uint i= 0;
if ( s )
{
while(1)
{
i++;
if(*s==c)
return i;
if(*s++=='\0')
break;
}
}
return 0;
}
void srev(char *s, char *__out)
{
__uint i= strlen(s);
if (i)
{
char g[i];
__uint u= 0;
while (i--)
g[u++] = s[i];
g[u] = '\0';
bsncpy(g, __out, strlen(g));
}
}
void strlast(char *str, int last_n, char *output_buf)
{
volatile register int __ptr_length __asm__("ebx");
register int __ptr_last_n __asm__("ecx");
__asm__(
"mov %%eax, (%%esp);"
"call _strlen;"
"movl %%eax, %%ebx;"
::"a"(str)
);
__asm__("mov %%edx, %%ecx;"
"sub %%ebx, %%ecx;"
"neg %%ecx;" :: "d"(last_n) );
int i=0;
for(; i<=last_n; i++)
__asm__ volatile(";":"=r"(output_buf[i]):"a"(str[ __ptr_last_n++ ]));
}
void __ssplt(char *s, char delim, char **p, __uint max)
{
__uint
chr_pos = 0,
del_pos = 0,
u = 0,
slen = strlen(s);
if(!slen)
return;
while( 1 )
{
if(!(chr_pos=chrp(s, delim) ))
break;
fncpy(s, 0, (chr_pos-0x1), *p++);
for(del_pos=0;
del_pos<chr_pos;
del_pos++)
*s++;
u++;
if(max)
if(u>max)
break;
}
}
inline void ssplt(char *s, char delim, char **p)
{
__ssplt(s, delim, p, 0);
}
int splt(char *s, char delim, char **p)
{
__uint
chr_pos = 0,
del_pos = 0,
u = 0,
slen = strlen(s);
if(!slen)
return;
while(1)
{
if(!(chr_pos=chrp(s, delim) ))
break;
*p= (char **)malloc( chr_pos+0x1 );
fncpy(s, 0, (chr_pos-0x1), *p++);
u++;
for(del_pos=0;
del_pos<chr_pos;
del_pos++)
*s++;
}
return(u);
}
[/i]Documentación: