Beispiel #1
0
void sampleUsage(){

    // Some example usage of the provided functions.
    // Feel free to use and modify this code to get started on the assignment.
    // We provide most of the string parsing code so you can focus on the data structures and logic.

    string worldlyString = "Hello there world!";

    vector<string> tokens = tokenize(worldlyString, " ");

    for(int i=0; i<tokens.size(); i++){
        cout << tokens.at(i) << endl;
    }
    cout << tokens.size() << endl << endl;

   string line = "var aVariableName =2+15 *(62 - anotherVariableName)*10";

    vector<string> splitLine = tokenize(line, "=");
    if(splitLine.size() > 2 || splitLine.size() < 1){
        // not a valid expression
    }else if(splitLine.size() == 2){
        // line with an assignment
        string lhs = splitLine.at(0);
        string rhs = splitLine.at(1);
        cout << "left hand side: " << endl;
        while(lhs.size() != 0){
            cout << getNextSymbol(lhs) << endl;
        }
        cout << endl << "right hand side: " << endl;
        while(rhs.size() != 0){
            cout << getNextSymbol(rhs) << endl;
        }
    }else{
        // line without an assignment
    }


    cout << endl;
    cout << "expected 1: " << isdigit('5') << endl;
    cout << "expected 1: " << isNumber("5") << endl;
    cout << "expected 1: " << isNumber("50.11") << endl;
    cout << "expected 1: " << isNumber(".000554325") << endl;
    cout << "expected 1: " << isNumber("53241.") << endl;
    cout << "expected 1: " << isNumber("53241.") << endl;
    cout << "expected -1: " << isNumber("") << endl;
    cout << "expected 0: " << isNumber("alphabet") << endl;
    cout << "expected 0: " << isNumber("these are words") << endl;
    cout << "expected 0: " << isNumber("5134589723452345a") << endl;
    cout << "expected 0: " << isNumber("a5134589723452345") << endl;
    cout << "expected 0: " << isNumber("5134589a723452345") << endl;
    cout << "expected 1: " << isNumber("5134589723452345") << endl;
    cout << "expected 0: " << isNumber("5134589723452345..") << endl;
    cout << "expected 0: " << isNumber("..5134589723452345") << endl;
    cout << "expected 0: " << isNumber("513.458972345.2345") << endl;
    cout << "expected 0: " << isNumber("513.4589723452345.") << endl;
    cout << "expected 0: " << isNumber(".513458972345.2345") << endl;

}
Beispiel #2
0
NodePointer Parser::factor()
{
	NodePointer result;
	SymbolType type = currentSymbol_.symbolType_;
	if (TheOperatorFactory::getInstance().isOperator(type))
	{
		if (type == SUBSTRACTION)
		{
			type = MINUS;
		}

		OperatorPointer theOperator = TheOperatorFactory::getInstance().create(type);
		if (!theOperator->isUnary())
		{
			throw ErrorMessage("Unary operator expected", getLineNumber());
		}

		getNextSymbol();
		NodePointer subExpressionTree = expression(theOperator->precedence());
		result = NodePointer(new Expression(theOperator, NodePointerInserter()(subExpressionTree), getLineNumber()));
	}
	else if (type == LEFT_PARENTHESIS)
	{
		getNextSymbol();
		NodePointer subExpressionTree = expression();
		accept(RIGHT_PARENTHESIS);
		result = subExpressionTree;
	}
	else if (type == IDENTIFIER || type == GLOBAL)
	{
		result = NodePointer(new Variable(currentSymbol_.value_.toString(), tables_, type == GLOBAL));
		getNextSymbol();
	}
	else if (type == ARGUMENT)
	{
		result = NodePointer(new Argument(tables_.subroutineStack_, currentSymbol_.value_, getLineNumber()));
		getNextSymbol();
	}
	else if (type == CONSTANT)
	{
		result = NodePointer(new Constant(currentSymbol_.value_));
		getNextSymbol();
	}
	else if (type == CALL)
	{
		return procedureCall();
	}
	else
	{
		throw ErrorMessage("Unexpected symbol " + symbolToString(currentSymbol_), getLineNumber());
	}

	return result;
}
Beispiel #3
0
int COF_getSymbolNum(COF_Handle desc, Long id, COF_Symbol *symbol,
    String name)
{
    if ((desc == NULL) || (symbol == NULL)) {
        return (COF_EBADARG);
    }
    
    if (id >= desc->header.nSyms) {
        return (COF_ESYMBOL);
    }

    /* move to the beginning of the symbol plus an offset*/
    if ((*desc->attrs.fseek)(desc->scanFile, desc->header.symTabPtr 
                         + (id * COF_SYMBOLSIZE), SEEK_SET)) {
        return (COF_EHEADER);
    }

    getNextSymbol(desc, desc->scanFile, symbol);

    if (name) {
        _COF_getName(desc, &symbol->nameDesc, name);
    }

    return (COF_EOK);
}
Beispiel #4
0
NodePointer Parser::printStatement()
{
	NodePointer printStatementTree(new PrintStatement());
	getNextSymbol();
	if (currentSymbol_.symbolType_ != SEMICOLON)
	{
		printStatementTree->add(expression());
		while (currentSymbol_.symbolType_ == COMMA)
		{
			getNextSymbol();
			printStatementTree->add(expression());
		}
	}
	accept(SEMICOLON);
	return printStatementTree;
}
Beispiel #5
0
/*
 *  ======== COF_getCSymbol ========
 *
 *  Retrieve the symbol indicated by '_' + name and return it in *sym.  Return
 *  COF_EOK on success, COF_EBADARG if the parameter "symbol" is null or if
 *  name is not found.
 */
