Example #1
0
void CRunner::CheckAndOpenFileForReading(ifstream & file, const string& fileName)
{
	file.open(fileName);
	file.exceptions(ifstream::badbit);
	if (!file.is_open())
	{
		throw ifstream::failure(MESSAGE_FAILED_OPEN + fileName + MESSAGE_FAILED_OPEN_FOR_READING);
	}
}
Example #2
0
int CSVop::readCSVfile(ifstream &file)
{
    int err;
    file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
    if(!file.is_open()) return -1;
    err = tab.loadTable(file, CSVparams.delim, CSVparams.ignorebefore, CSVparams.ignoreafter);
    if(0!=err) return err;
    tab.rmquotmarks();
    return 0;
}
Example #3
0
int KiCadSCH::readSCHfile(ifstream &file)
{
    int err;
    file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
    if(!file.is_open()) return -1;
    err = tab.loadTable(file, " ");
    if(0!=err) return err;
    tab.rmquotmarks();

    return 0;
}
Example #4
0
void evaluateKeyWord(ifstream &infile ///< stream with parameter file
		, string keyword, ///< keyword
		HostParameters &hostParamters, Parameters &khParamters) {
	infile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
	try {
		if (keyword == "USEOPENCL") {
			infile >> hostParamters.useOPENCL;
		} else if (keyword== "CLTARGET") {
			infile >> hostParamters.targetPlatform;
			infile >> hostParamters.targetDevice;
		} else if (keyword == "PNUMBER") {
/* Tries to read a world file from the specified stream.	On success, returns
 * true and updates the input parameters to mark the type of the world and
 * the world contents.	On failure, returns false, but may still modify the
 * input parameters.
 */
static bool readWorldFile(ifstream& input, Grid<double>& world,
                          WorldType& worldType) try {
	/* Enable exceptions on the stream so that we can handle errors using try-
	 * catch rather than continuously testing everything.
	 */
	input.exceptions(ios::failbit | ios::badbit);

	/* The file line of the file identifies the type, which should be either
	 * "terrain" or "maze."
	 */
	string type;
	input >> type;
	
	if (type == "terrain") {
		worldType = TERRAIN_WORLD;
	} else if (type == "maze") {
		worldType = MAZE_WORLD;
	} else return false;
	
	/* Read the size of the world. */
	int numRows, numCols;
	input >> numRows >> numCols;
	
	if (numRows <= 0 || numCols <= 0 ||
			numRows >= kMaxRows || numRows >= kMaxCols) {
		return false;
	}
	
	world.resize(numRows, numCols);
	
	for (int row = 0; row < numRows; row++) {
		for (int col = 0; col < numCols; col++) {
			double value;
			input >> value;

			/* Validate the input based on the type of world. */			
			if (worldType == MAZE_WORLD) {
				if (value != kMazeWall && value != kMazeFloor) {
					return false;
				}
			} else /* worldType == TERRAIN_WORLD */ {
				if (value < 0.0 || value > 1.0) {
					return false;
				}
			}
			world[row][col] = value;
		}
	}

		return true;
} catch (...) {
	/* Something went wrong, so report an error. */
	return false;
}
Example #6
0
void openInputFile(const string fileName, ifstream &handle){

	handle.exceptions(ifstream::failbit | ifstream::badbit);
	try{
		handle.open(fileName);
	}
	catch (const ifstream::failure &e){
		cerr << e.what() << endl;
		cerr << "File " << fileName << " cannot be opened!" << endl;
		terminate();
	}

}
/*
 * Tries to read a world file from the specified stream.  On success, returns
 * true and updates the input parameters to mark the type of the world and
 * the world contents.  On failure, returns false, but may still modify the
 * input parameters.
 */
static bool readWorldFile(ifstream& input, Grid<double>& world, WorldType& worldType) {
    try {
        // Enable exceptions on the stream so that we can handle errors using try-
        // catch rather than continuously testing everything.
        input.exceptions(ios::failbit | ios::badbit);

        // The file line of the file identifies the type, which should be either
        // "terrain" or "maze."
        string type;
        input >> type;

        if (type == "terrain") {
            worldType = TERRAIN_WORLD;
        } else if (type == "maze") {
            worldType = MAZE_WORLD;
        } else {
            cerr << "world file does not contain type (terrain/maze) as first line." << endl;
            return false;
        }

        // Read the size of the world.
        int numRows, numCols;
        input >> numRows >> numCols;

        if (numRows <= 0 || numCols <= 0 ||
                numRows >= kMaxRows || numRows >= kMaxCols) {
            cerr << "world file contains invalid number of rows/cols: "
                 << numRows << "," << numCols << endl;
            return false;
        }

        world.resize(numRows, numCols);

        for (int row = 0; row < numRows; row++) {
            for (int col = 0; col < numCols; col++) {
                double value;
                input >> value;
                if (input.fail()) {
                    cerr << "Illegal input file format; row #" << (row+1)
                         << "does not contain " << numCols << " valid numbers" << endl;
                    return false;
                }

                // Validate the input based on the type of world.
                if (worldType == MAZE_WORLD) {
                    if (value != kMazeWall && value != kMazeFloor) {
                        cerr << "world file contains invalid square value of " << value
                             << ", must be " << kMazeFloor << " or " << kMazeWall << endl;
                        return false;
                    }
                } else {  // worldType == TERRAIN_WORLD
                    if (value < 0.0 || value > 1.0) {
                        cerr << "world file contains invalid terrain value of " << value
                             << ", must be 0.0 - 1.0" << endl;
                        return false;
                    }
                }
                world[row][col] = value;
            }
        }

        return true;
    } catch (...) {
        // Something went wrong, so report an error.
        cerr << "exception thrown while reading world file" << endl;
        return false;
    }
}