示例#1
0
void StringInterface::functionMid(void) {
	int32_t len = cb->popValue().getInt();
	int32_t pos = cb->popValue().getInt();
	string str = cb->popValue().getString().getRef();

	if (pos <= 0) {
		cb->errors->createError("Mid() failed!", "Second parameter must be greater than 0");
		cb->pushValue(ISString(""));
		return;
	}

	if (pos < 0) {
		// Return remainder of string
		cb->pushValue(str.substr(pos - 1));
	}
	else {
		string::size_type uPos = pos;
		if (uPos - 1 > str.length()) {
			cb->pushValue(ISString(""));
		}
		else {
			cb->pushValue(str.substr(uPos - 1, len));
		}
	}
}
示例#2
0
void StringInterface::functionStrRemove(void) {
	int32_t len = cb->popValue().getInt();
	int32_t pos = cb->popValue().getInt();
	string str = cb->popValue().getString().getRef();

	if (pos <= 0) {
		cb->errors->createError("StrRemove() failed!", "Second parameter must be greater than 0");
		cb->pushValue(ISString(""));
		return;
	}
	if (len < 0) {
		cb->errors->createError("StrRemove() failed!", "Third parameter must be positive");
		cb->pushValue(ISString(""));
		return;
	}

	string::size_type uLen = len;
	string::size_type uPos = pos;

	if (uPos < str.length()) {
		if(uPos - 1 + uLen < str.length()) {
			str.erase(uPos - 1, uLen);
		}
		else {
			str.erase(uPos - 1, str.length());
		}
	}

	cb->pushValue(str);

}
/** Returns the fullscreen display mode in ID as string
 * @param id Id of the display mode. MUST BE in range of 0 to cbeGetGfxModesCount()-1.
 * @returns Display mode as string. I.e. cbeGetGfxMode(0) returns a string "640,480,60,32" (in most cases).
 */
