[TUTO]Dini.inc
#1


by Cristab

Bonjour avant de commencer vous devrez telecharger l'include Dini sur le site de sont createur DracoBlue (http://dracoblue.com)

Voila une chose faite placer dans pawno/include

pour nous en servir il va falloir l'indiquer au compilot

Code:
#include <a_samp>
#include <Dini>
tout simplement.

Desormer ouvrez le avec bloc note ou notepad comme bon vous semble et regardons cette include

premiиre fonction

Code:
stock dini_Exists(filename[]) {
	return fexist(filename);
}
la on peu remarquer que sa retourne juste a une fonction par default le fexist cf(voir le tuto de Syg Les fonctions de gestion des fichiers) Donc je ne me sens pas dans l'obligation de vous expliquer cette fonction.

seconde fonction

Code:
stock dini_Remove(filename[]) {
	return fremove(filename);
}
Tout comme la fonction precedente rйfйrer vous au tuto de Syg.

troisieme fonction


Code:
stock dini_Create(filename[]) {
	if (fexist(filename)) return false;
	new File:fhnd;
	fhnd=fopen(filename,io_write);
	if (fhnd) {
		fclose(fhnd);
		return true;
	}
	return false;
}
Ah enfin une vrais fonction ^^ comme son nom l'indique c'est pour creer un fichier exemple dini_Creat("Monfichier.txt");

votre fichier est creer.

Mais comment etre sur qu'il est creer si je suis en jeu et que je ne peu pas regarder ma console.

Je vous donne un petit truc biensur c'est juste pour que vous testiez

Code:
public OnPlayerCommandText(playerid, cmdtext[])
{
	if (strcmp("/fichier", cmdtext, true) == 0)
	{
		if(fexist("Monfichier.txt"))
		{
			SendClientMessage(playerid,0x00FF33AA,"Fichier la");
		}
		else
		{
			SendClientMessage(playerid,0xFF0000FF,"Fichier pas la");
		}
		return 1;
	}
	return 0;
}

quatriиme fonction


Code:
stock dini_Set(filename[],key[],value[]) {
	// If we have no key, it can't be set
	// we also have no chance to set the value, if all together is bigger then the max string
	new key_length = strlen(key);
	new value_length = strlen(value);
	if (key_length==0 || key_length+value_length+2>DINI_MAX_STRING) return false;

	new File:fohnd, File:fwhnd;
	new tmpres[DINI_MAX_STRING];
	new bool:wasset=false;

	// Let's remove the old *.part file if there was one.
	format(tmpres,sizeof(tmpres),"%s.part",filename);
	fremove(tmpres);

	// We'll open the source file.
	fohnd=fopen(filename,io_read);
	if (!fohnd) return false;

	fwhnd=fopen(tmpres,io_write);
	if (!fwhnd) {
		// we can't open the second file for writing, so .. let's close the open one and exit.
		fclose(fohnd);
		return false;
	}

	while (fread(fohnd,tmpres)) {
		if (
			!wasset
			&& tmpres[key_length]=='='
			&& !strcmp(tmpres, key, true, key_length)
		) {
				// We've got what needs to be replaced!
				format(tmpres,sizeof(tmpres),"%s=%s",key,value);
				wasset=true;
		} else {
			DINI_StripNewLine(tmpres);
		}
		fwrite(fwhnd,tmpres);
		fwrite(fwhnd,"\r\n");
	}

	if (!wasset) {
		format(tmpres,sizeof(tmpres),"%s=%s",key,value);
		fwrite(fwhnd,tmpres);
		fwrite(fwhnd,"\r\n");
	}

	fclose(fohnd);
	fclose(fwhnd);

	format(tmpres,sizeof(tmpres),"%s.part",filename);
	if (DINI_fcopytextfile(tmpres,filename)) {
		return fremove(tmpres);
	}
	return false;
}
Aoutch!!!! en vla une fonction ^^

Ce n'est rien sa va uniquement servire a stocker par exemple un mot ou un pseudo dini_Set("Monfichier.txt","test1","je suis un test");
vous trouverez ceci dans le fichier Monfichier.txt
Quote:

test1=je suis un test

n'est ce pas magique

cinquieme fonction

