Пример #1
0
Token Scanner::GetNextToken()
{
	Preprocess();
	if (file_.eof()){
		return Token(TokenType::TOKEN_END_OF_FILE, -1);
	}

	if (std::isdigit(cur_char_)){
		return HandleInteger();
	}
	else if (IsOperator()){
		char op = cur_char_;
		Advance();
		return Token(TokenType::TOKEN_OPERATION, op);
	}
	else if (IsLeftParam()){
		Advance();
		return Token(TokenType::TOKEN_LEFT_PARAM, '(');
	}
	else if (IsRightParam()){
		Advance();
		return Token(TokenType::TOKEN_RIGHT_PARAM, ')');
	}

	return Token();
}
Пример #2
0
bool PBFParser::readNextBlock(std::fstream &stream, ParserThreadData *thread_data)
{
    if (stream.eof())
    {
        return false;
    }

    if (!readPBFBlobHeader(stream, thread_data))
    {
        return false;
    }

    if (thread_data->PBFBlobHeader.type() != "OSMData")
    {
        return false;
    }

    if (!readBlob(stream, thread_data))
    {
        return false;
    }

    if (!thread_data->PBFprimitiveBlock.ParseFromArray(&(thread_data->charBuffer[0]),
                                                       thread_data->charBuffer.size()))
    {
        std::cerr << "failed to parse PrimitiveBlock" << std::endl;
        return false;
    }
    return true;
}
Пример #3
0
bool Conf_Process(std::fstream & f)
{
	char line[MAX_LINE_LENGTH];
	unsigned l = 0;
	bool breakflag = false;
	
	while (!f.eof() && !breakflag) {
		l++;
		f.getline(line, sizeof(line));
		if (line[0] == '\0') {
			// empty line, skip it (yeah we should ignore whitespaces too)
			continue;
		}
		if (line[0] == '/' && line[1] == '/') {
			// comment, skip it
			continue;
		}

		Conf_AcceptCommand(line, breakflag);
	}

	if (breakflag) {
		printf("Line: %u\n", l);
	}

	return true;
}
Пример #4
0
inline bool PBFParser::readBlob(std::fstream &stream, ParserThreadData *thread_data)
{
    if (stream.eof())
    {
        return false;
    }

    const int size = thread_data->PBFBlobHeader.datasize();
    if (size < 0 || size > MAX_BLOB_SIZE)
    {
        std::cerr << "[error] invalid Blob size:" << size << std::endl;
        return false;
    }

    char *data = new char[size];
    stream.read(data, sizeof(data[0]) * size);

    if (!thread_data->PBFBlob.ParseFromArray(data, size))
    {
        std::cerr << "[error] failed to parse blob" << std::endl;
        delete[] data;
        return false;
    }

    if (thread_data->PBFBlob.has_raw())
    {
        const std::string &data = thread_data->PBFBlob.raw();
        thread_data->charBuffer.clear();
        thread_data->charBuffer.resize(data.size());
        std::copy(data.begin(), data.end(), thread_data->charBuffer.begin());
    }
    else if (thread_data->PBFBlob.has_zlib_data())
    {
        if (!unpackZLIB(thread_data))
        {
            std::cerr << "[error] zlib data encountered that could not be unpacked" << std::endl;
            delete[] data;
            return false;
        }
    }
    else if (thread_data->PBFBlob.has_lzma_data())
    {
        if (!unpackLZMA(thread_data))
        {
            std::cerr << "[error] lzma data encountered that could not be unpacked" << std::endl;
        }
        delete[] data;
        return false;
    }
    else
    {
        std::cerr << "[error] Blob contains no data" << std::endl;
        delete[] data;
        return false;
    }
    delete[] data;
    return true;
}
Пример #5
0
	void Database::_WriteBlock(Item towrite, uint64_t blockpos) {
		backing.seekg(blockpos);
		backing.seekp(blockpos);
		uint64_t blkhdrs[4];
		if (backing.eof()) {
			blkhdrs[0] = 0;
			blkhdrs[1] = towrite.itemsize;
			blkhdrs[2] = towrite.itemsize;
			blkhdrs[3] = towrite.itemsize + (sizeof(uint64_t)*4) + 1;
			backing.write(0, 1);
			backing.write((char*)&blkhdrs, sizeof(uint64_t)*4);
			backing.write(towrite.item, towrite.itemsize);
			backing.flush();
		} else {
			_GetLock(blockpos, true);
			backing.read((char*)blkhdrs, sizeof(uint64_t)*4);
			if (towrite.itemsize + 1 + (sizeof(uint64_t)*4) <= blkhdrs[3]) {
				//Block large enough or non-existent block
				backing.write(0, 1);
				blkhdrs[0] = 0;
				blkhdrs[1] = towrite.itemsize;
				blkhdrs[2] = towrite.itemsize;
				//Keep blkhdrs[3]
				backing.write((char*)&blkhdrs, sizeof(uint64_t)*4);
				backing.write(towrite.item, towrite.itemsize);
				backing.flush();
				_ReleaseLock(blockpos, true);
			} else {
				//Have to continue to a new block, allocate or reuse
				std::streampos currentpos = backing.tellp();
				backing.seekp(0, std::ios::end);
				std::streampos endpos = backing.tellp();
				backing.seekp(currentpos, std::ios::beg);
				blkhdrs[0] = (uint64_t) endpos;

				//Keep blkhdrs[1]
				blkhdrs[2] = towrite.itemsize;
				//Keep blkhdrs[3]

				uint64_t blockcancontain = blkhdrs[3] - ((sizeof(uint64_t)*4) + 1);
				backing.write(0, 1);
				backing.write((char*)&blkhdrs, sizeof(uint64_t)*4);
				backing.write(towrite.item, blockcancontain);
				backing.flush();
				_ReleaseLock(blockpos, true);

				//Continue to next block!
				Item process;
				process.itemsize = towrite.itemsize-blockcancontain;
				process.item = towrite.item+blockcancontain;
				_WriteBlock(process, blockpos);
			}
		}
	}
