1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
#include <stdio.h>
#include <fstream>
const int MAX = 256; // la longueur max d'une ligne
/****************/
/* CONSTRUCTEUR */
/****************/
Fusion :: Fusion ( const char * filename ) {
this->filename = filename;
string file(filename);
fichierComplet = new char [file.length() + 2];
strcpy ( fichierComplet, filename );
strcat ( fichierComplet, ".x" );
}
/*************************************************************/
/* RENVOIE LE NOM DU FICHIER CONTENANT TOUTES LES ANIMATIONS */
/*************************************************************/
char * Fusion :: getFichierComplet ( ) {
FILE * fp = fopen (fichierComplet, "r");
// si le fichier existe on renvoie le nom du fichier
if (fp != NULL )
fclose(fp);
// sinon on fusionne les fichiers dont le nom commence par filename
else
fusionne( );
return fichierComplet;
}
/************************************************************************************/
/* FUSIONNE TOUS LES FICHIERS DONT LE NOM COMMENCE PAR FILENAME ( SAUF FILENAME.X ) */
/************************************************************************************/
char * Fusion :: fusionne ( ) {
// recherche du nom du repertoire dans lequel se trouvent les fichiers
string file(filename);
int index = file.find_last_of("\\");
string chemin = file.substr(0,index+1);
// recherche des fichiers x commencant par filename
char recherche [file.length() + 3];
strcpy(recherche, filename);
strcat(recherche, "*.x");
WIN32_FIND_DATA File;
HANDLE hSearch;
hSearch = FindFirstFile(recherche, &File);
if (hSearch == INVALID_HANDLE_VALUE){
throw Exception("Pas de fichier trouve");
}
//cout<<"FUSION\n";
BOOL result = TRUE;
int passage = 0;
do {
// recuperation du nom de fichier
char fichierX [ chemin.length() + strlen(File.cFileName) ];
strcpy ( fichierX, chemin.c_str() );
strcat ( fichierX, File.cFileName );
// s'il s'agit de fichierComplet, on l'oublie
//(il ne doit pas etre fusionne avec les autres car c'est lui qui recoit la fusion )
if ( strcmp( fichierX, fichierComplet ) == 0 ){
result = FindNextFile(hSearch, &File);
continue;
}
cout<<fichierX<<endl;
// on recupere une seule fois les donnees concernant les meshs, textures, materiel ...
if ( passage == 0 ){
copieFichier ( fichierX );
passage ++;
}
// pour tous les autres fichiers, on ne recupere que les anims, presentes en fin de fichier
else
copieAnim ( fichierX );
// recherche du suivant
result = FindNextFile(hSearch, &File);
} while (result);
FindClose(hSearch);
}
/***********************************************************************/
/* COPIE DU FICHIER DONNE EN PARAMETRE DANS LE FICHIER FICHIERCOMPLET, */
/* QUI SERA ECRASE S'IL EXISTE */
/***********************************************************************/
void Fusion :: copieFichier( char * fileToAdd ){
ifstream lecture ( fileToAdd, ios::in);
ofstream ecriture ( fichierComplet, ios::out );
char buf[MAX];
while ( !lecture.eof() ) {
lecture.getline(buf, MAX);
ecriture << buf << endl;
}
ecriture.close();
lecture.close();
}
/**********************************************************************************/
/* COPIE DES ANIMATIONS DU FICHIER DONNE EN PARAMETRE, A LA FIN DE FICHIERCOMPLET */
/**********************************************************************************/
void Fusion :: copieAnim( char * fileToAdd ){
ifstream lecture ( fileToAdd, ios::in);
ofstream ecriture ( fichierComplet, ios::ate );
char buf[MAX];
bool copy = false;
// les animations du fichiers x se trouvent a la fin du fichier.
// Nous lisons donc tout le fichier jusqu'a rencontrer la chaine "AnimationSet",
// et alors nous copions toutes les lignes suivantes jusqu'a la fin du fichier
while ( !lecture.eof() ) {
lecture.getline(buf, MAX);
string lu (buf);
if ( lu.find ( "AnimationSet" ) != string::npos ) // si la ligne contient "AnimationSet"
copy = true;
if (copy == true )
ecriture << buf << endl;
}
ecriture.close();
lecture.close();
} |
Partager