#!/usr/bin/perl

# Occidental (Windows Latin 1)

sub litFichier
{
        local ($fichier) = @_;
        local ($texte);
        
        if (open (FICHIER, $fichier))
        {
                $mode = $/;
                undef $/;
                
                $texte = <FICHIER>;
                
                close (FICHIER);
                
                $/ = $mode;
        }
        else
        {
                die "Fichier $fichier introuvable. Traitement interrompu\n";
        }
        return $texte;
}

sub condense
{
	local ($_) = @_;

	s/[^a-zA-Z0-9]+//g;
	tr/a-z/A-Z/;

        return $_;
}

sub presenteISBN
{
	local ($numero) = @_;

	return  ($numero =~ /\A(\w)(\w{5})(\w\w\w)(\w)\Z/) ? "$1-$2-$3-$4" : $numero;
}

sub determineFichierRepartition
{
	local ($detailRepartition) = @_;
	local ($pratique, $theorie, $total, $nomFichier);

	$pratique = $theorie = -1;

	if ($detailRepartition =~ /p[^:]*:\s*(\d+)/i)
	{
		$pratique = $1;
	}
	if ($detailRepartition =~ /(\A|\s)t[^:]*:\s*(\d+)/i)
	{
		$theorie = $2;
	}
	$total = $pratique + $theorie;

	if ($total == -2)
	{
		$total = $theorie = 100;
		$pratique = 0;
	}
	if ($total != 100)
	{
		warn "ATTENTION : la repartition $pratique + $theorie ($detailRepartition) donne un total different de 100\n";
		$pratique = ($pratique * 100) / $total;
		$theorie = ($theorie * 100) / $total;
	}
	$pratique = arrondi ($pratique / 5) * 5;
	$theorie = arrondi ($theorie / 5) * 5;

	$nomFichier = sprintf ("%03d%03d", $pratique, $theorie);

	return $nomFichier;
}

sub arrondi
{
	local ($valeur) = @_;

	return (($valeur - valeurEntiere ($valeur)) < 0.5) ? valeurEntiere ($valeur) : valeurEntiere ($valeur) + 1;
}

sub valeurEntiere
{
        local ($valeur) = @_;
        local ($temp);

        $temp = sprintf ("%f", $valeur);
        $temp =~ s/\..*//;

        return ($temp);
}

sub isolatin2html
{
	local ($_) = @_;

	tr/\r\n//d;

	s/&/&amp;/g;
	s/"/&quot;/g;
	s/</&lt;/g;
	s/>/&gt;/g;
	s/À/&Agrave;/g;
	s/Á/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®/&#174;/g;
	s/©/&copy;/g;
#	s/©/&#169;/g;
	s/™/&#8482;/g;
	s/·/&middot;/g;
	s/·/&#183;/g;
	s/…/&#8230;/g;
	s/\.\.\./&#8230;/g;
	s/\A\* /<li>/;
	s/\t\t/<p>/g;
	s/\t/<br>/g;
	s/ ([:;!?])/&nbsp;$1/g;
	s/ ([\/-]) /&nbsp;$1 /g;

	return $_;
}

1;