Пример #6
0
// ----------------------------------------------------------------------------
int read_n_lines(std::fstream & fs, int N, StringVect & lines) {
    lines.clear();
    int K = 0;
    while (!fs.eof() && K < N) {
        std::string line;
        std::getline(fs, line);
        lines.push_back(line);
        ++K;
    }
    return K;
}
Пример #7
0
void copy_file(std::fstream& outf, std::fstream& inf)
{
    while (true) {
        char c;
        inf.read(&c, sizeof (c));
        if(inf.eof()) {
            break;
        }
        outf.write(&c, sizeof (c));
    }
}
Пример #8
0
bool leerLineaTXTGID(std::fstream &fichero, std::string &info, std::string patron)
{
    // Buscamos la siguiente linea. Podremos encontrar cuatro cosas:
	//  - Linea en blanco: Seguimos buscando.
	//  - Linea compuesta de guiones: Seguimos buscando.
	//  - Linea de encabezado: Seguimos buscando.
	//  - Linea con patron en posiciones 2 a 12: Bingo.
	// Cualquier otra cosa encontrada, salimos devolviendo false.

    std::string linea_fichero;

    while(!fichero.eof()) {
        std::getline(fichero, linea_fichero);

        if(linea_fichero.length() < 10)
            // Linea en blanco, siguiente.
            continue;
        else if(linea_fichero.substr(2, 7).compare("-------") == 0)
            // Linea con guiones, siguiente.
            continue;
        else if(linea_fichero.substr(1, 7).compare("MARCIAL") == 0)
            // Linea de encabezado, siguiente.
            continue;
        else if(linea_fichero.substr(2, 10).compare(patron) == 0)
            // La linea que estamos buscando. Salimos.
            break;
        else
            // Lo que se haya leido no debería estar ahí. Error.
            return false;
    }

    if(!fichero.eof())
        // Almacenamos la linea en su sitio y seguimos.
        info = linea_fichero;
    else
        // Fin de fichero inesperado.
        return false;

    return true;
}
Пример #9
0
std::string extractQuotedPrefix(std::fstream &mfilestream)
{
    char c;
    c = getnextnonwhite(mfilestream);
    if(c!='\"')
    {
        throw parseerror::no_leading_quote;
    }
    else
    {
        mfilestream.get(c);
        std::string output;
        bool escaped = false;
        bool stringmfinished = false;

        while(!stringmfinished)
        {
            output+=c;
            if(mfilestream.eof())
            {
                throw parseerror::no_finishing_quote;
            }
            mfilestream.get(c);
            if(c=='\\')
            {
                if(escaped)
                {
                    escaped = false;
                }
                else
                {
                    escaped = true;
                }
            }
            else if(c =='\"')
            {
                if(escaped)
                {
                    escaped = false;
                }
                else stringmfinished = true;

            }
            else
            {
                escaped = false;
            }
        }
        return output;
    }
}
Пример #10
0
BOOL AflMapData::readHeader(std::fstream& file,LPSTR pHeader,LPVOID* ppData,LPDWORD pdwSize)
{
	DWORD dwSize=0;
	if(file.eof())
		return false;
	file.read((PCHAR)&dwSize,sizeof(DWORD));
	file.read((PCHAR)pHeader,dwSize);
	file.read((PCHAR)&dwSize,sizeof(DWORD));
	*ppData = NEW BYTE[dwSize];
	file.read((PCHAR)*ppData,dwSize);
	if(pdwSize)
		*pdwSize = dwSize;
	return true;
}
Пример #11
0
    static int getImageCount(std::fstream& file_list)
    {
        int sample = 0;
        std::string line;
        while (std::getline(file_list, line))
            ++sample;
        if (file_list.eof())
            file_list.clear();  //otherwise we can't do any further I/O
        else if (file_list.bad()) {
            throw  std::runtime_error("Error occured while reading files_list.txt");
        }

        return sample;
    }
