Beispiel #1
0
/**
 * Read the speed file
 * @param fileName the filename
 */
void readSpeedFile(char * fileName) {
	int fd;
	struct stat statBuf;
	FILE * fp = NULL;
	unsigned int lineNumber = 0;
	char * name = NULL;
	char * value = NULL;
	unsigned long uplink = DEF_UPLINK_SPEED;
	unsigned long downlink = DEF_DOWNLINK_SPEED;
	bool uplinkSet = false;
	bool downlinkSet = false;

	fd = open(fileName, O_RDONLY);
	if (fd < 0) {
		/* could not access the file */
		goto out;
	}

	if (fstat(fd, &statBuf)) {
		/* could not access the file */
		goto out;
	}

	if (!memcmp(&cachedStat.timeStamp, &statBuf.st_mtime, sizeof(cachedStat.timeStamp))) {
		/* file did not change since last read */
		goto out;
	}

	fp = fdopen(fd, "r");
	if (!fp) {
		goto out;
	}

	memcpy(&cachedStat.timeStamp, &statBuf.st_mtime, sizeof(cachedStat.timeStamp));

	while (fgets(line, LINE_LENGTH, fp)) {
		regmatch_t pmatch[regexNameValuematchCount];

		lineNumber++;

		if (regexMatch(&regexComment, line, 0, NULL)) {
			continue;
		}

		if (!regexMatch(&regexNameValue, line, regexNameValuematchCount, pmatch)) {
			sgwDynSpeedError(false, "Gateway speed file \"%s\", line %d uses invalid syntax: %s", fileName, lineNumber,
					line);
			goto out;
		}

		/* determine name/value */
		name = &line[pmatch[1].rm_so];
		line[pmatch[1].rm_eo] = '\0';
		value = &line[pmatch[2].rm_so];
		line[pmatch[2].rm_eo] = '\0';

		if (!strncasecmp(SPEED_UPLINK_NAME, name, sizeof(line))) {
			if (!readUL(SPEED_UPLINK_NAME, value, &uplink)) {
				goto out;
			}
			uplinkSet = true;
		} else if (!strncasecmp(SPEED_DOWNLINK_NAME, name, sizeof(line))) {
			if (!readUL(SPEED_DOWNLINK_NAME, value, &downlink)) {
				goto out;
			}
			downlinkSet = true;
		} else {
			sgwDynSpeedError(false, "Gateway speed file \"%s\", line %d uses an invalid option \"%s\","
					" valid options are [%s|%s]", fileName, lineNumber, name, SPEED_UPLINK_NAME, SPEED_DOWNLINK_NAME);
			goto out;
		}
	}

	fclose(fp);
	fp = NULL;

	if (uplinkSet) {
		olsr_cnf->smart_gw_uplink = uplink;
	}
	if (downlinkSet) {
		olsr_cnf->smart_gw_downlink = downlink;
	}
	if (uplinkSet || downlinkSet) {
	  refresh_smartgw_netmask();
	}

	out: if (fp) {
		fclose(fp);
	}
	if (fd >= 0) {
		close(fd);
	}
	return;
}
Beispiel #2
0
int readDataFile(char *fName, ProgramData *programData, char saveSpectra) {
  FILE *datf;
  unsigned int i;

  datf = fopen(fName, "r");
  if(datf) {
    if(!readUL(datf, &programData->version) || /* read version */
       !readInt(datf, &programData->opMode) || /* read operation mode */
       !readInt(datf, &programData->nRuns)) {  /* read number of runs */
      fclose(datf);
      return -2; /* ERROR parsing header */
    }
  
    programData->passFail = calloc(programData->nRuns+1, sizeof(char));
    for(i=0; i<(unsigned int)programData->nRuns; i++) {
      int c = fgetc(datf);
      if(c=='1') programData->passFail[i] = 1;
      else if(c=='0') programData->passFail[i] = 0;
      else return -3;
    }
    if(fgetc(datf)!='\n') {
      return -3;
    }

    programData->nSpectra = 0;
    for(i=0; i<MAX_SPECTRA; i++) {
      int r;
      int comps;

      _fscanSpectrumHeader(datf, &programData->spectrum[i]);           /* read spectrum header */
      comps = programData->spectrum[i].nComponents;

      if(saveSpectra) {
        programData->spectrum[i].data = (unsigned int*)calloc(comps * programData->nRuns, sizeof(unsigned int));
        if(comps > 0) {
          programData->nSpectra++;
          for(r=0; r<programData->nRuns; r++) {
            _fscanSpectrumData(datf, &programData->spectrum[i], r);  /* read spectrum data for run r */
          }
        }
      } else {
        _Spectrum s;
        s = programData->spectrum[i];
        s.data = (unsigned int*)calloc(comps * programData->nRuns, sizeof(unsigned int));
        if(comps > 0) {
          programData->nSpectra++;
          for(r=0; r<programData->nRuns; r++) {
            _fscanSpectrumData(datf, &s, r);
          }
        }
        free(s.data);
      }
    }

    programData->nInvariantTypes = 0;
    for(i=0; i<MAX_INVARIANTTYPES; i++) {
      int invariants;
      _fscanInvariantTypeHeader(datf, &programData->invariantType[i]);            /* read invariant type header */
      invariants = programData->invariantType[i].nInvariants;

      if(invariants > 0) {
        programData->nInvariantTypes++;
      }
      programData->invariantType[i].data = (_Invariant*)calloc(invariants, sizeof(_Invariant));
      _fscanInvariantTypeData(datf, &programData->invariantType[i]);  /* read invariant data */
    }

    fclose(datf);
    return 0; /* success */
  } else {
    return -1; /* ERROR opening datafile */
  }
}