2019-10-30 05:31:38 +01:00
|
|
|
/*
|
|
|
|
* NcLooper test des fonctions fichier (???)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2019-10-31 15:47:54 +01:00
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "ffuncs.h"
|
2019-10-30 05:31:38 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
2019-10-31 15:47:54 +01:00
|
|
|
int verbosity;
|
2019-10-30 05:31:38 +01:00
|
|
|
|
2019-10-31 15:47:54 +01:00
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
void affiche_un_sample(SampleRef *sref)
|
|
|
|
{
|
2019-11-01 16:25:42 +01:00
|
|
|
printf("%c %02X [%-20s] %s\n", sref->key, sref->flags, sref->text,
|
2019-10-31 15:47:54 +01:00
|
|
|
sref->path);
|
|
|
|
}
|
2019-10-30 05:31:38 +01:00
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
static int decode_la_ligne(char *line, SampleRef *sref)
|
|
|
|
{
|
2019-10-31 15:47:54 +01:00
|
|
|
char *ptr, *cp;
|
2019-10-30 05:31:38 +01:00
|
|
|
|
|
|
|
#if DEBUG_LEVEL
|
|
|
|
fprintf(stderr, ">>> %s ( '%s' %p )\n", __func__, line, sref);
|
2019-10-31 15:47:54 +01:00
|
|
|
// dump(line);
|
2019-10-30 05:31:38 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ON THE WAY TO PARSING HELL
|
|
|
|
*/
|
|
|
|
ptr = strtok(line, "|");
|
2019-10-31 15:47:54 +01:00
|
|
|
cp = ltrim(rtrim(ptr));
|
2019-11-01 11:37:35 +01:00
|
|
|
if (verbosity) fprintf(stderr, "key [%s]\n", cp);
|
2019-10-31 15:47:54 +01:00
|
|
|
if ( ! isalpha(*cp)) {
|
|
|
|
fprintf(stderr, "invalid key 0x%02x\n", *cp);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
sref->key = toupper(*cp);
|
|
|
|
|
2019-10-30 05:31:38 +01:00
|
|
|
ptr = strtok(NULL, "|");
|
2019-10-31 15:47:54 +01:00
|
|
|
if (NULL==ptr) {
|
|
|
|
fprintf(stderr, "line to short\n");
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
cp = ltrim(rtrim(ptr));
|
2019-11-01 11:37:35 +01:00
|
|
|
if (verbosity) fprintf(stderr, "text [%s]\n", cp);
|
2019-10-31 15:47:54 +01:00
|
|
|
if (strlen(cp) > SZ_TEXT) {
|
|
|
|
fprintf(stderr, "text too long\n");
|
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
strcpy(sref->text, cp);
|
|
|
|
|
2019-10-30 05:31:38 +01:00
|
|
|
ptr = strtok(NULL, "|");
|
2019-10-31 15:47:54 +01:00
|
|
|
if (NULL==ptr) {
|
|
|
|
fprintf(stderr, "line to short\n");
|
|
|
|
return -6;
|
|
|
|
}
|
2019-11-01 16:25:42 +01:00
|
|
|
cp = ltrim(rtrim(ptr));
|
|
|
|
if (verbosity) fprintf(stderr, "path [%s]\n", cp);
|
|
|
|
if (strlen(ptr) > SZ_PATH) {
|
2019-10-31 15:47:54 +01:00
|
|
|
fprintf(stderr, "path too long\n");
|
|
|
|
return -5;
|
|
|
|
}
|
2019-11-01 16:25:42 +01:00
|
|
|
strcpy(sref->path, cp);
|
2019-10-30 05:31:38 +01:00
|
|
|
|
2019-10-31 15:47:54 +01:00
|
|
|
return 0;
|
2019-10-30 05:31:38 +01:00
|
|
|
}
|
|
|
|
/* --------------------------------------------------------------------- */
|
2019-10-31 15:47:54 +01:00
|
|
|
#define T_LINE 300
|
2019-10-30 05:31:38 +01:00
|
|
|
|
|
|
|
int essai_lecture_liste(char *fname)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char line[T_LINE+1];
|
|
|
|
int ln; /* line number */
|
|
|
|
int foo, bar;
|
|
|
|
SampleRef sample;
|
|
|
|
|
|
|
|
#if DEBUG_LEVEL
|
|
|
|
fprintf(stderr, ">>> %s ( '%s' )\n", __func__, fname);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (NULL==(fp=fopen(fname, "r"))) {
|
|
|
|
perror(fname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ln = 1;
|
|
|
|
while (NULL != fgets(line, T_LINE, fp)) {
|
|
|
|
|
2019-10-31 15:47:54 +01:00
|
|
|
// fprintf(stderr, "%3d = %s", ln, line);
|
2019-10-30 05:31:38 +01:00
|
|
|
|
|
|
|
/* degommer l'eventuel \n */
|
|
|
|
bar = strlen(line);
|
|
|
|
if (0==bar) {
|
|
|
|
fprintf(stderr,"line %d very short\n", ln);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bar--; /* backspace one char */
|
|
|
|
if ('\n' == line[bar]) line[bar]='\0';
|
|
|
|
|
2019-10-31 15:47:54 +01:00
|
|
|
memset(&sample, 0, sizeof(SampleRef));
|
2019-10-30 05:31:38 +01:00
|
|
|
foo = decode_la_ligne(line, &sample);
|
2019-11-01 16:25:42 +01:00
|
|
|
#if DEBUG_LEVEL
|
2019-10-31 15:47:54 +01:00
|
|
|
fprintf(stderr, "decode la ligne -> %d\n\n", foo);
|
2019-11-01 16:25:42 +01:00
|
|
|
#endif
|
2019-10-31 15:47:54 +01:00
|
|
|
affiche_un_sample(&sample);
|
2019-10-30 05:31:38 +01:00
|
|
|
|
|
|
|
ln++;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
void help(int k)
|
|
|
|
{
|
|
|
|
puts("Test des fonctions de gestion des fichiers");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int foo;
|
|
|
|
int opt;
|
|
|
|
char *listname = "samples.list";
|
|
|
|
|
|
|
|
while ((opt = getopt(argc, argv, "hv")) != -1) {
|
|
|
|
switch(opt) {
|
|
|
|
case 'h': help(0); break;
|
|
|
|
case 'v': verbosity++; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG_LEVEL
|
|
|
|
fprintf(stderr, "argc = %d, optind = %d\n", argc, optind);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
foo = essai_lecture_liste(listname);
|
|
|
|
fprintf(stderr,"essai lecture '%s' -> %d\n", listname, foo);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* --------------------------------------------------------------------- */
|