Пример #12
0
	Database::Database(char* file) {
		backing.open(file, std::ios::in|std::ios::out|std::ios::binary);
		if (backing.eof()) {
			char ver = FIDBVER;
			backing.write(&ver, 1);
		} else {
			char usedver = '\0';
			backing.read(&usedver, 1);
			if (usedver != FIDBVER) {
				std::cerr << "Version " << usedver << " in file differs from " << FIDBVER << " in library!" << std::endl;
				return;
			}
		}
		indexstore = _ReadIndex(1); //Index is always on second byte
	}
Пример #13
0
inline bool PBFParser::readPBFBlobHeader(std::fstream &stream, ParserThreadData *thread_data)
{
    int size(0);
    stream.read((char *)&size, sizeof(int));
    size = SwapEndian(size);
    if (stream.eof())
    {
        return false;
    }
    if (size > MAX_BLOB_HEADER_SIZE || size < 0)
    {
        return false;
    }
    char *data = new char[size];
    stream.read(data, size * sizeof(data[0]));

    bool dataSuccessfullyParsed = (thread_data->PBFBlobHeader).ParseFromArray(data, size);
    delete[] data;
    return dataSuccessfullyParsed;
}
Пример #14
0
void calculateTransform(std::fstream &pointsFs) {
    Eigen::Matrix4f trans;
    pcl::PointCloud<pcl::PointXYZ> points_in_kinect;
    pcl::PointCloud<pcl::PointXYZ> points_in_arm;

    //read data from file into data structures
    char temp_buf[200];
    pcl::PointXYZ KinPos;
    pcl::PointXYZ ArmPos;
    while(true){
        pointsFs.getline(temp_buf,200);
        if(pointsFs.eof()) break;
        if (sscanf(temp_buf,"%f,%f,%f,%f,%f,%f", &(KinPos.x), 
                   &(KinPos.y), &(KinPos.z), &(ArmPos.x), &(ArmPos.y), 
                   &(ArmPos.z)) == 6) {
            points_in_kinect.push_back(KinPos);
            points_in_arm.push_back(ArmPos);
        }
    }

    //calibrate
    pcl::registration::TransformationEstimationSVD<pcl::PointXYZ,pcl::PointXYZ> transformation_estimator;
    if(points_in_kinect.size() > 6 ) {
        transformation_estimator.estimateRigidTransformation(points_in_kinect,points_in_arm,trans);
        cout << "Calculated transformation: " <<endl;
        cout << trans << endl;
        //write to file
        std::ofstream fs("mat.txt");
        if(!fs){
            std::cerr<<"Cannot open the output file."<<std::endl;
        }
        else{
            fs<< trans << endl ;
            fs.close();
        }
        cout << "transformation matrix written to mat.txt " << endl;
    }
    else{
        cout << "Not enough points to run SVD estimation" << endl;
    }
}
Пример #15
0
////////////////////////////////////////////////////////////////////////////////////
///
///   \brief Reads a line from a fstream adding it to a string, reading up
///          to a newline character.
///
///   \param[in] str File stream operator.
///   \param[out] line Line read from file (doesn't include carriage return).
///
///   \return Number of characters read.
///
////////////////////////////////////////////////////////////////////////////////////
unsigned int FileIO::ReadLine(std::fstream& str,
                              std::string& line)
{
    char ch;
    line.reserve(512);
    line.clear();
    while(str.eof() == false && str.good())
    {
        str.get(ch);
        if(ch == '\n')
        {
            break;
        }
        else
        {
            line.push_back(ch);
        }
    }

    return (unsigned int)line.size();
}
Пример #16
0
//options
void createandprintlist()//option1
{
    
    
    
    char x[80];
    
    
    while (((s[0])>'9' || (s[0])<'0') && !f1.eof())
    {
        f1.getline(s, 80, ' ');
        l1.insertqueue(x);
        
       
    }
    
    
    l1.display();
    


}
    void cConfigurationFileIO::Parse(std::fstream& argFile, cConfigurationData& argTarget) const
    {
        cConfigurationData::tKey key;
        cConfigurationData::tValue value;
        std::string equalSign;

        /*
            http://en.cppreference.com/w/cpp/string/basic_string/getline
            while not eof
                get line
                try to parse line
                    if parse fail
                        throw exception
                    if IO fail
                        throw exception
                set variables
            if eof and no exception so far
                success
        */
        while(!argFile.eof())
        {
            std::string line;
            std::getline(argFile, line);

            // Find delimiter '='

            int delimitersFound = 0;
            std::string lineCopy = line;
            std::vector<std::string> tokens;
            std::string token;
            tokens.clear();

            std::stringstream sstr(lineCopy);
            while(std::getline(sstr,token,this->ConfigFileDelimiter))
            {
                tokens.push_back(token);
            }
            if(tokens.size() != 2)
            { 
                throw std::exception();
            }
            else
            {
                bool t1 = Tools::Strings::IsSingleContinuousWord(tokens[0]);
                bool t2 = Tools::Strings::IsSingleContinuousWord(tokens[1]);
                tokens[0] = Tools::Strings::RemoveWhitespaces(tokens[0]);
                tokens[1] = Tools::Strings::RemoveWhitespaces(tokens[1]);
                if(tokens[0].empty() || tokens[1].empty())
                {
                    throw std::exception();
                }
            }

        }

        /*

        while(!argFile.eof())
        {
            key.clear();
            value.clear();
            equalSign.clear();

            try
            {
                //convert this into getline
                argFile >> key;
                argFile >> equalSign;
                argFile >> value;
            }
            catch(std::ios_base::failure ex)
            {
                if(!argFile.eof())
                {
                    throw ex;
                }
            }

            // FIX this shit --\
            //  logic          |
            //                \ /
            if((key.empty() || equalSign.compare("=") != 0 || value.empty()) && argFile.eof())
            {
                if(!(key.empty() && equalSign.compare("=") != 0 && value.empty()))
                {
                    throw std::exception("Parsing error");
                }
            }
            else
            {
                argTarget.Insert(key,value);
            }
        }
        */
    }
