Programme de conversion de formules mathématiques écrites en ASCII pseudo-C vers LaTeX.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
4.4 KiB

/*
* a simplistic API to manipulate ASCII strings in C.
*
* this is part of the libstr library sources
* copyright © 2008 Benoît Rouits <brouits@free.fr>
* released under the terms of GNU LGPL
* (GNU Lesser General Public License).
* more information on http://brouits.free.fr/libstr/
* licence text on http://www.gnu.org/licenses/lgpl.txt
*/
#include <stdlib.h>
#include <ctype.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <fnmatch.h>
#include <regex.h>
char* str_new (void)
{
/* create an empty string */
char* str;
str = malloc(1);
str[0]='\0';
return str;
}
void str_del (char* str)
{
/* delete a string */
free(str);
}
char* str_catall (const char* s, ...)
{
char *result = NULL;
if ( s != NULL )
{
va_list ap;
size_t n = 0;
va_start( ap, s );
for ( const char *p = s; p != NULL; p = va_arg( ap, const char * ) )
{
n += strlen( p );
}
va_end( ap );
++n;
result = ( char * )malloc( n );
if ( result != NULL )
{
n = 0;
va_start( ap, s );
for ( const char *p = s; p != NULL; p = va_arg( ap, const char * ) )
{
strcpy( result + n, p );
n += strlen( p );
}
va_end( ap );
}
}
return result;
}
char* str_cat (const char* str1, const char* str2)
{
/* cat two strings */
char* str;
str = malloc(strlen(str1)+strlen(str2)+1);
str[0]='\0';
strcat(str,str1);
strcat(str,str2);
return str;
}
int str_len (const char* str)
{
/* give length of string */
return strlen(str);
}
int str_end (const char* str)
{
/* give last index of string */
return strlen(str)-1;
}
char* str_up (const char* str)
{
/* capitalize string */
char* s;
int i;
s = malloc(strlen(str)+1);
for (i=0;i<strlen(str);i++)
s[i]=toupper(str[i]);
s[i]='\0';
return s;
}
char* str_cap (const char* str)
{
/* capitalize first letter */
char* s;
s = str_cat(str,"");
s[0]=toupper(str[0]);
return s;
}
char* str_low (const char* str)
{
/* lower string */
char* s;
int i;
s = malloc(strlen(str)+1);
for (i=0;i<strlen(str);i++)
s[i]=tolower(str[i]);
s[i]='\0';
return s;
}
char* str_sub (const char* str, int begin, int length)
{
/* create substring */
char* s;
s = malloc (length+1);
strncpy(s,&str[begin],length);
s[length]='\0';
return s;
}
int str_find (const char* str, const char* search)
{
/* search substing */
char *yes;
yes = strstr(str,search);
if (yes)
return yes - str;
else
return strlen(str);
}
int str_fnm (const char* str, const char* pattern)
{
/* looks fnmatch */
return !fnmatch(pattern, str, 0);
}
int str_rgm (const char* str, const char* regex)
{
/* looks re match */
regex_t *preg;
int r;
preg=malloc(sizeof(regex_t));
regcomp(preg,regex,REG_NOSUB|REG_EXTENDED);
r = regexec(preg,str,0,0,0);
regfree(preg);
free(preg);
return !r;
}
char* str_tr (const char* str, const char* from, const char* to)
{
/* translate substring (raw/raw) */
char* result;
int len1,len2,len3,len,i;
len1 = strlen(str);
len2 = strlen(from);
len3 = strlen(to);
if (len1-len2+len3 < 0)
return strdup(str);
result = malloc((len1-len2+len3+1)*sizeof(char));
len = str_find(str,from);
result[0]='\0';
if (len == len1)
return strdup(str);
strncat(result,str,len);
strncat(result,to,len3);
for (i=len+len3;i<len1-len2+len3;i++)
result[i] = str[len+len2+i-len-len3];
return result;
}
char* str_sed (const char* str, const char* regex1, const char* edit)
{
/* translate substring (regex/sedex) */
return "";
}
char** str_split (const char* str, const char* sep, char** trail)
{
/* split a string into a NULL-terminated array of substrings, given separator */
char** array;
char* string;
const char* s;
char* e;
int i;
array = NULL;
i = 0;
s = str;
while ((e = strstr(s, sep))) {
++i;
string = strndup(s, e-s);
s = e + strlen(sep);
array = realloc(array, i*sizeof(char*));
array[i-1] = string;
}
++i;
array = realloc(array, i*sizeof(char*));
array[i-1] = NULL;
*trail = strndup(s, (s-str)+strlen(str));
return array;
}