voici une fonction permettant de générer un chemin relatif a partir de chemins en absolu
BString BFichier::cheminRelatifADe( BString nomLongRef, BString nomLongVoulu)
{
/// -----------------------------------------------------------------------------------------------------------------
/// ---------------- BFichier::cheminRelatifADe(BString nomLongRef ,BString nomLongVoulu) -> BString ----------------
/// -----------------------------------------------------------------------------------------------------------------
/// ----- Objectif : donne le chemin d'un fichier en relatif
/// ----- Auteur(s) : Bruno CELLE 06/05/03
/// ----- PreCond : /
/// ----- PostCond : /
/// ----- Etat :
2
(-1<0<1<2)
/// -----------------------------------------------------------------------------------------------------------------
/// ----- BString nomLongRef : reférant
/// ----- BString nomLongVoulu : fichier voulu
/// -----------------------------------------------------------------------------------------------------------------
/// ----- retour (BString) : cf.obj
/// -----------------------------------------------------------------------------------------------------------------
/// ----- Var Muettes (cf.partie préc) (2) : nomLongRef ,nomLongVoulu
/// ----- Var Internes à la fonction (7) : cheminRef ,cheminV ,encore ,i ,maxi ,p ,rep
/// ----- Var Utilisées par adresse (2) : cheminRef ,cheminV
/// ----- Var In (2) : nomLongRef ,nomLongVoulu
BString cheminRef=extraitChemin(nomLongRef);
BString cheminV=extraitChemin(nomLongVoulu);
unsigned int i,p=0,maxi;
//anormal, max des stl n'arrive po a etre appelé sous devC++
#ifdef BVisuel
maxi=max(cheminRef.getLength(), cheminV .getLength());
#else /* BVisuel */
maxi=(cheminRef.getLength()> cheminV .getLength())
? cheminRef.getLength()
: cheminV .getLength();
#endif /* BVisuel */
//check lecteur
if(maxi<2 || cheminRef[1]!=cheminV[1])
PB1("cheminRelatifDeA - Ce ne sont pas des chemins qui sont transmis à la fonction!");
if(cheminRef[0]!=cheminV[0])
return nomLongVoulu;
bool encore=true;
for(i=0;encore && i<maxi;i++)
{
// encore=(cheminRef.getAt(i)==cheminV.getAt(i));
//
if(encore && (cheminRef.getAt(i)=='\\' ||
cheminRef.getAt(i)=='/')) //TODO : simplification en
normalisant !!
encore=(cheminRef[i]==cheminV[i]);
if(encore &&
(cheminRef[i]=='\\' || cheminRef[i]=='/')) //TODO :
simplification en normalisant !!
p=i;
}
cheminRef.depuisSelf(p);
cheminV.depuisSelf(p);
BString rep="";
for(i=0;i<(cheminRef.getLength()-1);i++)
if(cheminRef[i]=='\\' ||
cheminRef[i]=='/' ) //TODO : simplification en
normalisant !!
// if(cheminRef.getAt(i)=='\\' ||
cheminRef.getAt(i)=='/' ) //TODO : simplification en
normalisant !!
{
rep+="..";
rep+=cheminRef[i];
//rep+=cheminRef.getAt(i);
}
if(cheminV.getLength()>1)
rep+=cheminV.depuis(1);
rep+=extraitNom(nomLongVoulu);
return rep;
}
___________________________________________________________
MagicalementNono
avec ces sous fonctions triviales:
BString BFichier::extraitNom( const BString& nomLong)
{
/// -----------------------------------------------------------------------------------------
/// ---------------- BFichier::extraitNom(const BString& nomLong) -> BString ----------------
/// -----------------------------------------------------------------------------------------
/// ----- Objectif : extraire le nom d'un fichier depuis le nom long
/// ----- Auteur(s) : Bruno CELLE 26/04/03
/// ----- PreCond : gère séparateurs '/' et '\\' mais suppose qu'ils ne sont pas <<mélés>>
/// ----- PostCond : /
/// ----- Etat :
2
(-1<0<1<2)
/// ----- MaJ 15/01/05 : réécriture
/// -----------------------------------------------------------------------------------------
/// ----- const BString& nomLong : cf.nom
/// -----------------------------------------------------------------------------------------
/// ----- retour (BString) : le nom et l'extention
/// -----------------------------------------------------------------------------------------
/// ----- Var Muettes (cf.partie préc) (1) : nomLong
/// ----- Var Internes à la fonction (1) : plomp
/// ----- Var In (1) : nomLong
BString plomp=nomLong.getApresIemeSeparateur(-1,'\\',false);
if(!plomp.getLength())
plomp=nomLong.getApresIemeSeparateur(-1,'/',false);
//getApresIemeSeparateur retourne vide si inexistance...
if(!plomp.getLength())
return nomLong;
return plomp;
}
BString BFichier::extraitChemin( BString nomLong)
{
/// -------------------------------------------------------------------------------------
/// ---------------- BFichier::extraitChemin(BString nomLong) -> BString ----------------
/// -------------------------------------------------------------------------------------
/// ----- Objectif : extraire le chemin d'un fichier depuis le nom long
/// ----- Auteur(s) : Bruno CELLE 26/04/03
/// ----- PreCond : /
/// ----- PostCond : /
/// ----- Etat :
2
(-1<0<1<2)
/// ----- MaJ 16/12/03 : prise en cpte de l'absence de chemin...
/// ----- MaJ 15/01/05 : réécriture
/// -------------------------------------------------------------------------------------
/// ----- BString nomLong : fichier de référence
/// -------------------------------------------------------------------------------------
/// ----- retour (BString) : le chemin avec le '\\' final
/// -------------------------------------------------------------------------------------
/// ----- Var Muettes (cf.partie préc) (1) : nomLong
/// ----- Var Internes à la fonction (1) : plomp
/// ----- Var In (1) : nomLong
normaliseNom(&nomLong);
BString plomp=nomLong.getAvantIemeSeparateur(-1,'\\',true);
//getAvantIemeSeparateur retourne tt si inexistance...
if(plomp==nomLong) //plomp forcément différent si c'est la rép demandée
return"";
return plomp;
}