Пример #18
0
	/**
	 * Returns true if a read failed because the stream end has been reached.
	 * This flag is cleared by clearErr().
	 * For a SeekableReadStream, it is also cleared by a successful seek.
	 *
	 * @note The semantics of any implementation of this method are
	 * supposed to match those of ISO C feof(). In particular, in a stream
	 * with N bytes, reading exactly N bytes from the start should *not*
	 * set eos; only reading *beyond* the available data should set it.
	 */
	virtual bool eos() const {
		return _stream->eof();
	}
Пример #19
0
		/*
		  This function loads information from a binary file named
		  Serendipity.data
		  Checked
		*/
		void BookCollection::load(std::fstream &binFile)
		{
			// used to read 
			const int BUFFER_SIZE = 256;
			static char buffer[256];

			// create a book to hold the incoming info
			BookData tempB;

			// This is a marker so if the file is empty binFile.eof will return true
			char a;
			binFile.read(reinterpret_cast<char *>(&a), sizeof(a));

			while (!binFile.eof())
			{
				// get length and data for title
				int titleLength;
				binFile.read(reinterpret_cast<char *>(&titleLength), sizeof(int));
				// Read the data for the title into a local buffer
				binFile.read(buffer, titleLength);
				// Null terminate the buffer
				buffer[titleLength] = '\0';
				// get the title
				tempB.setTitle(buffer);

				// get length and data for the Isbn
				int isbnLength;
				binFile.read(reinterpret_cast<char *>(&isbnLength), sizeof(int));
				binFile.read(buffer, isbnLength);
				buffer[isbnLength] = '\0';
				tempB.setIsbn(buffer);

				// get length and data for the author
				int authorLength;
				binFile.read(reinterpret_cast<char *>(&authorLength), sizeof(int));
				binFile.read(buffer, authorLength);
				buffer[authorLength] = '\0';
				tempB.setAuthor(buffer);

				// get length and data for the publisher
				int pubLength;
				binFile.read(reinterpret_cast<char *>(&pubLength), sizeof(int));
				binFile.read(buffer, pubLength);
				buffer[pubLength] = '\0';
				tempB.setPublisher(buffer);

				// get length and data for the date added
				int daLength;
				binFile.read(reinterpret_cast<char *>(&daLength), sizeof(int));
				binFile.read(buffer, daLength);
				buffer[daLength] = '\0';
				tempB.setDateAdded(buffer);

				// get length and data for the quantity
				int quantIn;
				binFile.read(reinterpret_cast<char *>(&quantIn), sizeof(quantIn));
				tempB.setQuantity(quantIn);

				// get length and data for the wholesale
				double wsIn;
				binFile.read(reinterpret_cast<char *>(&wsIn), sizeof(wsIn));
				tempB.setWholesaleCost(wsIn);

				// get length and data for the retail price
				double rpIn;
				binFile.read(reinterpret_cast<char *>(&rpIn), sizeof(rpIn));
				tempB.setRetailPrice(rpIn);

				// get m_empty
				//bool e;
				//binFile.read(reinterpret_cast<char *>(&e), sizeof(e));
				//if (e)
				//{
				//	book[index].isEmpty();
				//} // end if
				//else
				//{
				//	book[index].insertBook();
			
				//} // end while
				if (!binFile.eof())
				{
					bookList.push_back(tempB);
				} // end if

			} // end for
		} // end function load
Пример #20
0
std::vector < std::string > getNonEmptyRow(std::fstream & file, char comment){
  std::vector < std::string > row;
  while((row = getRowSubstrings(file, comment)).empty() && !file.eof());
  return row;
}
Пример #21
0
	virtual bool eos() const {
		return _fileStream->eof();
	}