Exemplo n.º 1
0
char* substring(const char *value, const char *start, const char *end) {
	int i;
	int len = strlen(value);

	int startCount = 0;
	int startLen = (start == NULL) ? 0 : strlen(start);
	bool startDetected = (start == NULL);
	int startPos = -1;

	int endCount = 0;
	int endLen = (end == NULL) ? 0 : strlen(end);
	bool endDetected = (end == NULL);
	int endPos = -1;

	for (i = 0; i < len; i++) {
		char c = value[i];

		if (startDetected) {

			if ((startPos < 0) && alphanumeric(c)) {
				startPos = i;
			}

			if (!endDetected && c == end[endCount++]) {
				if (endCount == endLen) {
					endDetected = true;
					break;
				}
			} else {
				for (int j = (i - (endCount - (endDetected ? 0 : 1))); j <= i; j++)  {
					if (alphanumeric(value[j])) {
						endPos = j;
					}
				}

				endCount = 0;
			}
		} else {
			if (c == start[startCount++]) {
				if (startCount == startLen) {
					startDetected = true;
				}
			} else {
				startCount = 0;
			}
		}
	}

	if (endDetected && ((endPos - startPos) > -1)) {
		char *subValue = (char *) malloc((endPos - startPos)+2);
		for (i = startPos; i <= endPos; i++) {
			subValue[i - startPos] = value[i];
		}
		subValue[i - startPos] = 0;
		return subValue;
	}

	return NULL;
}
Exemplo n.º 2
0
void junk(void) {
    if (alphanumeric (inbyte ()))
        while (alphanumeric (ch ()))
            gch ();
    else
        while (alphanumeric (ch ())) {
            if (ch () == 0)
                break;
            gch ();
        }
    blanks ();
}
Exemplo n.º 3
0
/**
 * add new symbol to local table
 * @param sname
 * @param identity
 * @param type
 * @param offset size in bytes
 * @param storage_class
 * @return 
 */
int add_local (char *sname, int identity, int type, int offset, int storage_class) {
    int k;
    SYMBOL *symbol;
    char *buffer_ptr;

    if ((current_symbol_table_idx = find_locale (sname)) > -1) {
        return (current_symbol_table_idx);
    }
    if (local_table_index >= NUMBER_OF_GLOBALS + NUMBER_OF_LOCALS) {
        error ("local symbol table overflow");
        return (0);
    }
    current_symbol_table_idx = local_table_index;
    symbol = &symbol_table[current_symbol_table_idx];
    buffer_ptr = symbol->name;
    /* FIXME: only copy so many bytes */
    while (alphanumeric(*buffer_ptr++ = *sname++));
    symbol->identity = identity;
    symbol->type = type;
    symbol->storage = storage_class;
    if (storage_class == LSTATIC) {
        data_segment_gdata();
        print_label(k = getlabel());
        output_label_terminator();
        gen_def_storage();
        output_number(offset);
        newline();
        code_segment_gtext();
        offset = k;
    }
    symbol->offset = offset;
    local_table_index++;
    return (current_symbol_table_idx);
}
Exemplo n.º 4
0
/**
 * compares two string both must be zero ended, otherwise no match found
 * ensures that the entire token is examined
 * @param str1
 * @param str2
 * @param len
 * @return
 */
int astreq(char str1[], char str2[], int len) {
    int k;
    k = 0;
    while (k < len) {
        if ((str1[k] != str2[k]))
            break;
        if (str1[k] == 0)
            break;
        if (str2[k] == 0)
            break;
        k++;
    }
    if (alphanumeric (str1[k]))
        return (0);
    if (alphanumeric (str2[k]))
        return (0);
    return (k);
}
Exemplo n.º 5
0
/**
 * compares two string both must be zero ended, otherwise no match found
 * advances line pointer only if match found
 * it assumes that an alphanumeric (including underscore) comparison
 * is being made and guarantees that all of the token in the source line is
 * scanned in the process
 * @param lit
 * @param len
 * @return 
 */