Code:
stock dini_IntSet(filename[],key[],value) {
   new valuestring[DINI_MAX_STRING];
   format(valuestring,DINI_MAX_STRING,"%d",value);
   return dini_Set(filename,key,valuestring);
}
cette fonction va servire a stoker la valeur d'un variable
Code:
dini_IntSet("Monfichier.txt","test2",56);
vous trouverez ceci dans le fichier Monfichier.txt
Quote:

test2=56

sixieme fonction

Code:
stock dini_Int(filename[],key[]) {
   return strval(dini_Get(filename,key));
}
cette fonction va nous servire a recuperer la variable a la ligne test2

Code:
new mavar;
mavar = dini_Int("Monfichier.txt","test2");
printf("%d",mavar);
ce code devrais vous donnez le numero 56

septiиme fonction

Code:
stock dini_FloatSet(filename[],key[],Float:value) {
   new valuestring[DINI_MAX_STRING];
   format(valuestring,DINI_MAX_STRING,"%f",value);
   return dini_Set(filename,key,valuestring);
}
cette fonction va vous permettre de stocker un Float soit une position ce qui est le plus utiliser dini_FloatSet("Monfichier.txt","test3",1.235);

ce qui vous donneras dans le fichier Monfichier.txt
Quote:

test3=1.235

huitiиme fonction

Code:
stock Float:dini_Float(filename[],key[]) {
   return floatstr(dini_Get(filename,key));
}
cette fonction va nous servire a recupere un float prйcedement enregistrer dans un fichier

exemple

Code:
new Float:mapos;
mapos = dini_Float("Monfichier.txt","test3");
printf("test 3 %f",mapos);
neuviиme fonction

Code:
stock dini_Bool(filename[],key[]) {
   return strval(dini_Get(filename,key));
}
celle ci tres peu utiliser sert a recuperer une boolйenne c'est a dire 1 ou 0 (voir prochaine fonction)

dixiиme fonction

Code:
stock dini_BoolSet(filename[],key[],value) {
	if (value) {
		return dini_Set(filename,key,"1");
	}
	return dini_Set(filename,key,"0");
}
voila la fonction qui va permettre de sauver une boolйenne exemple dini_BoolSet("Monfichier.txt","test4",0); ici il enregistreras 0 par contre si je met la valeur a 1 il mettras 1 mais je ne peu pas mettre 2 ou autre chose dini_BoolSet("Monfichier.txt","test5",1);

pour verifier ceci

Code:
new bool1;
new bool2;
bool1=dini_Bool("Monfichier.txt","test4") ;
bool2=dini_Bool("Monfichier.txt","test5") ;
printf("bool1=%d   bool2=%d",bool1,bool2);
Onziиme fonction

Code:
stock dini_Unset(filename[],key[]) {
	// If we have no key, it can't be set
	// we also have no chance to unset the key, if all together is bigger then the max string
	new key_length = strlen(key);
	if (key_length==0 || key_length+2>DINI_MAX_STRING) return false;

	new File:fohnd, File:fwhnd;
	new tmpres[DINI_MAX_STRING];

	// Let's remove the old *.part file if there was one.
	format(tmpres,DINI_MAX_STRING,"%s.part",filename);
	fremove(tmpres);

	// We'll open the source file.
	fohnd=fopen(filename,io_read);
	if (!fohnd) return false;

	fwhnd=fopen(tmpres,io_write);
	if (!fwhnd) {
		// we can't open the second file for writing, so .. let's close the open one and exit.
		fclose(fohnd);
		return false;
	}

	while (fread(fohnd,tmpres)) {
		if (
			tmpres[key_length]=='='
			&& !strcmp(tmpres, key, true, key_length)
		) {
				// We've got what needs to be removed!
		} else {
			DINI_StripNewLine(tmpres);
			fwrite(fwhnd,tmpres);
			fwrite(fwhnd,"\r\n");
		}
	}

	fclose(fohnd);
	fclose(fwhnd);

	format(tmpres,DINI_MAX_STRING,"%s.part",filename);
	if (DINI_fcopytextfile(tmpres,filename)) {
		return fremove(tmpres);
	}
	return false;
}
cette fonction sert a retirer une ligne d'un fichier exemple dini_Unset("Monfichier.txt","test4");

voila la ligne 4 est supprimer du fichier.

