Subversion Repositories SvarDOS

Rev

Rev 614 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
219 mateuszvis 1
/*
268 mateuszvis 2
 * This file is part of pkg (SvarDOS)
1972 mateusz.vi 3
 * Copyright (C) 2013-2024 Mateusz Viste
219 mateuszvis 4
 */
5
 
6
#include <stdio.h>   /* fopen, fclose... */
7
#include <string.h>  /* strcasecmp() */
8
 
249 mateuszvis 9
#include "helpers.h"
10
 
219 mateuszvis 11
#include "lsm.h"     /* include self for control */
12
 
13
 
14
/* Loads metadata from an LSM file. Returns 0 on success, non-zero on error. */
1972 mateusz.vi 15
int readlsm(const char *filename, const char *field, char *result, size_t result_maxlen) {
219 mateuszvis 16
  char linebuff[128];
17
  char *valuestr;
18
  FILE *fd;
1972 mateusz.vi 19
 
219 mateuszvis 20
  /* open the file */
21
  fd = fopen(filename, "rb");
1972 mateusz.vi 22
  result[0] = 0; /* reset fields to be read to empty values (now in case it is the same ptr as filename) */
219 mateuszvis 23
  if (fd == NULL) return(-1);
1972 mateusz.vi 24
 
219 mateuszvis 25
  /* read the LSM file line by line */
249 mateuszvis 26
  while (freadtokval(fd, linebuff, sizeof(linebuff), &valuestr, ':') == 0) {
1972 mateusz.vi 27
    if (valuestr == NULL) continue;
28
    if (strcasecmp(linebuff, field) == 0) {
29
      snprintf(result, result_maxlen, "%s", valuestr);
30
      break;
219 mateuszvis 31
    }
32
  }
33
  fclose(fd);
34
  return(0);
35
}