int amatch(char *lit, int len) {
    int k;

    blanks();
    if ((k = astreq (line + lptr, lit, len)) != 0) {
        lptr = lptr + k;
        while (alphanumeric (ch ()))
            inbyte ();
        return (1);
    }
    return (0);
}
Exemplo n.º 6
0
std::deque<Bid> SearchEngine::processAds(std::string inStr) {
    // Bring input to lowercase
    std::string line;
    std::transform(inStr.begin(),inStr.end(),inStr.begin(),::tolower);

    // Get only the first word
    std::stringstream ss;
    ss << inStr;
    ss >> inStr;
    // And then the rest of the line
    getline(ss,line);

    // Add on blank space to allow last word to be processed if AND/OR
    line += "\n";

    // Make set of words used
    Set<std::string> searchterms;
    if(inStr == "and" || inStr == "or") {
        std::string word;
        for(unsigned int i=0; i<line.size(); ++i)    {
            if(alphanumeric(line[i])) word += line[i];
            else    {
                // This means we're at end of the word - check for content
                if(word.size() > 0) {
                    // Put the query to lowercase and try to get; if not, make an entry if necessary
                    std::transform(word.begin(),word.end(),word.begin(),::tolower);
                    // Then add it to result (doesn't matter if already contained for insert function)
                    searchterms.insert(word);
                    // Reset word for the next word in query
                    word = "";
                }
            }
        }
    }
    else searchterms.insert(inStr);

    // Carter's search algorithm, reimplemented
    std::deque<Bid> relevantBids;
    Set<std::string> companies;
    for(std::deque<Bid>::iterator it = bids.begin(); it != bids.end(); ++it)
    {
        if(searchterms.count(it->searchTerm) == 1 && companies.count(it->company) == 0) // checks if search term included
        {
            // Should be in order
            relevantBids.push_back(*it);
            companies.insert(it->company);
        }
    }

    return relevantBids;
}
Exemplo n.º 7
0
/**
 * test if next input string is legal symbol name
 */
int symname(char *sname) {
    int k;

    blanks();
    if (!alpha (ch ()))
        return (0);
    k = 0;
    while (alphanumeric(ch ()))
        if (k < NAMEMAX)
            sname[k++] = gch ();
        else
            gch();

    sname[k] = 0;
    return (1);
}
Exemplo n.º 8
0
/**
 * add new symbol to global table
 * @param sname
 * @param identity
 * @param type
 * @param offset size in bytes
 * @param storage
 * @return new index
 */
