[Include] zini.inc - Simples escrita e leitura em arquivos
#1

Introduзгo:
Essa include serve pra quem precisa de um sistema de gerenciamento de arquivos, mas nгo necessariamente de tudo que os sistemas publicados atualmente oferecem (escrever int, escrever float, etc).

Simplesmente escreve e lк strings, claro que vocк pode obter valores tambйm (strval, floatstr).



Funзхes:
Код:
file_create(file[])
    - Cria um arquivo
    - Parвmetro file: Nome do arquivo.

file_delete(file[])
    - Deleta um arquivo.
    - Parвmetro file: Nome do arquivo

file_exists(file[])
    - Verifica se um arquivo existe.
    - Parвmetro file: Nome do arquivo

file_set(file[], tag[], value[])
    - Cria um arquivo
    - Parвmetro file: Nome do arquivo
    - Parвmetro tag: Tag para escrever
    - Parвmetro value: Texto para atribuir а tag

file_get(file[], tag[])
    - Cria um arquivo
    - Parвmetro file: Nome do arquivo
    - Parвmetro tag: Tag cujo valor serб retornado

Source:
pawn Код:
#include <a_samp>


#if defined zini_included
    #endinput
#endif

#define zini_included
#pragma library zini



stock buffer[0xffff];



/*

native file_create(file[]);
native file_delete(file[]);
native file_exists(file[]);

native file_set(file[], tag[], value[]);
native file_get(file[], tag[]);

*/




#define file_create(%0)     fclose(fopen(%0, io_write))
#define file_delete(%0)     fremove(%0)
#define file_exists(%0)     fexist(%0)



stock file_set(file[], tag[], value[]) {

    if(strlen(tag) + strlen(value) + 2 > 255)
        return 0;
       
    if(!fexist(file))
        goto write;
       
    buffer[0] = EOS;
       
    static string[0xff], index;
   
    static File:handle;
    handle = fopen(file, io_read);

    while(fread(handle, string)) {
   
        if((index = strfind(string, "=", false)) != -1) {
       
            string[index] = EOS;
           
            if(strcmp(string, tag, false)) {
           
                string[index] = '=';
                strcat(buffer, string);
            }
        }
    }
   
    fclose(handle);

    write:

    format(string, sizeof string, "%s=%s\r\n", tag, value);
    strcat(buffer, string);
       
    handle = fopen(file, io_write);

    fwrite(handle, buffer);
    fclose(handle);
    return 1;
}



stock file_get(file[], tag[]) {

    static string[0xff];
    string[0] = EOS;
   
    if(!fexist(file) || !strlen(tag))
        return string;
       
    static
        File:handle,
        line[0xff], i;
       
    handle = fopen(file, io_read);
   
    while(fread(handle, line)) {

        if((i = strfind(line, "=", false)) != -1) {

            line[i] = EOS;

            if(!strcmp(tag, line, false)) {

                format(string, sizeof string, line[++i]);
                break;
            }
        }
    }
   
    return string;
}


Download:
http://pastebin.com/WquqkYeC



Crйditos:
- zPain: Desenvolvimento do script
- ipsBruno: Pequeno esclarecimento
Reply
#2

Legal

Na verdade todos os gerenciadores de dados funcionam a base de string, afinal, eles criam uma string dentro do arquivo, se vocк tiver a fim dб pra fazer completinho

"You must spread some Reputation around before giving it to zPain again"
Reply
#3

Quote:
Originally Posted by Juniiro3
Посмотреть сообщение
Na verdade todos os gerenciadores de dados funcionam a base de string, afinal, eles criam uma string dentro do arquivo
Sim. Mas como й algo simples tambйm, deixei somente a funзгo de escrita. E perfeitamente possнvel fazer com outros tipos de dados tambйm.
Reply
#4

Simples e direto, assim fica rбpido os cуdigos, pena que jб vi muitas deste tipo.

Bom trabalho.

@edit:
Код:
You must spread some Reputation around before giving it to zPain again.
Reply
#5

Quote:
Originally Posted by Schocc
Посмотреть сообщение
Simples e direto
Essa era a intenзгo
Reply
#6

Muito bom Pain, gostei do seu trabalho! Й sempre bom ver usuбrios novatos no fуrum fazendo уtimas releases assim, parabйns, embora eu nгo use arquivos ini, vocк й um уtimo scripter. Rep++;
Reply
#7

Quote:
Originally Posted by n0minal
Посмотреть сообщение
Muito bom Pain, gostei do seu trabalho! Й sempre bom ver usuбrios novatos no fуrum fazendo уtimas releases assim, parabйns, embora eu nгo use arquivos ini, vocк й um уtimo scripter. Rep++;
Obrigado. Como eu jб disse, eu programo Pawn hб um bom tempo, sу nгo participava do fуrum.



Ediзгo:
Adicionei break em file_get pra nгo precisar ler o restante do arquivo quando achar a tag.
Reply
#8

se fosse anos atrбs seria boa. DOF2 ainda owna.kk
Reply
#9

Muito boa

parabйns
Reply
#10

@edit: dei uma analisada.

Poderia pegar mais pesado nas outras funзхes, ainda tem muitas utilidades a ser feita.
Vocк deve saber que os macros ali, nгo tem muita importвncia.
Por exemplo se eu quiser criar um arquivo, e este arquivo jб se existir, quero que ele nгo escreve novamente em cima.
A nгo ser que usasse io_append

io_write= somente escrever
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)