Пример #1
0
void createHeader(FileWriter& writer, FileReader& reader) {
	std::string line("");
	while (true){
		line = reader.getNextLine() + "\n";
		if (isHeaderLine(line)) {
			writer.writeLine(line);
		} else if (isCommentLine(line)) {
			writer.writeLine(line);
			return;
		}
		if (!reader.isGood()) return;
	}
};
Пример #2
0
void ConfigurationFile::loadFromFile(const char *file_name)
{
        std::ifstream ifs(file_name);
        if (!ifs.is_open())
        {
                throw ConfigurationError("File does not exists");
                return;
        }
        std::string line;
        ConfigurationSection section;
        std::string section_name;
        root_ = SectionContainerRefPtr(new SectionContainer);
        while (getline(ifs,line))
        {
                std::string new_section_name;
                std::string key;
                std::string value;
                if (isCommentLine(line))
                {
                        continue;
                }
                if (isSectionLine(line,new_section_name))
                {
                        if (!section_name.empty())
                        {
                                section.setRoot(section_name,root_);
                                root_->insert(std::make_pair(section_name,section));
                                section_name.clear();
                        }
                        section_name = new_section_name;
                        section.clear();
                }
                else if (isKeyValueLine(line,key,value))
                {
                        section.insert(key,value);
                }
        }

        if (!section_name.empty())
        {
                section.setRoot(section_name,root_);
                root_->insert(std::make_pair(section_name,section));
                section_name.clear();
        }

        //std::cerr << "After Building " << (unsigned long long )root_.get() << "\t" <<root_.use_count() << std::endl;
}
Пример #3
0
char *Script::readLine(char *buf, size_t bufSize) {
	bool inBlockComment = false;
	bool ignoreLine = true;

	char *line = 0;
	do {
		line = readLineIntern(buf, bufSize);
		if (line == 0) {
			return 0;
		}

		if (line[0] == '\0')
			continue;

		ignoreLine = false;

		line = Common::ltrim(line);
		if (isCommentLine(line)) {
			// ignore this line
			ignoreLine = true;
		} else
		if (isStartOfCommentBlock(line)) {
			// mark this and the following lines as comment
			inBlockComment = true;
		} else
		if (isEndOfCommentBlock(line)) {
			// comment is finished, so stop ignoring
			inBlockComment = false;
			// the current line must be skipped, though,
			// as it contains the end-of-comment marker
			ignoreLine = true;
		}

	} while (inBlockComment || ignoreLine);

	return line;
}
Пример #4
0
bool loadImage( std::istream& input, PPM_Image& output )
{
    PPM_ParserState state = PPM_HEADER;
    assert( input.good() );

//    size_t rows = 0, cols = 0, maxValue = 0, r = 0, g = 0, b = 0;

    //
    // read each line
    //
    std::string line;

    while ( input.eof() == false && input.good() )
    {
        // Get the next line
        std::getline( input, line );
        
        // Skip any line that starts with a comment
        if ( isCommentLine( line ) )
        {
            continue;
        }

        //
        // Parse the line
        //
        switch ( state )
        {
            case PPM_HEADER:
                // read dimensions
                if ( line != "P6" )
                {
                    raisePPMError("Invalid file - expected 'P6' header");
                    state = PPM_INVALID;
                }

                // move to next state
                state = PPM_DIMS;
                break;

            case PPM_DIMS:
                break;

            case PPM_DEPTH:
                break;

            case PPM_PIXELS:
                break;

            case PPM_INVALID:
                return false;

            default:
                assert( false );
        }


        std::cout << line << std::endl;
    }

    output.rows = output.rows;
    return false;
}
boost::any CsvRecordParser::next() {
    if(!configured_) {
        throw std::runtime_error("not configured");
    }
    //if we already have a line, consume that
    if((uint64_t)rowIndex_ < row_.size()) {
        if (schema_.find(rowIndex_) == schema_.end() ) {
            dumpDebugInfo();
            std::ostringstream os;
            os << "Invalid schema. Unable to find type for column " << rowIndex_ << ".";
            throw std::runtime_error(os.str());
        }

        boost::any myany;
        bool isNull = false;
        if (row_[rowIndex_] == "") {
            isNull = true;
            myany = CsvRecord(isNull);
        }
        else if (schema_[rowIndex_].second == "logical") {
            bool boolCell;
            std::string stringCell = row_[rowIndex_];
            std::transform(stringCell.begin(), stringCell.end(), stringCell.begin(), ::tolower);
            if (stringCell == "true") {
                boolCell = true;
            } else if (stringCell == "false") {
                boolCell = false;
            } else {
                boolCell = static_cast<bool>(atoll(row_[rowIndex_].c_str()));
            }
            myany = CsvRecord(isNull, boolCell);
        }
        else if (schema_[rowIndex_].second == "integer") {
            myany = CsvRecord(isNull, (int32_t)atoi(row_[rowIndex_].c_str()));
        }
        else if (schema_[rowIndex_].second == "int64") {
            myany = CsvRecord(isNull, (double)atof(row_[rowIndex_].c_str()));
        }
        else if (schema_[rowIndex_].second == "numeric") {
            myany = CsvRecord(isNull, (double)atof(row_[rowIndex_].c_str()));
        }
        else if (schema_[rowIndex_].second == "character") {
            myany = CsvRecord(isNull, row_[rowIndex_]);
        }
        else {
            std::ostringstream os;
            os << "Unsupported type ";
            os << schema_[rowIndex_].second;
            os << ". Supported types are int64, double and string";
            throw std::runtime_error(os.str());
        }
        DLOG_IF(INFO, recordsProduced_ < 10) << "returning record: " << row_[rowIndex_];
        ++recordsProduced_;
        rowIndex_++;
        return myany;
    }
    else if((uint64_t)rowIndex_ == row_.size()) {
        if(rowIndex_ != 0) {
            //notify we're done with line
            if(!observer_) {
                throw std::runtime_error("observer not set");
            }
            observer_->update(0);
        }
        if(splitProducer_->hasNext()) {
            //load new line and reset rowIndex_
            boost::shared_ptr<splitproducer::Split> s;
            std::string line;

            bool isCommentLineFlag = false;
            bool isEmptyLineFlag = false;
            do {
                try {
                    s = splitProducer_->next();
                    line = std::string(const_cast<const char*>(reinterpret_cast<char *>(s->buffer)), s->used);
                }
                catch(const splitproducer::EmptySplitException& e) {
                    DLOG(INFO) << "Split producer returned an empty split";
                    throw;
                }
                /**
                 * Discard comment lines
                 */
                isCommentLineFlag = isCommentLine(line, commentCharacter_);
                if (isCommentLineFlag) {
                    ++commentLinesDiscarded_;
                }
                /**
                 * Discard blank lines
                 */
                isEmptyLineFlag = line == "";
                if (isEmptyLineFlag) {
                    ++blankLinesDiscarded_;
                }
            }
            while (isCommentLineFlag || isEmptyLineFlag);

            std::istringstream ss(line);
            text::csv::csv_istream is(ss, delimiter_);
            is >> row_;
            rowIndex_ = 0;
            return next();
        }
        else {
            throw std::runtime_error("no more records");