bool SingleLineDelimTextFileReader::detectAndHandleHeader()
{
	if (!isHeaderLine(_sLine)) {
		return false;
	}
	if (!_fullHeaderFound) {
		_header += _sLine;
		_header += "\n"; //add new line, since it was chomped by getline
	}
	return true;
}
Example #2
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;
	}
};
bool SingleLineDelimTextFileReader::detectAndHandleHeader()
{
	//not sure why the linker is giving me a hard time about
	//passing a non-const string to isHeaderLine, but
	//this const ref is a workaround.
	const string &sLine2 = _sLine;
	if (!isHeaderLine(sLine2) && (!(_inheader && _lineNum==1))) {
		return false;
	}
	if (!_fullHeaderFound) {
		_header += _sLine;
		_header += "\n"; //add new line, since it was chomped by getline
	}
	return true;
}
Example #4
0
void VcfFile::parseHeader() {
  if ( isHeaderLine() ) {
    lineTokens.ReplaceColumns(line, '\t');
    verifyHeaderLine();
    if ( lineTokens.Length() > 8 ) {
      int sampleCount = lineTokens.Length() - 9;
      vpVcfInds.resize(sampleCount);
      for(int i=0; i < sampleCount; ++i) {
	vpVcfInds[i] = new VcfInd(lineTokens[9+i]);
      }
    }
    else {
      for(int i=0; i < (int) vpVcfInds.size(); ++i) {
	delete vpVcfInds[i];
      }
      vpVcfInds.clear();
    }
  }
  else {
    throw VcfFileException("Header line is not found : #CHROM...");
  }
}
Example #5
0
void LatexLexer::styleText(int start,int end)
{
    // Ajouter support verbatim
    // Colorier les erreurs de syntaxe
    if (!editor())
        return;

    char *lineChars = new char[end - start+1];
    editor()->SendScintilla(QsciScintilla::SCI_GETTEXTRANGE, start, end, lineChars);
    QString source = QString(lineChars);
    free(lineChars);

    startStyling(start,0xFF);
    QStringList list=source.split('\n');
    int lineIndex=editor()->SendScintilla(QsciScintilla::SCI_LINEFROMPOSITION, start);

    int style=Default,nextStyle=Default;
    if(lineIndex>0){
        int pos = editor()->SendScintilla(QsciScintilla::SCI_GETLINEENDPOSITION, lineIndex-1 );
        if(pos>0){
            int s = editor()->SendScintilla(QsciScintilla::SCI_GETSTYLEAT, pos);
            if(s==MathInline || s==MathDisplay || s==Environment){
                style=s;
                nextStyle=s;
            }
        }
    }

    int level=QsciScintilla::SC_FOLDLEVELBASE;
    if(lineIndex>0){
        QString previousLine = getLine(lineIndex-1);
        level=(editor()->SendScintilla(QsciScintilla::SCI_GETFOLDLEVEL,lineIndex-1)&QsciScintilla::SC_FOLDLEVELNUMBERMASK)+getDiffLevel(previousLine);
    }

    int nbCharsToStyle;
    int styleBeforeMathDisplay=Default,styleBeforeMathInline=Default,styleInBrace=Default,styleBeforeBrace=Default;
    for (int i = 0; i < list.size(); i++) {
        QString line=list.at(i);
        int lineLength=line.size();
        if(style!=MathInline && style!=MathDisplay && style!=Environment){
            style=Default;
            nextStyle=Default;
        }
        int j=0;
        while(j<lineLength){
            QString l=line.mid(j);
            if(l.startsWith("\\%")){
                nbCharsToStyle=2;
                nextStyle=style;
                style=Command;
            }
            else if(l.startsWith("%")){
                nbCharsToStyle=lineLength-j;
                style=Comment;
                nextStyle=Comment;
            }
            else if(l.startsWith("\\[")){
                nbCharsToStyle=2;
                styleBeforeMathDisplay=style;
                style=Command;
                nextStyle=MathDisplay;
            }
            else if(l.startsWith("\\]")){
                nbCharsToStyle=2;
                style=Command;
                nextStyle=styleBeforeMathDisplay;
            }
            else if(l.startsWith("\\(")){
                nbCharsToStyle=2;
                styleBeforeMathInline=style;
                style=Command;
                nextStyle=MathInline;
            }
            else if(l.startsWith("\\)")){
                nbCharsToStyle=2;
                style=Command;
                nextStyle=styleBeforeMathInline;
            }
            else if(l.startsWith("\\$") || l.startsWith("\\{") || l.startsWith("\\}")){
                nbCharsToStyle=2;
                nextStyle=style;
                style=Command;
            }
            else if(l.startsWith("\\")){
                nbCharsToStyle=1;
                nextStyle=style;
                while(nbCharsToStyle<l.size() && l.at(nbCharsToStyle).isLetter()){
                    nbCharsToStyle++;
                }
                QString s=l.left(nbCharsToStyle);
                if(s=="\\begin" || s=="\\end"){
                    styleInBrace=Environment;
                }
                else if(s=="\\part"){
                    styleInBrace=Part;
                }
                else if(s=="\\chapter"){
                    styleInBrace=Chapter;
                }
                else if(s=="\\section"){
                    styleInBrace=Section;
                }
                else if(s=="\\subsection"){
                    styleInBrace=SubSection;
                }
                else if(s=="\\subsubsection"){
                    styleInBrace=SubSubSection;
                }
                else if(s=="\\paragraph"){
                    styleInBrace=Paragraph;
                }
                else if(s=="\\subparagraph"){
                    styleInBrace=SubParagraph;
                }
                style=Command;
            }
            else if(l.startsWith("$")){
                nbCharsToStyle=1;
                if(style==MathInline){
                    nextStyle=styleBeforeMathInline;
                }
                else{
                    styleBeforeMathInline=style;
                    nextStyle=MathInline;
                }
                style=SpecialChar;
            }
            else if(l.at(0).isDigit()){
                bool isNumber=true;
                nbCharsToStyle=1;
                nextStyle=style;
                while(isNumber && nbCharsToStyle<l.size()){
                    nbCharsToStyle++;
                    l.left(nbCharsToStyle).toFloat(&isNumber);
                }
                if(!isNumber){
                    nbCharsToStyle--;
                }
                style=Digit;
            }
            else if(l.startsWith("(")||l.startsWith(")")||l.startsWith("[")||l.startsWith("]")||l.startsWith("&")||l.startsWith("#")){
                nbCharsToStyle=1;
                nextStyle=style;
                style=SpecialChar;
            }
            else if(l.startsWith("{")){
                nbCharsToStyle=1;
                styleBeforeBrace=style;
                nextStyle=styleInBrace;
                style=SpecialChar;
            }
            else if(l.startsWith("}")){
                nbCharsToStyle=1;
                nextStyle=styleBeforeBrace;
                style=SpecialChar;
                styleInBrace=Default;
            }
            else{
                nbCharsToStyle=1;
                if(l.at(0)!=' '){
                    styleInBrace=style;
                }
            }
            setStyling(nbCharsToStyle,defaultStyle(style));
            style=nextStyle;
            j+=nbCharsToStyle;

        }
        setStyling(1,defaultStyle(style));


        QString s=trimComment(line);
        if(s.contains("\\part")){
            level=2000;

        }
        else if(s.contains("\\chapter")){
            level=2001;

        }
        else if(s.contains("\\section")){
            level=2002;

        }
        else if(s.contains("\\subsection")){
            level=2003;

        }
        else if(s.contains("\\subsubsection")){
            level=2004;

        }
        else if(s.contains("\\paragraph")){
            level=2005;

        }
        else if(s.contains("\\subparagraph")){
            level=2006;

        }
        if(isHeaderLine(s)){
            editor()->SendScintilla(QsciScintilla::SCI_SETFOLDLEVEL,lineIndex,level|QsciScintilla::SC_FOLDLEVELHEADERFLAG);
        }
        /*
        else if(line.trimmed().isEmpty()){
            editor()->SendScintilla(QsciScintilla::SCI_SETFOLDLEVEL,lineIndex,level|QsciScintilla::SC_FOLDLEVELWHITEFLAG);
        }
        */
        else{
            editor()->SendScintilla(QsciScintilla::SCI_SETFOLDLEVEL,lineIndex,level);

        }
        level+=getDiffLevel(s);

        lineIndex++;
    }
}
bool FileRecordTypeChecker::isTextDelimtedFormat(const char *buffer, size_t len)
{
	//Break single string buffer into vector of QuickStrings. Delimiter is newline.
	_tokenizer.setKeepFinalIncompleteElem(Tokenizer::IGNORE);
	int numLines = _tokenizer.tokenize(buffer, '\n', _eofHit);

	//anticipated delimiter characters are tab, comma, and semi-colon.
	//If we need new ones, they must be added in this method.
	//search each line for delimiter characters.

	vector<int> tabCounts;
	vector<int> commaCounts;
	vector<int> semicolonCounts;

	tabCounts.reserve(numLines);
	commaCounts.reserve(numLines);
	semicolonCounts.reserve(numLines);

	//loop through the lines, ignoring headers. Count potential delimiters,
	//see if we can find a few lines with the same number of a given delimiter.
	//delims are tested in hierarchical order, starting with tab,then comma, then semi-colon.

	int validLinesFound=0;
	int headerCount = 0;
	int emptyLines = 0;
	for (int i=0; i < numLines; i++ ) {

		if (validLinesFound >=4) {
			break; //really only need to look at like 4 lines of data, max.
		}
		const QuickString &line = _tokenizer.getElem(i);
		int len =line.size();
		//skip over any empty line
		if (len == 0) {
			emptyLines++;
			continue;
		}

		//skip over any header line
		if (isHeaderLine(line)) {
			//clear any previously found supposedly valid data lines, because valid lines can only come after header lines.
			if (_firstValidDataLineIdx > -1 && _firstValidDataLineIdx < i) {
				_firstValidDataLineIdx = -1;
				validLinesFound--;
				headerCount++;
			}
			headerCount++;
			continue;
		}
		//a line must have some alphanumeric characters in order to be valid.
		bool hasAlphaNum = false;
		for (int j=0; j < len; j++) {
			if(isalnum(line[j])) {
				hasAlphaNum = true;
				break;
			}
		}
		if (!hasAlphaNum) {
			continue;
		}

		validLinesFound++;
		if (_firstValidDataLineIdx == -1) {
			_firstValidDataLineIdx = i;
		}

		int lineTabCount = 0, lineCommaCount=0, lineSemicolonCount =0;
		for (int j=0; j < len; j++) {
			char currChar = line[j];
			if (currChar == '\t') {
				lineTabCount++;
			} else if (currChar == ',') {
				lineCommaCount++;
			} else if (currChar == ';') {
				lineSemicolonCount++;
			}
		}
		tabCounts.push_back(lineTabCount);
		commaCounts.push_back(lineCommaCount);
		semicolonCounts.push_back(lineSemicolonCount);
	}

	if (headerCount + emptyLines == numLines) {
		_insufficientData = true;
	}
	if (validLinesFound == 0) {
		return false;
	}
	_insufficientData = false;

	if (delimiterTesting(tabCounts, '\t')) {
		return true;
	}
	if (delimiterTesting(commaCounts, ',')) {
		return true;
	}
	if (delimiterTesting(semicolonCounts, ';')) {
		return true;
	}

	return false; //unable to detect delimited file.
}