int COF_getCSymbol(COF_Handle desc, String name, COF_Symbol *symbol)
{
    Char        symName[COF_MAXEXPR];
    Int         error = COF_EBADARG;
    Long       i;

    if (symbol == NULL) {
        return (COF_EBADARG);
    }
    
    (*desc->attrs.fseek)(desc->file, desc->header.symTabPtr, SEEK_SET);
    
    for (i = 0; i < desc->header.nSyms; ) {

        i += getNextSymbol(desc, desc->file, symbol);

        _COF_getName(desc, &symbol->nameDesc, symName);

        if (strcmp(name, symName + 1) == 0 && symName[0] == '_') {
            error = COF_EOK;
            break;
        }
    }

    return (error);
}
Beispiel #6
0
NodePointer Parser::procedureCall()
{
	NodeArray arguments;
	SymbolIndex definitionIndex = currentSymbol_.value_.toString();
	getNextSymbol();
	accept(LEFT_PARENTHESIS);
	if (currentSymbol_.symbolType_ != RIGHT_PARENTHESIS)
	{
		arguments.push_back(expression());
		while (currentSymbol_.symbolType_ == COMMA)
		{
			getNextSymbol();
			arguments.push_back(expression());
		}
	}
	accept(RIGHT_PARENTHESIS);
	NodePointer subroutineCall = NodePointer(new SubroutineCall(tables_, definitionIndex, arguments, getLineNumber()));
	return subroutineCall;
}
Beispiel #7
0
void Parser::accept(SymbolType symbolType)
{
	if (currentSymbol_.symbolType_ == symbolType)
	{
		getNextSymbol();
	}
	else
	{
		throw ErrorMessage("Expected " + keywordToString(symbolType), getLineNumber());
	}
}
Beispiel #8
0
NodePointer Parser::returnStatement()
{
	getNextSymbol();
	NodePointer expressionTree;
	if (currentSymbol_.symbolType_ != SEMICOLON)
	{
		expressionTree = expression();
	}
	accept(SEMICOLON);
	NodePointer returnStatement(new ReturnStatement(tables_.subroutineStack_, expressionTree, getLineNumber()));
	return returnStatement;
}
Beispiel #9
0
NodePointer Parser::procedureDefinition()
{
	getNextSymbol();
	std::string name = "&" + currentSymbol_.value_.toString();
	accept(DEFINITION);
	NodePointer definition = blockStatement();
	if (tables_.subroutineTable_.find(name) != tables_.subroutineTable_.end())
	{
		throw ErrorMessage("Multiple definition of subroutine " + name, getLineNumber());
	}
	tables_.subroutineTable_[name] = definition;
	return NodePointer(new EmptyNode());
}
Beispiel #10
0
NodePointer Parser::getNextParseTree()
{
	if (!currentParseTree_)
	{
		getNextSymbol();
	}

	if (currentSymbol_.symbolType_ != END)
	{
		currentParseTree_ = statement();
	}
	else
	{
		currentParseTree_ = NodePointer();
	}

	return currentParseTree_;
}
Beispiel #11
0
/*
 *  ======== COF_nextSymbol ========
 *
 *  Read the next symbol into *symbol, and read the symbol's name into name.
 *  COF_scanSymbols() must be called prior to COF_nextSymbol() calls. Neither
 *  COF_scanReloc() nor COF_nextReloc() may be called between COF_scanSymbols()
 *  and COF_nextSymbol() without saving and restoring the desc->scanFile file
 *  handle.  Returns COF_EBADARG if either desc or symbol is NULL, COF_ESYMBOL
 *  if the symbol cannot be read, or COF_EOK on success.
 */