void cbeGetGfxMode(CBEnchanted *cb) {
	// Store the old flags for later restoring
	int32_t oldFlags = al_get_display_flags(cb->gfxInterface->getWindow());

	// Set the display flags for fullscreen
	al_set_new_display_flags(ALLEGRO_FULLSCREEN);

	// Get display modes count
	int32_t displayModesCount = al_get_num_display_modes();

	// Pop the ID from input
	int32_t displayId = cb->popValue().toInt();

	// Check if displayId is valid
	if (displayId < 0 || displayId >= displayModesCount) {
		string id = std::to_string(displayId);
		string count = std::to_string(displayModesCount);
		bool ignore = cb->errors->createError("cbeGetGfxMode() failed!", "Trying to get gfx mode with ID " + id + " out of " + count + " modes\nWhen ignored, the first display mode available in the list is returned.");

		// If ignored, displayId is 0. Otherwise push empty string and return.
		if (ignore) {
			displayId = 0;
		}
		else {
			cb->pushValue(ISString(""));
			return;
		}
	}

	// Where the display modes data is stored
	ALLEGRO_DISPLAY_MODE *displayData = new ALLEGRO_DISPLAY_MODE;

	// Get the display mode with id
	al_get_display_mode(displayId, displayData);

	// Restore old flags
	al_set_new_display_flags(oldFlags);

	if (displayData != NULL) {
		// Initialize the string to be returned
		string displayModeString;

		// Construct the string
		displayModeString =	std::to_string(displayData->width);
		displayModeString += "," + std::to_string(displayData->height);
		displayModeString += "," + std::to_string(displayData->refresh_rate);
		displayModeString += "," + std::to_string(al_get_pixel_format_bits(displayData->format));

		// Return the display mode
		cb->pushValue(displayModeString.substr(0 , displayModeString.length() ));
	}
	else {
		INFO("Something funny happened in cbeGetGfxMode(), you got an empty display mode...")
		cb->pushValue(ISString(""));
	}

	// Free memory
	delete displayData;
}
/** Reads an UTF-8 encoded line and converts all possible characters to Windows-1252. */
void cbeReadLineUTF8(CBEnchanted *cb) {
	int32_t fileId = cb->popValue().getInt();
	FILE* file = cb->fileInterface->getFile(fileId);
	if (file == 0) {
		FIXME("Invalid file pointer given to cbeReadLineUTF8()")
		cb->pushValue(ISString(""));
		return;
	}
	string line = "";
	while(1) {
		int c = fgetc(file);
		if (c != '\r' && c != EOF) {
			if (c != '\n') {
				line = line + char(c);
			}
		}
		else {
			break;
		}
	}
	line += '\0';

	line = utf8toCP1252(line);
	cb->pushValue(line);
}
示例#5
0
void StringInterface::functionStrMove(void) {
	int32_t mov = cb->popValue().getInt();
	int32_t len = cb->popValue().getInt();
	int32_t pos = cb->popValue().getInt();
	string str = cb->popValue().getString().getRef();

	if (pos <= 0) {
		cb->errors->createError("StrMove() failed!", "Second parameter must be greater than 0");
		cb->pushValue(ISString(""));
		return;
	}
	if (len < 0) {
		cb->errors->createError("StrMove() failed!", "Third parameter must be positive");
		cb->pushValue(ISString(""));
		return;
	}
	if (mov < 0) {
		cb->errors->createError("StrMove() failed!", "Fourth parameter must be positive");
		cb->pushValue(ISString(""));
		return;
	}

	string::size_type uMov = mov;
	string::size_type uLen = len;
	string::size_type uPos = pos;

	if(uPos - 1 + uLen > str.length()) {
		cb->pushValue(str);
		return;
	}

	string txt = str.substr(uPos - 1, uLen);
	str.erase(uPos - 1, uLen);

	if (-1 + uPos + uMov > str.length()) {
		str.insert(str.length(), txt);
	}
	else {
		str.insert(-1 + uPos + uMov, txt);
	}

	cb->pushValue(str);
}
示例#6
0
void StringInterface::functionLSet(void) {
	int32_t len = cb->popValue().getInt();
	string str = cb->popValue().getString().getRef();

	if (len < 0) {
		cb->errors->createError("LSet() failed!", "Second parameter must be positive");
		cb->pushValue(ISString(""));
		return;
	}

	str.resize(len, ' ');
	cb->pushValue(str);
}
示例#7
0
ISString Any::toString() const {
	assert(!empty());
	try {
		switch (type()) {
			case Any::Int: {
				ISString i(boost::lexical_cast<string>(getInt()));
				i.requireEncoding(false);
				return i;
			}
			case Any::Float: {
				ISString i(boost::lexical_cast<string>(getFloat()));
				i.requireEncoding(false);
				return i;
			}
			case Any::String: return ISString(dString);
		}
	}
	catch (boost::bad_lexical_cast &) {
		return ISString();
	}
	FIXME("Unsupported cast %s >= %s", typeInfo().name(), typeid(string).name());
	return ISString();
}
示例#8
0
void StringInterface::functionRight(void) {
	int32_t n = cb->popValue().toInt();
	string s = cb->popValue().getString().getRef();

	if (n < 0) {
		cb->errors->createError("Right() failed!", "Second parameter must be positive");
		cb->pushValue(ISString(""));
		return;
	}

	string::size_type uN = n;
	if (uN >= s.length()) {
		cb->pushValue(s);
	} else {
		cb->pushValue(s.substr(s.length() - uN));
	}
}
示例#9
0
void StringInterface::functionRSet(void) {
	int32_t len = cb->popValue().getInt();
	string str = cb->popValue().getString().getRef();

	if (len < 0) {
		cb->errors->createError("RSet() failed!", "Second parameter must be positive");
		cb->pushValue(ISString(""));
		return;
	}

	string::size_type uLen = len;
	if (uLen > str.length()) {
		string str2;
		str2.resize(uLen - str.length(), ' ');
		cb->pushValue(str2 + str);
	}
	else {
		cb->pushValue(str.substr(str.length() - uLen));
	}
}
示例#10
0
void StringInterface::functionStrInsert(void) {
	string txt = cb->popValue().getString().getRef();
	int32_t pos = cb->popValue().getInt();
	string str = cb->popValue().getString().getRef();

	if (pos < 0) {
		cb->errors->createError("StrInsert() failed!", "Second parameter must be positive");
		cb->pushValue(ISString(""));
		return;
	}

	string::size_type uPos = pos;
	if (uPos > str.length()) {
		str.insert(str.length(), txt);
	}
	else {
		str.insert(uPos, txt);
	}

	cb->pushValue(str);

}