Commits

Alex Szpakowski committed a25ea35

Added the getString method to all Data objects (issue #608)

Comments (0)

Files changed (8)

src/common/wrap_Data.cpp

 	return luax_checktype<Data>(L, idx, "Data", DATA_T);
 }
 
+int w_Data_getString(lua_State *L)
+{
+	Data *t = luax_checkdata(L, 1);
+	lua_pushlstring(L, (const char *) t->getData(), (size_t) t->getSize());
+	return 1;
+}
+
 int w_Data_getSize(lua_State *L)
 {
 	Data *t = luax_checkdata(L, 1);
 
 const luaL_Reg w_Data_functions[] =
 {
+	{ "getString", w_Data_getString },
 	{ "getSize", w_Data_getSize },
 	{ 0, 0 }
 };

src/common/wrap_Data.h

 {
 
 Data *luax_checkdata(lua_State *L, int idx);
+int w_Data_getString(lua_State *L);
 int w_Data_getSize(lua_State *L);
 int w_Data_open(lua_State *L);
 

src/modules/filesystem/physfs/wrap_FileData.cpp

 
 static const luaL_Reg w_FileData_functions[] =
 {
-
 	// Data
+	{ "getString", w_Data_getString },
 	{ "getSize", w_Data_getSize },
 
 	{ "getFilename", w_FileData_getFilename },

src/modules/image/wrap_CompressedData.cpp

 	return luax_checktype<CompressedData>(L, idx, "CompressedData", IMAGE_COMPRESSED_DATA_T);
 }
 
+int w_CompressedData_getString(lua_State *L)
+{
+	CompressedData *t = luax_checkcompresseddata(L, 1);
+
+	// CompressedData's data isn't contiguous in memory, so we need to copy it
+	// all to a single block before sending the string.
+
+	size_t totalsize = 0;
+
+	for (int i = 0; i < t->getNumMipmaps(); i++)
+		totalsize += t->getSize(i);
+
+	if (totalsize == 0)
+	{
+		lua_pushstring(L, "");
+		return 1;
+	}
+
+	char *datastr = 0;
+	try
+	{
+		datastr = new char[totalsize];
+	}
+	catch (std::exception &)
+	{
+		return luaL_error(L, "Out of memory.");
+	}
+
+	size_t curpos = 0;
+	for (int i = 0; i < t->getNumMipmaps(); i++)
+	{
+		memcpy(&datastr[curpos], t->getData(i), t->getSize(i));
+		curpos += t->getSize(i);
+	}
+
+	lua_pushlstring(L, datastr, totalsize);
+
+	delete[] datastr;
+
+	return 1;
+}
+
+int w_CompressedData_getSize(lua_State *L)
+{
+	CompressedData *t = luax_checkcompresseddata(L, 1);
+
+	size_t totalsize = 0;
+
+	for (int i = 0; i < t->getNumMipmaps(); i++)
+		totalsize += t->getSize(i);
+
+	lua_pushnumber(L, (lua_Number) totalsize);
+	
+	return 1;
+}
+
 int w_CompressedData_getWidth(lua_State *L)
 {
 	CompressedData *t = luax_checkcompresseddata(L, 1);
 static const luaL_Reg functions[] =
 {
 	// Data
-	{ "getSize", w_Data_getSize },
+	// CompressedData's data is not in contiguous memory, so it needs custom functions.
+	{ "getString", w_CompressedData_getString },
+	{ "getSize", w_CompressedData_getSize },
 
 	{ "getWidth", w_CompressedData_getWidth },
 	{ "getHeight", w_CompressedData_getHeight },

src/modules/image/wrap_CompressedData.h

 {
 
 CompressedData *luax_checkcompresseddata(lua_State *L, int idx);
+int w_CompressedData_getString(lua_State *L);
+int w_CompressedData_getSize(lua_State *L);
 int w_CompressedData_getWidth(lua_State *L);
 int w_CompressedData_getHeight(lua_State *L);
 int w_CompressedData_getDimensions(lua_State *L);

src/modules/image/wrap_ImageData.cpp

 	return 0;
 }
 
-int w_ImageData_getString(lua_State *L)
-{
-	ImageData *t = luax_checkimagedata(L, 1);
-	lua_pushlstring(L, (const char *)t->getData(), t->getSize());
-	return 1;
-}
-
 int w_ImageData_paste(lua_State *L)
 {
 	ImageData *t = luax_checkimagedata(L, 1);
 static const luaL_Reg functions[] =
 {
 	// Data
+	{ "getString", w_Data_getString },
 	{ "getSize", w_Data_getSize },
 
 	{ "getWidth", w_ImageData_getWidth },
 	{ "getPixel", w_ImageData_getPixel },
 	{ "setPixel", w_ImageData_setPixel },
 	{ "mapPixel", w_ImageData_mapPixel },
-	{ "getString", w_ImageData_getString },
 	{ "paste", w_ImageData_paste },
 	{ "encode", w_ImageData_encode },
 	{ 0, 0 }

src/modules/image/wrap_ImageData.h

 int w_ImageData_getPixel(lua_State *L);
 int w_ImageData_setPixel(lua_State *L);
 int w_ImageData_mapPixel(lua_State *L);
-int w_ImageData_getString(lua_State *L);
 int w_ImageData_paste(lua_State *L);
 int w_ImageData_encode(lua_State *L);
 extern "C" int luaopen_imagedata(lua_State *L);

src/modules/sound/wrap_SoundData.cpp

 
 static const luaL_Reg functions[] =
 {
-
 	// Data
+	{ "getString", w_Data_getString },
 	{ "getSize", w_Data_getSize },
 
 	{ "getChannels", w_SoundData_getChannels },