Int COF_nextSymbol(COF_Handle desc, COF_Symbol *symbol, String name)
{
    if ((desc == NULL) || (symbol == NULL)) {
        return (COF_EBADARG);
    }

    if (desc->curSymIndex >= desc->header.nSyms) {
        return (COF_ESYMBOL);
    }

    desc->curSymIndex += getNextSymbol(desc, desc->scanFile, symbol);

    if (name) {
        _COF_getName(desc, &symbol->nameDesc, name);
    }
    
    return (COF_EOK);
}
Beispiel #12
0
NodePointer Parser::ifStatement()
{
	accept(IF);
	accept(LEFT_PARENTHESIS);
	NodePointer expressionTree = expression();
	accept(RIGHT_PARENTHESIS);
	NodePointer blockStatementTree = blockStatement();
	NodePointer elseStatementTree;
	if (currentSymbol_.symbolType_ == ELSIF)
	{
		// nasty trick splitting 'elsif' into 'else if'
		currentSymbol_.symbolType_ = IF;
		elseStatementTree = ifStatement();
	}
	else if (currentSymbol_.symbolType_ == ELSE)
	{
		getNextSymbol();
		elseStatementTree = blockStatement();
	}
	NodePointer ifStatementTree(new IfStatement(expressionTree, blockStatementTree, elseStatementTree));
	return ifStatementTree;
}
Beispiel #13
0
int COF_getSymbol(COF_Handle desc, String name, COF_Symbol *symbol)
{
    Char        symName[COF_MAXEXPR];
    Int         error = COF_EBADARG;
    Long        i;
    COF_Symbol  temp;

    if (symbol == NULL) {
        return (COF_EBADARG);
    }

    (*desc->attrs.fseek)(desc->file, desc->header.symTabPtr, SEEK_SET);
    
    for (i = 0; i < desc->header.nSyms; ) {

        i += getNextSymbol(desc, desc->file, symbol);

        _COF_getName(desc, &symbol->nameDesc, symName);

        if (strcmp(name, symName) == 0) {
            copySymbol(symbol, &temp);
            /* If we find an external definition, we get out, otherwise save
             * the found one to return it, if we can't find and external one.
             */
            if (symbol->type == COF_C_EXT) {
                error = COF_EOK;
                break;
            }
            else {
                copySymbol(symbol, &temp);
                error = COF_EOK;
            }
        }
    }
    copySymbol(&temp, symbol);
    return (error);
}
Beispiel #14
0
NodePointer Parser::expression(int precedence)
{
	NodePointer expressionTree = factor();
	while (true)
	{
		SymbolType type = currentSymbol_.symbolType_;
		if (TheOperatorFactory::getInstance().isOperator(type))
		{
			OperatorPointer theOperator = TheOperatorFactory::getInstance().create(type);
			if (!theOperator->isBinary())
			{
				throw ErrorMessage("Binary operator expected", getLineNumber());
			}

			int newPrecedence = theOperator->precedence();
			if (newPrecedence < precedence)
			{
				break;
			}

			if (theOperator->assiociativity() == LEFT)
			{
				newPrecedence++;
			}

			getNextSymbol();
			NodePointer subExpressionTree = expression(newPrecedence);
			expressionTree = NodePointer(new Expression(theOperator, NodePointerInserter()(expressionTree)(subExpressionTree), getLineNumber()));
		}
		else
		{
			break;
		}
	}

	return expressionTree;
}
void Interpreter::interpretLine(string lineFromFile, ofstream &outputFile, ifstream &inputFile, bool _eval) {
    if (_eval) {
        LINE_TYPE lineType = getLineType(lineFromFile);
        vector<string> *tokens = new vector<string>();
        vector<string> quotes = tokenize(lineFromFile, "\"");
        vector<string> splitByEqual = tokenize(lineFromFile, "=");

        while (!lineFromFile.empty()) {
            tokens->push_back(getNextSymbol(lineFromFile));
        }

        printVector(*tokens);
        printVector(quotes);
        printVector(splitByEqual);
        //std::cout << "Outside switch";
        //std::cout << "Line type : " << (lineType) << std::endl;

        for (auto it = functionMap.begin(); it != functionMap.end(); it++) {
            //std::cout << it->first << std::endl;
            it->second.tostring();
        }

        //std::cout << std::endl;
        switch (lineType) {
            case DEFINE_VAR: {
                if (isFunction(splitByEqual[1])) {
                    // std::cout << "Passed isFunction";
                    vector<string> parts;
                    vector<double> parameters;
                    while (!splitByEqual[1].empty()) {
                        parts.push_back(getNextSymbol(splitByEqual[1]));
                    }
                    //std::cout << "Before print parts" << std::endl;
                    printVector(parts);

                    int index = 2;
                    while (index < parts.size() - 1) {
                        parameters.push_back(variableMap.at(parts.at(index)));
                        index += 2;
                    }

                    //std::cout << "Hey !" << std::endl;
                    UserFunction function = functionMap.at(parts.front());
                    function.call(parameters, outputFile);
                    double functionReturn = function.thisReturn;

                    //std::cout << "Return value :" << functionReturn << std::endl;
                    string x = tokens->at(0);

                    if (x == "var") {
                        variableMap.insert(make_pair(tokens->at(1), functionReturn));
                    } else {
                        variableMap[x] = functionReturn;
                    }
                } else {
                    // std::cout << "1";
                    string name = tokens->at(1);
                    tokens->erase(tokens->begin(), tokens->begin() + 3);
                    double value = evaluateInfix(*tokens);
                    variableMap.insert(make_pair(name, value));
                }
            }
                break;

            case USER_DEFINED: {
                if (splitByEqual.size() == 2 && isFunction(splitByEqual[1])) {
                    vector<string> parts;
                    vector<double> parameters;
                    while (!splitByEqual[1].empty()) {
                        parts.push_back(getNextSymbol(splitByEqual[1]));
                    }
                    //std::cout << "Before print parts" << std::endl;
                    printVector(parts);

                    int x = 2;
                    while (x < parts.size() - 1) {
                        parameters.push_back(variableMap.at(parts.at(x)));
                        x++;
                        x++;
                    }

                    //std::cout << "Hey !" << std::endl;
                    UserFunction function = functionMap.at(parts.front());
                    function.call(parameters, outputFile);
                    double returnValue = function.thisReturn;
                    variableMap[tokens->at(0)] = returnValue;
                }
                else if (splitByEqual.size() == 1) {
                    vector<string> parts;
                    vector<double> parameters;
                    while (!splitByEqual[0].empty()) {
                        parts.push_back(getNextSymbol(splitByEqual[0]));
                    }
                    // std::cout << "Before print parts" << std::endl;
                    printVector(parts);

                    int x = 2;
                    while (x < parts.size() - 1) {
                        parameters.push_back(variableMap.at(parts.at(x)));
                        x++;
                        x++;
                    }

                    // std::cout << "Hey !" << std::endl;
                    UserFunction function = functionMap.at(parts.front());
                    function.call(parameters, outputFile);
                } else {
                    // std::cout << "2";
                    string name = tokens->at(0);
                    tokens->erase(tokens->begin(), tokens->begin() + 2);
                    double result = evaluateInfix(*tokens);
                    variableMap[name] = result;
                }
            }
                break;
            case DOC_WRITE: {
                //std::cout << "3";
                quotes.size() > 1 ? outputFile << quotes.at(1) : outputFile << variableMap.at(tokens->at(2));
            }
                break;

            case FUNCTION_DEF: {
                int ifCounter = 0;
                UserFunction function(functionMap);
                int closing;
                for (int i = 0; i < tokens->size(); i++) { if (tokens->at(i) == ")") closing = i; }
                for (int i = 3; i < closing; i = i + 2) { function.arguments.push_back(tokens->at(i)); }
                printVector(function.arguments);
                string functionLine;

                while (getline(inputFile, functionLine)) {

                    LINE_TYPE type = getLineType(functionLine);
                    if (type == IF) {
                        ifCounter = ifCounter + 1;
                    }
                    if (type == END_BLOCK) {
                        if (ifCounter == 0) break;
                        else ifCounter = ifCounter - 1;
                    }
                    //if(getLineType(functionLine) == END_BLOCK) break;
                    //   std::cout << ifCounter << " " << functionLine << std::endl;
                    function.functionDefinition.push_back(functionLine);
                }
                functionMap.insert(make_pair(tokens->at(1), function));
            }
                break;

            case RETURN: {
                returnValue = variableMap.at(tokens->at(1));
            }
                break;

            case BLANK_LINE: {
                break;
            }

            case END_BLOCK: {
                if (!conditionStack.empty()) {
                    conditionStack.pop();
                }
                break;
            }
        }
    }
}
Beispiel #16
0
GmlObject *GmlParser::parseList(GmlObjectType closingKey,
	GmlObjectType /* errorKey */)
{
	GmlObject *firstSon = nullptr;
	GmlObject **pPrev = &firstSon;

	for( ; ; ) {
		GmlObjectType symbol = getNextSymbol();

		if (symbol == closingKey || symbol == gmlError)
			return firstSon;

		if (symbol != gmlKey) {
			setError("key expected");
			return firstSon;
		}

		GmlKey key = m_keySymbol;

		symbol = getNextSymbol();
		GmlObject *object = nullptr;

		switch (symbol) {
		case gmlIntValue:
			object = OGDF_NEW GmlObject(key,m_intSymbol);
			break;

		case gmlDoubleValue:
			object = OGDF_NEW GmlObject(key,m_doubleSymbol);
			break;

		case gmlStringValue: {
			size_t len = strlen(m_stringSymbol)+1;
			char *pChar = new char[len];
			if (pChar == nullptr) OGDF_THROW(InsufficientMemoryException);

			strcpy(pChar,m_stringSymbol);
			object = OGDF_NEW GmlObject(key,pChar); }
			break;

		case gmlListBegin:
			object = OGDF_NEW GmlObject(key);
			object->m_pFirstSon = parseList(gmlListEnd,gmlEOF);
			break;

		case gmlListEnd:
			setError("unexpected end of list");
			return firstSon;

		case gmlKey:
			setError("unexpected key");
			return firstSon;

		case gmlEOF:
			setError("missing value");
			return firstSon;

		case gmlError:
			return firstSon;

		OGDF_NODEFAULT // one of the cases above has to occur
		}

		*pPrev = object;
		pPrev = &object->m_pBrother;
	}

	return firstSon;
}
Beispiel #17
0
Int COF_nextReloc(COF_Handle desc, COF_Reloc *reloc, COF_Symbol *sym, 
                  String name)
{
    if ((desc == NULL) || (reloc == NULL)) {
        return (COF_EBADARG);
    }

    if (desc->curRelocIndex++ >= (Long)desc->curSect->nRelocs) {
        return (COF_ESYMBOL);
    }

    if ((*desc->attrs.fread)(reloc, COF_RELOCSIZE, 1, desc->scanFile) != 1) {
        return (COF_ERELOC);
    }

    if (desc->byteSwapped) {
        _COF_swapLong(&reloc->r_vaddr);
        _COF_swapLong(&reloc->r_symndx);
#if defined(_TI_)
        _COF_swapUns(&reloc->r_disp);
#endif
        _COF_swapUns(&reloc->r_type);
    }
    
#if defined(_TI_)
    if (sym) {
        Long       save;
        
        /* symndx == -1 implies relcation is relative to current section */
        if (reloc->r_symndx < 0) {  
            name[0] = '\0';
            return (COF_EOK);
        }

        /* save current file position */
        save = (*desc->attrs.ftell)(desc->scanFile);
        
        /* move to the beginning of the symbol */
        if ((*desc->attrs.fseek)(desc->scanFile,
            desc->header.symTabPtr + (reloc->r_symndx * COF_SYMBOLSIZE),
            SEEK_SET)) {
            return (COF_EHEADER);
        }
    
        if (getNextSymbol(desc, desc->scanFile, sym) == 0) {
            (*desc->attrs.fseek)(desc->scanFile, save, SEEK_SET);
            return (COF_ESYMBOL);
        }
    
        if (name) {
            _COF_getName(desc, &sym->nameDesc, name);
        }

        /* restore current file position */
        (*desc->attrs.fseek)(desc->scanFile, save, SEEK_SET);
    }
#elif defined(_MOT_)                    /* #if defined(_TI_) */
    _COF_USEARG(sym);
    
    if (name) {
        Int nChar;
        
        (*desc->attrs.fseek)(desc->stfile, desc->strTab + reloc->r_symndx, 
                             SEEK_SET);

        /* copy the string from the file; guaranteed to be null-terminated */
        for (nChar = 0; nChar < COF_MAXEXPR; nChar++) {
            if ((*desc->attrs.fread)(name, sizeof(Char), 1, desc->stfile) != 1) {
                return (COF_ERELOC);
            }
            if (*name++ == '\0') {
                break;
            }
        }
        if (nChar >= COF_MAXEXPR) { /* loop above didn't copy '\0' into name */
            return (COF_ERELOC);
        }
    }

#endif                  /* #if defined(_TI_) / #elif defined (_MOT_) */
    return (COF_EOK);
}
Beispiel #18
0
//input arguments: reference, variant-databse, startPos, bitDataVector(s) [at least 1]
int main (int argc, char * const argv[]) {
    
    int startPos;
    
    startPos = atoi(argv[3]);
    
    int inBitV = argc-4;
    
    if (inBitV < 1)
    {
        printf("Required arguments: reference, variant database, startPos, bitVectors [1+]\n");
        exit(9);
    }
    
	FILE * vl;	//input file with variants
	FILE * ref; //input file with reference seuence
	FILE * bitData;
	 char **resName;
    FILE * file;
    resName = new char *[inBitV];
    
	
	int  i, linePos[inBitV], j =0, samePos=0;
	int deleteSeq[inBitV];
    unsigned long insertSeq[inBitV];
    unsigned long prevIns1;
	char * buffer;
    char temp[1000000];
    
    char type[10];
    char ch;
	buffer = new char[BUFFER_SIZE];
	
	char ** buf, **inputBuffer;
	buf = new char*[inBitV];
    inputBuffer = new char*[inBitV];
    
    
	for (i=0; i < inBitV; i++)
	{
        
		buf[i] = new char[BUFFER_SIZE];
	}
	
	int bufferPos = 0 , bufPos[inBitV];
    
	int  currentPos = 0, allele;
    //  unsigned int noVar=0;
	
	bool doubleInsertion=false;
	
    
	
	int  chRef;
    short int prevVariant = NO;
	long int size;
	
    unsigned long int refSize, logTreshold;
    size_t tmpPt;
	
	printf("input reference: %s\n", argv[1]);
	ref = fopen(argv[1], "r");
	if (ref == NULL) {
		printf("Cannot open %s\n", argv[1]);
        printf("The error is - %s\n", strerror(errno));
		exit(8);
	}
	
	printf("input variant database: %s\n", argv[2]);
	vl = fopen(argv[2], "r");
	if (vl == NULL) {
		printf("Cannot open %s\n", argv[2]);
        printf("The error is - %s\n", strerror(errno));
		exit(8);
	}
    
    int noVariants = 0;
    while (EOF != (ch=getc(vl)))
        if ('\n' == ch)
            ++noVariants;
    fseek(vl, 0, SEEK_SET);
    
    variant * vt;
    vt = (variant *) malloc (noVariants * sizeof(variant));
    
    for(i=0; i < noVariants; i++)
    {
        
        fscanf(vl, "%s %u %s ", (char*)&temp, &vt[i].position, type);
        
        if (strstr(type, "SNP") != NULL)  //VT is a SNP
        {
            vt[i].type = SNP;
            vt[i].alt = new char[1];
            fscanf(vl, "%s\n", vt[i].alt);
            vt[i].delLen = 0;
        }
        else if (strstr(type, "INS") != NULL)  //VT is an INS
        {
            vt[i].type = INS;
            fscanf(vl, "%s\n", temp);
            vt[i].alt = new char[strlen(temp)+1];
            memcpy(vt[i].alt , temp, strlen(temp));
            vt[i].alt[strlen(temp)]='\0';
            vt[i].delLen = 0;
            
        }
        else if (strstr(type, "DEL") != NULL)  //VT is an DEL
        {
            vt[i].type = DEL;
            fscanf(vl, "%u\n", &vt[i].delLen);
            vt[i].alt = new char[1];
            vt[i].alt[0] = '-';
        }
        else if (strstr(type, "SV") != NULL)  //VT is an SV
        {
            vt[i].type = SV;
            fscanf(vl, "%u %s\n", &vt[i].delLen, temp);
            vt[i].alt = new char[strlen(temp)+1];
            memcpy(vt[i].alt , temp, strlen(temp));
            vt[i].alt[strlen(temp)]='\0';
        }
    }
    
    
    
    
    for (i=0; i < inBitV; i++)
	{
        
        printf("\rinput bitData: %s (%d of %d) ", argv[4+i], i+1, inBitV);
        bitData = fopen(argv[4+i], "rb");
        if (bitData == NULL) {
            printf("Cannot open %s\n", argv[4+i]);
            printf("The error is - %s\n", strerror(errno));
            exit(8);
        }
        
        fseek(bitData, 0, SEEK_END);
        size = ftell(bitData);
        fseek(bitData, 0, SEEK_SET);
        
        inputBuffer[i] = new char[size];
        fread(inputBuffer[i], 1, size, bitData);
        fclose(bitData);
    }
	
	
	i=0;
    char header[1000];
    chRef = getc(ref);
	while (chRef != '\n') {
		header[i++]=chRef;
        chRef = getc(ref);
	}
    header[i++]='\n';
    header[i]='\0';
    
	
	for (i=0; i < inBitV; i++)
	{
        resName[i] = new char[strlen(argv[4+i])+ 10];
		linePos[i]=0;
		deleteSeq[i]=0;
        insertSeq[i]=0;
		bufPos[i]=0;
        
		resName[i][0]='\0';
		strncat(resName[i], argv[4+i], strlen(argv[4+i]));
		strcat(resName[i], ".fa");
		strcat(resName[i], "\0");
	/*	resultSeq[i] = fopen(resName[i], "w");
		if (resultSeq[i] == NULL) {
			printf("Cannot open %s", resultFileName);
			exit(8);
		}
        delete resultFileName;
*/		
	}
	
    
    for (i=0; i < inBitV; i++)
	{
        file = fopen(resName[i], "w");
        if (file == NULL) {
            printf("Cannot open %s\n", resName[i]);
            printf("The error is - %s\n", strerror(errno));
            exit(8);
        }
        fprintf(file, "%s", header);
        fclose(file);
	}
    
    
    tmpPt = ftell(ref);
    fseek( ref, 0, SEEK_END);
	refSize = ftell(ref);
    fseek( ref, tmpPt, SEEK_SET);
    logTreshold = startPos+ refSize/5;
    
    
    
    printf("\nProcessing...\n");
    
	currentPos = startPos;
    
    int currVT = 0;
    
	
	while (currentPos < vt[currVT].position){
		
		chRef = getNextSymbol(ref);
		
		if(bufferPos == BUFFER_SIZE)
		{
			for (i=0; i < inBitV; i++)
			{
                file = fopen(resName[i], "a");
                fwrite(buffer, 1, BUFFER_SIZE, file);
                fclose(file);
			}
			bufferPos = 0;
		}
		
		buffer[bufferPos]=(255&chRef);
        bufferPos++;
        if(bufferPos == BUFFER_SIZE)
        {
            for (i=0; i < inBitV; i++)
            {
                file = fopen(resName[i], "a");
                fwrite(buffer, 1, BUFFER_SIZE, file);
                fclose(file);
            }
            bufferPos = 0;
        }
        j++;
        if (j == LINE_WIDTH) {
            j=0;
            buffer[bufferPos]= '\n';
            bufferPos++;
        }
		
		
		
		currentPos++;
	}
	for (i=0; i < inBitV; i++)
	{
		file = fopen(resName[i], "a");
        fwrite(buffer, 1, bufferPos, file);
        fclose(file);
    }
	for (i=0; i < inBitV; i++)
	{
		linePos[i] = j;
	}
    
    bool *vtPresent;
	vtPresent = new bool[inBitV];
    int cntAlleles;
	
    
    
    while( currVT < noVariants)
	{
        if(currentPos > logTreshold)
        {
            printf("Processed %.f%% of reference sequence\n", (float)(logTreshold-startPos)/(float)(refSize)*100);
            logTreshold = logTreshold + refSize/5;
        }
        
		while (currentPos < vt[currVT].position) {
			
			chRef = getNextSymbol(ref);
			
            for (i=0; i < inBitV; i++)
			{
				if(deleteSeq[i] > 0)
				{
					deleteSeq[i]--;
				}
				else
				{
					writeChar(chRef, buf[i], bufPos[i], linePos[i], resName[i]);
				}
            }
			currentPos++;
		}
		
        if(!samePos)
			chRef = getNextSymbol(ref);
		
		
		if (vt[currVT].type == SNP)  //VT is a SNP
        {
            
            if(currVT+2 == noVariants|| vt[currVT+1].position > vt[currVT].position || vt[currVT+1].type != SNP )
                cntAlleles = 1;
            else
            {
                cntAlleles = 1;
                while (vt[currVT+cntAlleles].type == SNP && vt[currVT+cntAlleles].position == vt[currVT].position)
                    cntAlleles++;
            }
            
            
            for (j=0; j < inBitV; j++)
            {
                allele=0;
                for(i=0; i < cntAlleles; i++)
                {
                    if( (inputBuffer[j][(currVT+i)/8] & 1<<(7-(currVT+i)%8)) > 0)
                    {
                        allele = i+1;
                        break;
                    }
                    
                }
                
                if(allele > 0) //some variant present
                {
                    if(deleteSeq[j] > 0)
                    {
                        deleteSeq[j]--;
                    }
                    else
                    {
                        writeChar(vt[currVT + allele - 1].alt[0], buf[j], bufPos[j], linePos[j], resName[j]);
                    }
                }
                else
                {
                    if(deleteSeq[j] > 0)
                    {
                        deleteSeq[j]--;
                    }
                    else
                    {
                        writeChar(chRef, buf[j], bufPos[j], linePos[j], resName[j]);
                    }
                }
            }
            prevVariant = SNP;
            
            
        }
        else if (vt[currVT].type == INS)  //VT is an INDEL
        {
            if(currVT+2 == noVariants || vt[currVT+1].position > vt[currVT].position || vt[currVT+1].type != INS)
                cntAlleles = 1;
            else
            {
                cntAlleles = 1;
                while (vt[currVT+cntAlleles].type == INS && vt[currVT+cntAlleles].position == vt[currVT].position)
                    cntAlleles++;
                
            }
            
            
            if(samePos && prevVariant == INS)
            {
                doubleInsertion=true;
            }
            else
                doubleInsertion=false;
            
            
            prevVariant = INS;
            
            
            
            for (j=0; j < inBitV; j++)
            {
                allele=0;
                for(i=0; i < cntAlleles; i++)
                {
                    if( (inputBuffer[j][(currVT+i)/8] & 1<<(7-(currVT+i)%8)) > 0)
                    {
                        allele = i+1;
                        break;
                    }
                    
                }
                
                if(doubleInsertion)
                {
                    prevIns1 = insertSeq[j];
                }
                else
                {
                    prevIns1 = 0;
                }
                
                if(allele > 0) //some variant present
                {
                    if(!samePos)
                    {
                        if(deleteSeq[j] > 0)
                        {
                            deleteSeq[j]--;
                        }
                        else
                        {
                            writeChar(chRef, buf[j], bufPos[j], linePos[j], resName[j]);
                        }
                    }
                    
                    for (i = 0; i < strlen(vt[currVT + allele - 1].alt); i++)
                    {
                        
                        if(doubleInsertion && insertSeq[j] > 0)
                        {
                            insertSeq[j]--;
                        }
                        else
                        {
                            writeChar(vt[currVT + allele - 1].alt[i], buf[j], bufPos[j], linePos[j], resName[j]);
                        }
                        
                    }
                    insertSeq[j]=(strlen(vt[currVT + allele - 1].alt)) > prevIns1 ? (strlen(vt[currVT + allele].alt)) : prevIns1;
                }
                else
                {
                    if(!doubleInsertion)
                    {
                        insertSeq[j]=0;
                        
                    }
                    if(!samePos)
                    {
                        if(deleteSeq[j] > 0)
                        {
                            deleteSeq[j]--;
                        }
                        else
                        {
                            writeChar(chRef, buf[j], bufPos[j], linePos[j], resName[j]);
                        }
                    }
                }
                
            }
            
            
        }
        else if (vt[currVT].type == DEL) //deletion
        {
            prevVariant = DEL;
            
            
            cntAlleles = 1;
            
            for (j=0; j < inBitV; j++)
            {
                allele=0;
                for(i=0; i < cntAlleles; i++)
                {
                    if( (inputBuffer[j][(currVT+i)/8] & 1<<(7-(currVT+i)%8)) > 0)
                    {
                        allele = i+1;
                        break;
                    }
                    
                }
                
                if(allele>0)
                {
                    if(!samePos)
                    {
                        if(deleteSeq[j] > 0)
                        {
                            deleteSeq[j]--;
                        }
                        else
                        {
                            writeChar(chRef, buf[j], bufPos[j], linePos[j], resName[j]);
                        }
                    }
                    deleteSeq[j] = (deleteSeq[j]>(vt[currVT + allele - 1].delLen)) ? deleteSeq[j] :(vt[currVT + allele - 1].delLen) ;
                }
                else
                {
                    if(!samePos)
                    {
                        if(deleteSeq[j] > 0)
                        {
                            deleteSeq[j]--;
                        }
                        else
                        {
                            writeChar(chRef, buf[j], bufPos[j], linePos[j], resName[j]);
                        }
                        
                    }
                }
            }
            
        }
        else if (vt[currVT].type == SV)  //VT is a SV
        {
            
            
            if(vt[currVT].alt[0] == '-')
            {
                vt[currVT].isInsertion=false;
                //prevVariant = DEL;
            }
            else
            {
                if(samePos && prevVariant == INS)
                {
                    vt[currVT].doubleInsertion = true;
                }
                else
                {
                    vt[currVT].doubleInsertion = false;
                }
                vt[currVT].isInsertion=true;
                //prevVariant = INS;
            }
            
            
            //sth different than a SV at the same pos - do nothing
            if(currVT+2 == noVariants || vt[currVT+1].position > vt[currVT].position || vt[currVT+1].type != SV)
                cntAlleles = 1;
            else
            {
                
                cntAlleles = 1;
                while (vt[currVT+cntAlleles].type == SV && vt[currVT+cntAlleles].position == vt[currVT].position)
                {
                    if(vt[currVT + cntAlleles].alt[0] == '-')
                    {
                        vt[currVT+cntAlleles].isInsertion=false;
                        // prevVariant = DEL;
                    }
                    else
                    {
                        if(samePos && prevVariant == INS)
                        {
                            vt[currVT+cntAlleles].doubleInsertion = true;
                        }
                        else
                        {
                            vt[currVT+cntAlleles].doubleInsertion = false;
                        }
                        vt[currVT+cntAlleles].isInsertion = true;
                        // prevVariant = INS;
                    }
                    cntAlleles++;
                    
                }
                
            }
            
            
            for (j=0; j < inBitV; j++)
            {
                allele = 0;
                for(i=0; i < cntAlleles; i++)
                {
                    if( (inputBuffer[j][(currVT+i)/8] & 1<<(7-(currVT+i)%8)) > 0)
                    {
                        allele = i+1;
                        break;
                    }
                    
                }
                if(allele > 0)
                {
                    if(!samePos)
                    {
                        if(deleteSeq[j] > 0)
                        {
                            deleteSeq[j]--;
                        }
                        else
                        {
                            writeChar(chRef, buf[j], bufPos[j], linePos[j], resName[j]);
                        }
                    }
                    
                    deleteSeq[j] = (deleteSeq[j] > vt[currVT + allele - 1].delLen) ? deleteSeq[j] : vt[currVT + allele - 1].delLen ;
                    
                    if(vt[currVT + allele - 1].isInsertion)
                    {
                        
                        if(vt[currVT + allele - 1].doubleInsertion)
                        {
                            prevIns1 = insertSeq[j];
                        }
                        else
                        {
                            prevIns1 = 0;
                        }
                        for (i = 0; i < strlen(vt[currVT + allele - 1].alt); i++)
                        {
                            
                            if(vt[currVT + allele - 1].doubleInsertion && insertSeq[j] > 0)
                            {
                                insertSeq[j]--;
                            }
                            else
                            {
                                writeChar(vt[currVT + allele - 1].alt[i], buf[j], bufPos[j], linePos[j], resName[j]);
                            }
                            
                        }
                        insertSeq[j]=(strlen(vt[currVT + allele - 1].alt)) > prevIns1 ? (strlen(vt[currVT + allele - 1].alt)) : prevIns1;
                    }
                }
                else
                {
                    if(!samePos)
                    {
                        if(deleteSeq[j] > 0)
                        {
                            deleteSeq[j]--;
                        }
                        else
                        {
                            writeChar(chRef, buf[j], bufPos[j], linePos[j], resName[j]);
                        }
                    }
                    
                    
                    for (int a = 0; a < cntAlleles; a++)
                    {
                        if(vt[currVT + a].isInsertion && !vt[currVT + a].doubleInsertion)
                        {
                            insertSeq[j]=0;
                        }
                    }
                }
            }
            
            
            
        }
        else{ printf("Unknown vt");}
        
        currVT = currVT + cntAlleles;
		samePos = 0;
		if(vt[currVT].position == currentPos)
			samePos = 1;
		else
			currentPos++;
        
        
        
	}
	
	chRef = getNextSymbol(ref);
	
	while (!feof(ref))
	{
		
		for (i=0; i < inBitV; i++)
		{
			if(deleteSeq[i] > 0)
			{
				deleteSeq[i]--;
			}
			else
			{
				writeChar(chRef, buf[i], bufPos[i], linePos[i], resName[i]);
			}
        }
		currentPos++;
		chRef = getNextSymbol(ref);
	}
	
	
    for (i=0; i < inBitV; i++)
	{
        file = fopen(resName[i], "a");
        fwrite(buf[i], 1, bufPos[i], file);
        fclose(file);
	}
	
	
	fclose(ref);
	fclose(vl);
    
    printf("Processed 100%% of reference sequence\n");
    
    
    return 0;
}