Compare commits

..

2 Commits

Author SHA1 Message Date
tth
af13698209 reading samples list 2019-10-31 15:47:54 +01:00
tth
5bb5ae987f ltrim & rtrim, to be tested 2019-10-30 11:58:18 +01:00
5 changed files with 141 additions and 18 deletions

View File

@ -1,9 +1,11 @@
CC = gcc
CCOPT = -Wall -g -DDEBUG_LEVEL=1
ffuncs.o: ffuncs.c ffuncs.h Makefile
$(CC) ${CCOPT} -c $<
LIBS =
OBJS = ffuncs.o
t: t.c ${OBJS} Makefile
$(CC) ${CCOPT} $< ${OBJS} ${LIBS} -o $@

61
files/ffuncs.c Normal file
View File

@ -0,0 +1,61 @@
/*
* various functions
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include "ffuncs.h"
/* --------------------------------------------------------------------- */
extern int verbosity;
/* --------------------------------------------------------------------- */
void dump(unsigned char *ptr)
{
int foo;
for (foo=0; foo<24; foo++) fprintf(stderr, "%02x ", ptr[foo]);
fputs("\n", stderr);
for (foo=0; foo<24; foo++) fprintf(stderr, "%c ",
isgraph(ptr[foo])?ptr[foo]:' ');
fputs("\n", stderr);
}
/* --------------------------------------------------------------------- */
char *rtrim(char *src)
{
int foo;
for (foo=strlen(src)-1; src[foo]==' '|| src[foo]=='\t'; foo--) {
src[foo] = '\0';
}
return src;
}
/* --------------------------------------------------------------------- */
char *ltrim(char *src)
{
int foo, bar;
char *tmp;
tmp = alloca(strlen(src));
for (foo=0; src[foo]==' ' || src[foo]=='\t'; foo++);
bar = 0;
while (src[foo]!='\0') {
tmp[bar]=src[foo];
foo++;
bar++;
}
strcpy(src, tmp);
return src;
}
/* --------------------------------------------------------------------- */

26
files/ffuncs.h Normal file
View File

@ -0,0 +1,26 @@
/*
* various file functions
*/
/* --------------------------------------------------------------------- */
/* two arbitrary magic number */
#define SZ_TEXT 20
#define SZ_PATH 400
typedef struct {
char key;
int flags;
char text[SZ_TEXT+1];
char path[SZ_PATH+1];
} SampleRef;
/* --------------------------------------------------------------------- */
void dump(unsigned char *ptr);
char *rtrim(char *src);
char *ltrim(char *src);
/* --------------------------------------------------------------------- */

View File

@ -1,2 +1,3 @@
a | bla bla | ../AK14V-ORDRES.wav
z |znare|znare.wav
B | plop ! | ../AK14V-ORDRES.wav

View File

@ -6,39 +6,71 @@
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include "ffuncs.h"
/* --------------------------------------------------------------------- */
int verbosity;
typedef struct {
char key;
char text[100];
char path[200];
} SampleRef;
/* --------------------------------------------------------------------- */
void affiche_un_sample(SampleRef *sref)
{
printf("%c %02X [%-20s] %s\n", sref->key, sref->flags, sref->text,
sref->path);
}
/* --------------------------------------------------------------------- */
static int decode_la_ligne(char *line, SampleRef *sref)
{
char *ptr;
char *ptr, *cp;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( '%s' %p )\n", __func__, line, sref);
// dump(line);
#endif
/*
* ON THE WAY TO PARSING HELL
*/
ptr = strtok(line, "|");
fprintf(stderr, "[%s]\n", ptr);
ptr = strtok(NULL, "|");
fprintf(stderr, "[%s]\n", ptr);
ptr = strtok(NULL, "|");
fprintf(stderr, "[%s]\n", ptr);
cp = ltrim(rtrim(ptr));
fprintf(stderr, "key [%s]\n", cp);
if ( ! isalpha(*cp)) {
fprintf(stderr, "invalid key 0x%02x\n", *cp);
return -2;
}
sref->key = toupper(*cp);
return -1;
ptr = strtok(NULL, "|");
if (NULL==ptr) {
fprintf(stderr, "line to short\n");
return -3;
}
cp = ltrim(rtrim(ptr));
fprintf(stderr, "text [%s]\n", cp);
if (strlen(cp) > SZ_TEXT) {
fprintf(stderr, "text too long\n");
return -4;
}
strcpy(sref->text, cp);
ptr = strtok(NULL, "|");
if (NULL==ptr) {
fprintf(stderr, "line to short\n");
return -6;
}
fprintf(stderr, "path [%s]\n", ltrim(rtrim(ptr)));
if (strlen(ptr) > SZ_TEXT) {
fprintf(stderr, "path too long\n");
return -5;
}
strcpy(sref->path, ptr);
return 0;
}
/* --------------------------------------------------------------------- */
#define T_LINE 200
#define T_LINE 300
int essai_lecture_liste(char *fname)
{
@ -60,7 +92,7 @@ if (NULL==(fp=fopen(fname, "r"))) {
ln = 1;
while (NULL != fgets(line, T_LINE, fp)) {
fprintf(stderr, "%3d = %s", ln, line);
// fprintf(stderr, "%3d = %s", ln, line);
/* degommer l'eventuel \n */
bar = strlen(line);
@ -71,9 +103,10 @@ while (NULL != fgets(line, T_LINE, fp)) {
bar--; /* backspace one char */
if ('\n' == line[bar]) line[bar]='\0';
memset(&sample, 0, sizeof(SampleRef));
foo = decode_la_ligne(line, &sample);
fprintf(stderr, "decode la ligne -> %d\n", foo);
fprintf(stderr, "decode la ligne -> %d\n\n", foo);
affiche_un_sample(&sample);
ln++;
}