Subversion Repositories SvarDOS

Rev

Rev 245 | 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. */
35
int readlsm(char *filename, char *version, int version_maxlen) {
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
  /* check the file's header */
46
  if (readline_fromfile(fd, linebuff, 64) < 0) {
47
    fclose(fd);
48
    return(-1);
49
  }
50
  if (strcasecmp(linebuff, "begin3") != 0) {
51
    fclose(fd);
52
    return(-1);
53
  }
54
  /* read the LSM file line by line */
55
  while (readline_fromfile(fd, linebuff, 127) >= 0) {
56
    for (x = 0;; x++) {
57
      if (linebuff[x] == 0) {
58
        x = -1;
59
        break;
60
      } else if (linebuff[x] == ':') {
61
        break;
62
      }
63
    }
64
    if (x > 0) {
65
      linebuff[x] = 0;
66
      valuestr = linebuff + x + 1;
67
      trim(linebuff);
68
      trim(valuestr);
69
      if (strcasecmp(linebuff, "version") == 0) {
70
        snprintf(version, version_maxlen, "%s", valuestr);
71
        version[version_maxlen] = 0; /* snprintf is supposed to terminate string itself, but the DJGPP doesn't */
72
      }
73
    }
74
  }
75
  fclose(fd);
76
  return(0);
77
}