Subversion Repositories SvarDOS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
219 mateuszvis 1
/*
2
 * This file is part of FDNPKG
3
 * Copyright (C) 2013-2016 Mateusz Viste, All rights reserved.
4
 */
5
 
6
#include <stdio.h>   /* fopen, fclose... */
7
#include <string.h>  /* strcasecmp() */
8
 
9
#include "lsm.h"     /* include self for control */
10
#include "rtrim.h"
11
#include "version.h"
12
 
13
/* reads a line from a file descriptor, and writes it to *line, the *line array is filled no more than maxlen bytes. returns the number of byte read on success, or a negative value on failure (reaching EOF is considered an error condition) */
14
static int readline_fromfile(FILE *fd, char *line, int maxlen) {
15
  int bytebuff, linelen = 0;
16
  for (;;) {
17
    bytebuff = fgetc(fd);
18
    if (bytebuff == EOF) {
19
      line[linelen] = 0;
20
      if (linelen == 0) return(-1);
21
      return(linelen);
22
    }
23
    if (bytebuff < 0) return(-1);
24
    if (bytebuff == '\r') continue; /* ignore CR */
25
    if (bytebuff == '\n') {
26
      line[linelen] = 0;
27
      return(linelen);
28
    }
29
    if (linelen < maxlen) line[linelen++] = bytebuff;
30
  }
31
}
32
 
33
 
34
/* Loads metadata from an LSM file. Returns 0 on success, non-zero on error. */
245 mateuszvis 35
int readlsm(const char *filename, char *version, int version_maxlen) {
219 mateuszvis 36
  char linebuff[128];
37
  char *valuestr;
38
  int x;
39
  FILE *fd;
40
  /* reset fields to be read to empty values */
41
  version[0] = 0;
42
  /* open the file */
43
  fd = fopen(filename, "rb");
44
  if (fd == NULL) return(-1);
45
  /* read the LSM file line by line */
245 mateuszvis 46
  while (readline_fromfile(fd, linebuff, sizeof(linebuff) - 1) >= 0) {
219 mateuszvis 47
    for (x = 0;; x++) {
48
      if (linebuff[x] == 0) {
49
        x = -1;
50
        break;
51
      } else if (linebuff[x] == ':') {
52
        break;
53
      }
54
    }
55
    if (x > 0) {
56
      linebuff[x] = 0;
57
      valuestr = linebuff + x + 1;
58
      trim(linebuff);
59
      trim(valuestr);
245 mateuszvis 60
      if (strcasecmp(linebuff, "version") == 0) snprintf(version, version_maxlen, "%s", valuestr);
219 mateuszvis 61
    }
62
  }
63
  fclose(fd);
64
  return(0);
65
}