int add_global (char *sname, int identity, int type, int offset, int storage) {
    SYMBOL *symbol;
    char *buffer_ptr;
    if ((current_symbol_table_idx = find_global(sname)) > -1) {
        return (current_symbol_table_idx);
    }
    if (global_table_index >= NUMBER_OF_GLOBALS) {
        error ("global symbol table overflow");
        return (0);
    }
    current_symbol_table_idx = global_table_index;
    symbol = &symbol_table[current_symbol_table_idx];
    buffer_ptr = symbol->name;
    /* FIXME: only copy so many bytes */
    while (alphanumeric(*buffer_ptr++ = *sname++));
    symbol->identity = identity;
    symbol->type = type;
    symbol->storage = storage;
    symbol->offset = offset;
    global_table_index++;
    return (current_symbol_table_idx);
}
Exemplo n.º 9
0
// What to do when the user issues an or command
Set<WebPage*> SearchEngine::processOR(const std::string line)  {
    Set<WebPage*> result,singleword;
    std::string word="";

    // Go through and generate all the queries
    for(unsigned int i=0; i<line.size(); i++)    {
        if(alphanumeric(line[i])) word += line[i];
        else    {
            // This means we're at end of the word - check for content
            if(word.size() > 0) {
                // Put the query to lowercase and try to get; if not, make an entry if necessary
                std::transform(word.begin(),word.end(),word.begin(),::tolower);
                singleword = processSingle(word,"");
                // Then add it to result
                if(result.empty()) result = singleword;
                else result = result.setUnion(singleword);
                // Reset word for the next word in query
                word = "";
            }
        }
    }
    return result;
}
Exemplo n.º 10
0
std::shared_ptr< Expression> parse(std::istream& input, int& line_no, bool topLevel) {
	std::string buffer("");
	char c;
	
	while (input.good()) {
		input.get(c);
		if (whitespace(c)) {
			if (c == '\n') {
				line_no++;
			}
			if (buffer.empty()) {
				;//nothing to do
			} else {
				if (digit(buffer[0])) {
					return std::make_shared<Float>(buffer);
				} else if (doubleQuote(buffer[0])) {
					buffer += c;
				} else if (colon(buffer[0])) {
					return std::make_shared<Atom>(buffer.substr(1));
				} else if (alphanumeric(buffer[0])) {
					return std::make_shared<Identifier>(buffer);
				//} else {
				//	return new Identifier(buffer);
				}
			}
		} else if (digit(c)) {
			buffer += c;
		} else if (dot(c)) {
			if (buffer.empty()) {
				buffer += c;
			} else {
				if (digit(buffer[0])) {
					if (!dotIn(buffer)) {
						buffer += c;
					} else {
						SYNTAX_ERROR(line_no, "Second dot found in Float literal " << buffer + c );
					}
				} else {
					buffer += c;
				}
			}
		} else if (colon(c)) {
			if (buffer.empty()) {
				buffer += c;
			} else {
				SYNTAX_ERROR(line_no, "Colon found in the middle of token " << buffer + c );
			}
		} else if (doubleQuote(c)) {
			if (buffer.empty()) {
				buffer += c;
			} else {
				if (doubleQuote(buffer[0])) {
					buffer += c;
					return std::make_shared<String>(buffer);
				} else {
					SYNTAX_ERROR(line_no, "Double-quote found in the middle of token " << buffer + c );
				}
			}
		} else if (parenthenesis(c)) { //BEWARE - THE TRICKY PART //TODO refactor to smaller functions
			if (buffer.empty()) {
				if (openParenthenesis(c)) {
					std::vector< std::shared_ptr<Expression>> list;
					char c2;
					input.get(c2);
					while (!closeParenthenesis(c2)) {
						input.unget();
						std::shared_ptr<Expression> tmpExp = parse(input, line_no, false);
						if (tmpExp) {
						    list.push_back(tmpExp);
						} else {
						    input.unget();
						}
						input.get(c2);
					}
					if (!parenMatches(c, c2)) {
						SYNTAX_ERROR(line_no, "Parenthenesis " << c << " is closed by not matching paren : " << c2);
					}
					return std::make_shared<List>(list);
				} else {//closeParenthenesis(c)
					if (topLevel) {
					    SYNTAX_ERROR(line_no, "Found unmatched close parenthenesis " << c);
					} else {
					    return NULL;//oznacza, ze trzeba wyjsc
					}
				}
			} else {
				//end of some token
				//return c back to the ctream and return expression as a result
				//or possibly it was ending paren inside a string and we add it simply
                          
				if (digit(buffer[0])) {
					input.unget();
					return std::make_shared<Float>(buffer);
				} else if (doubleQuote(buffer[0])) {
					buffer += c; //no unget, cause it's inside a string literal
                                } else if (colon(buffer[0])) {
					input.unget();
					return std::make_shared<Atom>(buffer);
				} else if (alphanumeric(buffer[0])) {
					input.unget();
					return std::make_shared<Identifier>(buffer);
				}  else {
					SYNTAX_ERROR(line_no, "Situation impossible : buffer == " << buffer);
				}
			}
		} else if (alphanumeric(c)) {
			if (buffer.empty()) {
				buffer += c;
			} else {
				if (!digit(buffer[0])) {
					buffer += c;
				} else {
					SYNTAX_ERROR(line_no, "Non-digit found in the middle of Float literal " << buffer + c );
				}
			}
		} else {
			SYNTAX_ERROR(line_no, "Unrecognised char " << c);
		}
	}
}