Librairie languages

Description

Salut

Apres de longues heures de travail je viens de creer un systeme de multilangues en C.
Je poste ici pour avoir votre avis.

Source / Exemple :


/*
main.c

Author: AstroProduction

Goal: main directions

Created:20/04/2010
Last Updated: 23/04/2010

Copyright 2010. All rights reserved to AstroProduction
astro567@hotmail.com
User Rights:
    All Rights, just don't delete the headers.

  • /
#include "config.h" int main (int argc, char *argv[]) { char lang[3]="en"; int numberFiles=langCountFiles(lang); LANG_LINE files[numberFiles]; langCountLines(lang,numberFiles,files); langCountC(lang,numberFiles,files); langShowContent(numberFiles,files); langFree(numberFiles,files); return EXIT_SUCCESS; }
</secret>

/*
lang.c

Author: AstroProduction

Goal: All functions about the languages.

Created:20/04/2010
Last Updated: 23/04/2010

Copyright 2010. All rights reserved to AstroProduction
astro567@hotmail.com
User Rights:
All Rights, just don't delete the headers.
  • /


#include "config.h"
#include "lang.h"

int langCountFiles(char* lang)
{
int fileLength=0;
char *path=langInitPath(lang);
DIR *pdir=langOpen(path);
if(pdir!=NULL)
{
struct dirent *pent;
while(pent=readdir(pdir))
{
if(isalnum(pent->d_name[0]))
{
if(strchr(pent->d_name,'.')) fileLength++;
}
}
closedir(path);
}
free(path);
return fileLength;
}

char* langInitPath(char* lang)
{
int pathLength=strlen(LANG)+strlen(lang);
char *path=(char*)malloc(pathLength*sizeof(path));
if(path==NULL)
{
fprintf(stderr,"Can't allowed memory to path (file:lang.c line:44)\n");
exit(EXIT_FAILURE);
}
pathLength=sprintf(path,LANG"%s",lang);
return path;
}

DIR* langOpen(char* path)
{
DIR *pdir;
if(!(pdir=opendir(path)))
{
fprintf(stderr,"Can't open %s. Check it exist and try again. (file:lang.c line:56)\n",path);
return NULL;
}
return pdir;
}

void langCountLines(char* lang,int numberFiles,LANG_LINE files[])
{
int j=0;
char *path=langInitPath(lang);
DIR *pdir=langOpen(path);
if(pdir!=NULL)
{
struct dirent *pent;
while(pent=readdir(pdir))
{
char *fullPath=malloc((strlen(path)+strlen(pent->d_name))*sizeof(char*));
if(!fullPath)
{
fprintf(stderr,"Can't allowed memory to fullPath (file:lang.c line:75)\n");
exit(EXIT_FAILURE);
}
sprintf(fullPath,"%s\\%s",path,pent->d_name);
if(isalnum(pent->d_name[0]))
{
if(strchr(pent->d_name,'.'))
{
FILE *file=fopen(fullPath,"r");
if(file==NULL)
{
fprintf(stderr,"Can't open file %s (file:lang.c line:86)\n",fullPath);
exit(EXIT_FAILURE);
}
files[j].fileName=malloc(pent->d_namlen*sizeof(char));
if(files[j].fileName==NULL)
{
fprintf(stderr,"Can't allowed memory for fileName (file:lang.c line:92)\n");
exit(EXIT_FAILURE);
}
strcpy(files[j].fileName,pent->d_name);
files[j].numberLines=0;
files[j].maxLengthLine=0;
int c=0,maxLength=0;
while(c!=EOF)
{
c=fgetc(file);
maxLength++;
if(c=='\n')
{
if(maxLength > files[j].maxLengthLine)
files[j].maxLengthLine=maxLength;
maxLength=0;
(files[j].numberLines)++;
}
}
fclose(file);
j++;
}
}
free(fullPath);
}
closedir(path);
}
free(path);
}

//*
void langCountC(char lang[],int numberFiles,LANG_LINE files[])
{
char *path=langInitPath(lang);
DIR *pdir=langOpen(path);
int j=0,i=0;
if(pdir!=NULL)
{
struct dirent *pent;
while(pent=readdir(pdir))
{
char *fullPath=malloc((strlen(path)+strlen(pent->d_name))*sizeof(char*));
if(!fullPath)
{
fprintf(stderr,"Can't allowed memory to fullPath (file:lang.c line:136)\n");
exit(EXIT_FAILURE);
}
sprintf(fullPath,"%s\\%s",path,pent->d_name);
if(isalnum(pent->d_name[0]))
{
if(strchr(pent->d_name,'.'))
{
FILE *file=fopen(fullPath,"r");
if(file==NULL)
{
fprintf(stderr,"Can't open file %s (file:lang.c line:147)\n",fullPath);
exit(EXIT_FAILURE);
}
int c=0;
files[j].contentLine=malloc((files[j].maxLengthLine+1)*sizeof(char));
if(!(files[j].contentLine))
{
fprintf(stderr,"Can't allow memory (file:lang.c line:154)\n");
exit(EXIT_FAILURE);
}
for(i=0;i<=files[j].numberLines;i++)
{
files[j].contentLine[i]=malloc((files[j].maxLengthLine+1)*sizeof(char*));
}
i=0;
while(fgets(files[j].contentLine[i],files[j].maxLengthLine+1,file)!=NULL)
{
files[j].contentLine[i][strlen(files[j].contentLine[i])-1]='\0';
i++;
}
fclose(file);
j++;
}
}
free(fullPath);
}
closedir(path);
}
free(path);
}

void langShowContent(int numberFiles,LANG_LINE files[])
{
int i=0,j=0;
for(j=0;j<numberFiles;j++)
{
fprintf(stdout,"%s:\n",files[j].fileName);
for(i=0;i<files[j].numberLines;i++)
fprintf(stdout,"%d:%d => %s\n",j,i,files[j].contentLine[i]);
fprintf(stdout,"\n");
}
}

void langFree(int numberFiles,LANG_LINE files[])
{
int i=0,j=0;
for(j=0;j<numberFiles;j++)
{
for(i=0;i<files[j].numberLines;i++)
free(files[j].contentLine[i]);
free(files[j].contentLine);
free(files[j].fileName);
}
}

/*
lang.h

Author: AstroProduction

Goal: Prototypes of lang.c

Created:20/04/2010
Last Updated: 23/04/2010

Copyright 2010. All rights reserved to AstroProduction
astro567@hotmail.com
User Rights:
All Rights, just don't delete the headers.
  • /


#ifndef LANGH_
#define LANGH_

/*
@struct LANG_LINE
@parameters:
@int numberLines: number of lines in a file;
@int maxLengthLine: line longest length in all the file;
@char **contentLine: contain the sentence of the line;
  • /

typedef struct{
int numberLines;
int maxLengthLine;
char **contentLine;
char *fileName;
}LANG_LINE;

/*
@function langCountFiles
@return: int: number of files
@parameters:
@char* lang: the language name in two letters (eg:"en" for English)
@steps:
@init path;
@open the language directory
@read the directory
@count number of files
@close directory
@free memory
  • /

int langCountFiles(char* lang);

/*
@function langInitPath
@return char*: full directory to the language folder
@parameters:
@char* lang: the language name in two letters (eg:"en" for English)
@steps:
@count the length of the path
@write the path
  • /

char* langInitPath(char* lang);

/*
@function langOpen
@return DIR*: pointer to the directory or NULL
@parameters:
@char* path: path to the directory concerned
@steps:
@open folder: fail return NULL, success return pointer to the folder
  • /

DIR* langOpen(char* path);

/*
@function langCountLines
@return void
@parameters:
@char* lang: the language name in two letters (eg:"en" for English)
@int numberFiles: number of files in the folder
@LANG_LINE (*files)[]: pointer to the struct
@steps:
@init path;
@open the language directory
@read the directory
@get fullpath
@check file name (First lette is alnum and contain an extension)
@open file
@count the line that have the max length
@count number of lines
@close and free all.
  • /

void langCountLines(char* lang,int numberFiles,LANG_LINE files[]);

/*
@function langCountC
@return void
@parameters:
@char* lang: the language name in two letters (eg:"en" for English)
@int numberFiles: number of files in the folder
@LANG_LINE (*files)[]: pointer to the struct
@steps:
@init path;
@open the language directory
@read the directory
@get fullpath
@check file name (First lette is alnum and contain an extension)
@open file
@allow memory for file[j].content[i]
@write content in file[j].content[i]
@close and free all.
  • /

void langCountC(char lang[],int numberFiles,LANG_LINE files[]);

/*
@function langShowContent
@return void
@parameters:
@int numberFiles: number of files in the folder
@LANG_LINE (*files)[]: pointer to the struct
@steps:
@Write the content with a loop
  • /

void langShowContent(int numberFiles,LANG_LINE files[]);

/*
@function langFree
@return void
@parameters:
@int numberFiles: number of files in the folder
@LANG_LINE (*files)[]: pointer to the struct
@steps:
@Free the content with a loop
  • /

void langFree(int numberFiles,LANG_LINE files[]);

#endif

/*
config.h

Author: AstroProduction

Goal: General definitions for all the application

Created:20/04/2010
Last Updated: 20/04/2010

Copyright 2010. All rights reserved to AstroProduction
astro567@hotmail.com
User Rights:
All Rights, just don't delete the headers.
  • /


#ifndef CONFIGH_
#define CONFIGH_

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <dirent.h>
#include "lang.h"

#define LANG "languages\\"

#endif

/*
versions.c

Author: AstroProduction

Goal: Resume all modifications

Created:23/04/2010
Last Updated: 23/04/2010

Copyright 2010. All rights reserved to AstroProduction
astro567@hotmail.com
User Rights:
All Rights, just don't delete the headers.

Version: 1.0

=> System lang completed
  • /


utilisation:
Definition du repertoire contenant les langues dans config.h
Definition de la langues dans la variable langues tel que defini dans le main.c (string de 2 lettres)
Il faut avoir creer le repertoire contenant les langues et les sous repertoires de langues: languages\\en\\ dans mon cas.

La fonction langShowContent ecris dans le stdout le nom du fichier les nombres files et content line respectivement ainsi que le contenu de la lignes correspondant.
Par exemple:
main.txt
0:0 Bienvenue
0:1 A bientot


Pour afficher "A bientot" dans l'application il suffira d'<taille valeur="gros">ecrire avant la fonction langFree</taille>:
<secret>
printf("%s",files[0].contentLine[1]);
</secret>

J'espere que mes explications sont assez clair.
Merci pour vos reactions a l'avance.

A bientot
Astro
</code>

Conclusion :


utilisation:
Definition du repertoire contenant les langues dans config.h
Definition de la langues dans la variable langues tel que defini dans le main.c (string de 2 lettres)
Il faut avoir creer le repertoire contenant les langues et les sous repertoires de langues: languages\\en\\ dans mon cas.

La fonction langShowContent ecris dans le stdout le nom du fichier les nombres files et content line respectivement ainsi que le contenu de la lignes correspondant.
Par exemple:
main.txt
0:0 Bienvenue
0:1 A bientot

Pour afficher "A bientot" dans l'application il suffira d'ecrire avant la fonction langFree:

printf("%s",files[0].contentLine[1]);

J'espere que mes explications sont assez clair.
Merci pour vos reactions a l'avance.

A bientot
Astro

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.