Files
compilateur-l/src/util.c
2016-04-01 10:59:35 +02:00

103 lines
2.5 KiB
C

#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) + 1));
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, "&lt;" );
}
else if( texte_[ i ] == '>' ) {
fprintf( stdout, "&gt;" );
}
else if( texte_[ i ] == '&' ) {
fprintf( stdout, "&amp;" );
}
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_ );
}
}