00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "stdafx.h"
00027 #include "floatfile.h"
00028 #include <ctype.h>
00029 #include <stdlib.h>
00030
00031 bool ReadFloatFile(const char *filename, vector<float> *values) {
00032 FILE *f = fopen(filename, "r");
00033 if (!f) {
00034 Error("Unable to open file \"%s\"", filename);
00035 return false;
00036 }
00037
00038 int c;
00039 bool inNumber = false;
00040 char curNumber[32];
00041 int curNumberPos = 0;
00042 int lineNumber = 1;
00043 while ((c = getc(f)) != EOF) {
00044 if (c == '\n') ++lineNumber;
00045 if (inNumber) {
00046 if (isdigit(c) || c == '.' || c == 'e' || c == '-' || c == '+')
00047 curNumber[curNumberPos++] = c;
00048 else {
00049 curNumber[curNumberPos++] = '\0';
00050 values->push_back(atof(curNumber));
00051 Assert(curNumberPos < (int)sizeof(curNumber));
00052 inNumber = false;
00053 curNumberPos = 0;
00054 }
00055 }
00056 else {
00057 if (isdigit(c) || c == '.' || c == '-' || c == '+') {
00058 inNumber = true;
00059 curNumber[curNumberPos++] = c;
00060 }
00061 else if (c == '#') {
00062 while ((c = getc(f)) != '\n' && c != EOF)
00063 ;
00064 ++lineNumber;
00065 }
00066 else if (!isspace(c)) {
00067 Warning("Unexpected text found at line %d of float file \"%s\"",
00068 lineNumber, filename);
00069 }
00070 }
00071 }
00072 fclose(f);
00073 return true;
00074 }
00075
00076