// conversion d'un source C en fichier HTML


/*
lien avec une feuille de style (css)
premiere ligne du source = titre
commentaires multi-lignes colorés (pas de test pour detecter
	le fait que le signe commentaire soit au sein d'une chaine)
 */

#include "stdio.h"
#include <string.h>

 
/*
 *********************
*conversion du source
 *********************
**/
void convert_html(char repto[],char repfrom[],char arg[])
	{
	FILE  *pfin, *pfout;
	char nomin[64],nomout[64],titre[];
	char ch=NULL; char prevch=NULL;
	int commentaire = 0,commentligne=0;
	int i_titre =0;

        printf("\nConversion du programme %s",arg);

	strcpy(nomin,"");
        strcat(nomin,repfrom);
        strcat(nomin,arg);
	strcat(nomin,".cpp");

	strcpy(nomout,"");
	strcat(nomout,repto);
        strcat(nomout,arg);
	strcat(nomout,".htm");

	pfin=fopen(nomin,"r");
	pfout=fopen(nomout,"w");
	printf("\nfichiers %s et %s",nomin,nomout);

        if (pfin==NULL) {printf("\n ***pb acces au fichier in %s",nomin);};
	if (pfout==NULL) {printf("\n ***pb acces au fichier out %s", nomout);};

	if ((pfin!=NULL)&&(pfout!=NULL))
        {
	printf("\nfichier in %s",nomin);
	printf("\nfichier out %s", nomout);

	fprintf(pfout,"<html><head><title>Cours C< /title>");
	fprintf(pfout,"<link rel=\"stylesheet\" href=\"styles.css\" type=\"text /css\">");
	fprintf(pfout,"< /head><body><h1>");

       
/* la premiere ligne est une ligne de titre  */
	ch = fgetc(pfin);
   	while (ch != '\n')
		{fputc(ch,pfout);
	 	ch = fgetc(pfin);};
   	fprintf(pfout,"< /h1><pre class=\"normal\">");
   
   while (ch != EOF)
   {
       
/* traitement du texte  */
      switch (ch)
        {
	case ' *':if (prevch==' /')  // debut de commentaire long
			{fprintf(pfout,"< /pre><pre class=\"comment\"> 
/*");
			commentaire=1;prevch=' ';}
		 else {fputc(prevch,pfout);prevch=ch;};
		 break;
	case ' /':
		switch (prevch)
		{
		case ' *':  / fin de commentaire
			if (commentaire==1) 
			{fprintf(pfout," */
< /pre><pre class=\"normal\">");
			commentaire=0;prevch=' ';};break;
		case ' /':  // debut de commentaire ligne
			if ((commentaire!=1)&&(commentligne==0)) 
			{fprintf(pfout,"<i class=\"comment\"> //");
			commentligne=1;prevch=' ';};break;
		default:fputc(prevch,pfout);prevch=ch;break;
		}
		break;
        case '<':fprintf(pfout,"%s","<");break;
        case '>':fprintf(pfout,"%s",">");break;
	case '\n':
		if (commentligne==1)  // fin de commentaire ligne
			{fprintf(pfout,"< /i>");
			commentligne=0;prevch=' ';};
		fputc(ch,pfout);break;
	default:
		if (prevch!=' ') {fputc(prevch,pfout);prevch=' ';};
		fputc(ch,pfout);break;
	};

      

       
/* read a char from the file  */
      ch = fgetc(pfin);

   };

	fprintf(pfout,"< /pre>< /body>< /html>");
     };  //
	fclose(pfout);
	fclose(pfin);

};


 
/*
 ********************
*programme principal
 ********************
**/
void main()
{       
	char repto[64],repfrom[64];
	char c=' ';
	char nomfic[];
	strcpy(repfrom,"f:\\autocad\\cpp\\");
	strcpy(repto,"f:\\siteweb\\cours\\c_src\\");
	printf("\nDebut programme");

	while (nomfic[0]!=' ')
        {
	printf("\nEntrez un nom de fichier (ctrl&C pour terminer) :");
	scanf("%s",nomfic);
	convert_html(repto,repfrom,nomfic);
	};
	printf("\nFin programme\n");
	
};