Initial Commit
This commit is contained in:
102
src/util.c
Normal file
102
src/util.c
Normal file
@@ -0,0 +1,102 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
|
||||
extern int nb_ligne;
|
||||
int indent_xml = 0;
|
||||
int indent_step = 1; // set to 0 for no indentation
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
|
||||
void erreur(char *message) {
|
||||
fprintf (stderr, "Ligne %d : ", nb_ligne);
|
||||
fprintf (stderr, "%s\n", message);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
|
||||
void erreur_1s(char *message, char *s) {
|
||||
fprintf( stderr, "Ligne %d : ", nb_ligne );
|
||||
fprintf( stderr, message, s );
|
||||
fprintf( stderr, "\n" );
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
|
||||
char *duplique_chaine(char *src) {
|
||||
char *dest = malloc(sizeof(char) * strlen(src));
|
||||
strcpy(dest, src);
|
||||
return dest;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
|
||||
void indent() {
|
||||
int i;
|
||||
for( i = 0; i < indent_xml; i++ ) {
|
||||
printf( " " );
|
||||
}
|
||||
}
|
||||
/*-------------------------------------------------------------------------*/
|
||||
void affiche_balise_ouvrante(const char *fct_, int trace_xml) {
|
||||
if( trace_xml ) {
|
||||
indent();
|
||||
indent_xml += indent_step ;
|
||||
fprintf (stdout, "<%s>\n", fct_);
|
||||
}
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
|
||||
void affiche_balise_fermante(const char *fct_, int trace_xml) {
|
||||
if(trace_xml) {
|
||||
indent_xml -= indent_step ;
|
||||
indent();
|
||||
fprintf (stdout, "</%s>\n", fct_);
|
||||
}
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
|
||||
void affiche_texte(char *texte_, int trace_xml) {
|
||||
if(trace_xml) {
|
||||
indent();
|
||||
fprintf (stdout, "%s\n", texte_);
|
||||
}
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
|
||||
void affiche_xml_texte( char *texte_ ) {
|
||||
int i = 0;
|
||||
while( texte_[ i ] != '\0' ) {
|
||||
if( texte_[ i ] == '<' ) {
|
||||
fprintf( stdout, "<" );
|
||||
}
|
||||
else if( texte_[ i ] == '>' ) {
|
||||
fprintf( stdout, ">" );
|
||||
}
|
||||
else if( texte_[ i ] == '&' ) {
|
||||
fprintf( stdout, "&" );
|
||||
}
|
||||
else {
|
||||
putchar( texte_[i] );
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
|
||||
void affiche_element(char *fct_, char *texte_, int trace_xml) {
|
||||
if(trace_xml) {
|
||||
indent();
|
||||
fprintf (stdout, "<%s>", fct_ );
|
||||
affiche_xml_texte( texte_ );
|
||||
fprintf (stdout, "</%s>\n", fct_ );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user