All pastes #64493 Raw Edit

Stuff

public text v1 · immutable
#64493 ·published 2006-06-11 19:25 UTC
rendered paste body
// oTokenizer - Tokenizes a string
string *oTokenizer(string otInput)
{
	bool otInString1 = false; // Inside single-quoted string
	bool otInString2 = false; // Inside double-quoted string
	string *otToken = (string *) malloc(1 * sizeof(string));

	if (otInput.length() == 0)
		return otToken;

	for (unsigned int i = 0; i < otInput.length(); i++) {
		// Single-quoted string
		if (otInput[i] == '\'') {
			if (otInString1) {
				otInString1 = false;
				realloc(otToken, sizeof(otToken) / sizeof(string) + 1);
				otToken[sizeof(otToken) / sizeof(string) - 1] = "";
			}
			else if (otInString2)
				otToken[sizeof(otToken) / sizeof(string) - 1] += otInput[i];
			else {
				otInString1 = true;

				if (otToken[sizeof(otToken) / sizeof(string) - 1].length() > 0) {
					realloc(otToken, sizeof(otToken) / sizeof(string) + 1);
					otToken[sizeof(otToken) / sizeof(string) - 1] = "";
				}
			}
		}

		// Double-quoted string
		else if (otInput[i] == '"') {
			if (otInString1)
				otToken[sizeof(otToken) / sizeof(string) - 1] += otInput[i];
			else if (otInString2) {
				otInString2 = false;
				realloc(otToken, sizeof(otToken) / sizeof(string) + 1);
				otToken[sizeof(otToken) / sizeof(string) - 1] = "";
			}
			else {
				otInString2 = true;

				if (otToken[sizeof(otToken) / sizeof(string) - 1].length() > 0) {
					realloc(otToken, sizeof(otToken) / sizeof(string) + 1);
					otToken[sizeof(otToken) / sizeof(string) - 1] = "";
				}
			}
		}

		// Space or horizontal tab
		else if (otInput[i] == ' ' || otInput[i] == '\t') {
			if (otInString1 || otInString2)
				otToken[sizeof(otToken) / sizeof(string) - 1] += otInput[i];
			else if (otToken[sizeof(otToken) / sizeof(string) - 1].length() > 0) {
				realloc(otToken, sizeof(otToken) / sizeof(string) + 1);
				otToken[sizeof(otToken) / sizeof(string) - 1] = "";
			}
		}

		// Backslash
		else if (otInput[i] == '\\') {
			if (i < otInput.length() - 1) {
				otToken[sizeof(otToken) / sizeof(string) - 1] += otInput[i + 1];
				i++;
			}
			else
				realloc(otToken, 0);
		}

		// Other
		else
			otToken[sizeof(otToken) / sizeof(string) - 1] += otInput[i];
	}
	return otToken;
}
// oTokenizer