douziиme fonction


Code:
stock dini_Get(filename[],key[]) {
	new tmpres[DINI_MAX_STRING];

	new key_length = strlen(key);
	if (key_length==0 || key_length+2>DINI_MAX_STRING) return tmpres;

	new File:fohnd;
	fohnd=fopen(filename,io_read);
	if (!fohnd) return tmpres;

	while (fread(fohnd,tmpres)) {
		if (
			tmpres[key_length]=='='
			&& !strcmp(tmpres, key, true, key_length)
		) {
			/* We've got what we need */
			DINI_StripNewLine(tmpres);
			strmid(tmpres, tmpres, key_length + 1, strlen(tmpres), DINI_MAX_STRING);
			fclose(fohnd);
			return tmpres;
		}
	}
	fclose(fohnd);
	return tmpres;
}
Voila la fonction qui va nous permettre de recuperer notre phrase a la ligne test comme ci dessous

Code:
new txt[256];
txt = dini_Get("Monfichier.txt","test1");
printf("ma phrase ligne test1 est = %s",txt);
treiziиme fonction

Code:
stock dini_Isset(filename[],key[]) {
	new key_length = strlen(key);
	if (key_length==0 || key_length+2>DINI_MAX_STRING) return false;

	new File:fohnd;
	fohnd=fopen(filename,io_read);
	if (!fohnd) return false;

	new tmpres[DINI_MAX_STRING];
	while (fread(fohnd,tmpres)) {
		if (
				tmpres[key_length]=='='
			&&  !strcmp(tmpres, key, true, key_length)
		) {
			// We've got what we need
			fclose(fohnd);
			return true;
		}
	}
	fclose(fohnd);
	return false;
}
Voila une fonction qui va permettre de verifier si tel ligne existe dans le fichier exemple

Code:
if(dini_Isset("Monfichier.txt","ligne") )
{
   print("la ligne exist");
}
else
{
   print("la ligne exist pas");
}
a vous de tester


Les fonction interne a l'include mais qui peuvent peu etre vous servire

Code:
stock DINI_StripNewLine(string[]) {
	new len = strlen(string);
	if (string[0]==0) return ;
	if ((string[len - 1] == '\n') || (string[len - 1] == '\r')) {
		string[len - 1] = 0;
		if (string[0]==0) return ;
		if ((string[len - 2] == '\n') || (string[len - 2] == '\r')) string[len - 2] = 0;
	}
}

stock DINI_fcopytextfile(oldname[],newname[]) {
	new File:ohnd,File:nhnd;
	if (!fexist(oldname)) return false;
	ohnd=fopen(oldname,io_read);
	if (!ohnd) return false;
	nhnd=fopen(newname,io_write);
	if (!nhnd) {
		fclose(ohnd);
		return false;
	}
	new tmpres[DINI_MAX_STRING];
	while (fread(ohnd,tmpres)) {
		DINI_StripNewLine(tmpres);
		format(tmpres,sizeof(tmpres),"%s\r\n",tmpres);
		fwrite(nhnd,tmpres);
	}
	fclose(ohnd);
	fclose(nhnd);
	return true;
}
voila petite astuce vous desierez voir dini sur le coter comme les include par default ajouter ce qui suis a votre include

Quote:

native dini_Exists(filename[]);
native dini_Remove(filename[]);
native dini_Create(filename[]);
native dini_Set(filename[],key[],value[]);
native dini_IntSet(filename[],key[],value);
native dini_Int(filename[],key[]);
native dini_FloatSet(filename[],key[],Float:value);
native dini_Float(filename[],key[]);
native dini_Bool(filename[],key[]);
native dini_BoolSet(filename[],key[],value);
native dini_Unset(filename[],key[]);
native dini_Get(filename[],key[]);
native dini_Isset(filename[],key[]);
native DINI_StripNewLine(string[]);
native DINI_fcopytextfile(oldname[],newname[]);

juste apres
Quote:

#if defined MAX_STRING
#define DINI_MAX_STRING MAX_STRING
#else
#define DINI_MAX_STRING 255
#endif

et vous obtiendrez ceci


Voila je tenterais de repondre a vos question trouvez en piece jointe le fichier .pwn realiser le long de ce tuto.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)