Wicri:Dilib, module Buffer, interface BufferFormat
De Wicri Outils
Le type BufferFormat est un outil pour générer des chaînes à partir d'un format (type printf).
Maquette
Code source
/* -*- coding: utf-8 -*- */
#include "Buffer.h"
#include <string.h>
/* a insérer dans Buffer.h */
struct BufferFormatMemoryStruct
{
Buffer **bufArray;
Buffer *result;
char escapeChar;
};
typedef struct BufferFormatMemoryStruct BufferFormatMemory;
BufferFormatMemory* NewBufferFormatMemory();
char *BufferFormatMemorySet(BufferFormatMemory* mem1, char c1, char *str);
char *BufferFormatMemoryGet(BufferFormatMemory* mem1, char c1);
/* a créer BufferFormat.o (lib) */
BufferFormatMemory* NewBufferFormatMemory()
{
BufferFormatMemory *memory;
int i;
memory = (BufferFormatMemory *) malloc(sizeof(BufferFormatMemory));
memory->bufArray=(Buffer **)malloc(sizeof(Buffer*)*16*4);
/* char *[4 * 16] ; for 4 lines of ascii char */
for (i=0 ; i<=63 ; i++) memory->bufArray[i]=NULL;
memory->escapeChar='%';
memory->result=NewBuffer();
return memory;
}
char *BufferFormatMemorySet(BufferFormatMemory* mem1, char c1, char *str)
{
int i;
i=c1-'@'; /* bufArray[0] is for ascii char '@' */
if (! mem1->bufArray[i]) mem1->bufArray[i]=NewBuffer();
BufferStrcpy(mem1->bufArray[i], str);
return BufferString(mem1->bufArray[i]);
}
char *BufferFormatMemoryGet(BufferFormatMemory* mem1, char c1)
{
int i;
i=c1-'@';
if (mem1->bufArray[i]) return BufferString(mem1->bufArray[i]);
return NULL;
}
char *BufferFormatEval(BufferFormatMemory *mem1, char *f1)
{
char *evalPos;
char *memStr;
char *escapePos;
evalPos=f1;
BufferReset (mem1->result);
while ((escapePos=strchr(evalPos, mem1->escapeChar)))
{
BufferStrncat(mem1->result, evalPos, escapePos-evalPos);
if ((memStr=BufferFormatMemoryGet(mem1, escapePos[1]))) BufferStrcat( mem1->result, memStr);
else BufferStrcat( mem1->result, "*** null***");
evalPos=escapePos+2;
}
BufferStrcat(mem1->result, evalPos);
return BufferString (mem1->result);
}
/* zone test (non regression ? )*/
int main()
{
BufferFormatMemory *mem1;
char c;
mem1 = NewBufferFormatMemory();
BufferFormatMemorySet(mem1, 'c', "Paris");
printf("%s\n", BufferFormatMemoryGet(mem1, 'c'));
BufferFormatMemorySet(mem1, 'p', "France");
printf("%s\n", BufferFormatEval(mem1, "Pays :"));
printf("%s\n", BufferFormatEval(mem1, "Pays : %p"));
printf("%s\n", BufferFormatEval(mem1, "Pays : %p ;"));
printf("%s\n", BufferFormatEval(mem1, "Pays : %p ; capitale %c."));
}
Importation
WicriGetPage -l wicri-outils.fr -p "Wicri:Dilib, module Buffer, interface BufferFormat" \
| MediaWikiExtractSources -w | HfdStoreFile
Compilation
gcc testBufferFormat.c $DILIB_CC -o testBufferFormat
Test
- Pour visualiser la liste d'entrée
./testBufferFormat