Beispiel #1
0
	/**
	 * Read a data object from a file.
	 * @param filePath Full path of file to read.
	 * @param inPlaceholder Placeholder handle for data object.
	 * @return true on success, false on error.
	 */
	bool FileUtil::readDataFromFile(
		const MAUtil::String& filePath,
		MAHandle inPlaceholder)
	{

		MAHandle file = openFileForReading(filePath);
		if (file < 0)
		{
			return false;
		}

		int size = maFileSize(file);
		if (size < 1)
		{
			return false;
		}

		int result = maCreateData(inPlaceholder, size);
		if (RES_OK != result)
		{
			return false;
		}

		result = maFileReadToData(file, inPlaceholder, 0, size);

		maFileClose(file);

		return result == 0;
	}
Controller::~Controller() {
  // remember the selected font scale and path
  char path[FILENAME_MAX + 1];
  getcwd(path, FILENAME_MAX);
  int pathLength = strlen(path) + 1;
  int dataLength = (sizeof(int) * 3) + pathLength;
  MAHandle data = maCreatePlaceholder();

  if (maCreateData(data, dataLength) == RES_OK) {
    int storeVersion = STORE_VERSION;
    int offset = 0;

    // write the version number
    maWriteData(data, &storeVersion, offset, sizeof(int));
    offset += sizeof(int);

    // write the fontScale
    maWriteData(data, &_fontScale, offset, sizeof(int));
    offset += sizeof(int);
    
    // write the current path
    maWriteData(data, &pathLength, offset, sizeof(int));
    maWriteData(data, path, offset + sizeof(int), pathLength);

    MAHandle store = maOpenStore(PACKAGE, MAS_CREATE_IF_NECESSARY);
    maWriteStore(store, data);
    maCloseStore(store, 0);
  }
  maDestroyPlaceholder(data);

  delete _output;
  delete [] _programSrc;
}
Beispiel #3
0
/**
 * Used for Android only.
 * Stores the registration ID in a store for later use.
 * @param token The registration_ID.
 */
void MainScreen::storeRegistrationID(MAUtil::String* token)
{
	mToken = token;

	// Store doesn't exist.
	MAHandle myStore = maOpenStore("MyPushStore", MAS_CREATE_IF_NECESSARY);

	// Create store and write Registration ID
	MAHandle myData = maCreatePlaceholder();
	if(maCreateData(myData, token->length()) == RES_OK)
	{
		 maWriteData(myData, token->c_str(), 0, token->length());
		 // Write the data resource to the store.
		 int result = maWriteStore(myStore, myData);

		 if ( result < 0 )
		 {
			 printf("Cannot write to store the token!!");
		 }
		 maCloseStore(myStore, 0);
	}

	// Finally, send it over TCP to the server.
//	mConnection->sendData(token);
}
void ImageCache::httpFinished(MAUtil::HttpConnection* http, int result) {
	if (result == 200) {
		MAUtil::String *contentLengthStr = new MAUtil::String("-1");
		int responseBytes = mHttp.getResponseHeader("content-length", contentLengthStr);
		mContentLength = 0;
		mDataOffset = 0;
		mData = maCreatePlaceholder();
		if(responseBytes == CONNERR_NOHEADER) {

		} else {
			mContentLength = atoi(contentLengthStr->c_str());
		}
		delete contentLengthStr;
		if (maCreateData(mData, mContentLength) == RES_OK){

		}

		if(mContentLength >= 1024 || mContentLength == 0) {
			mHttp.recv(mBuffer, 1024);
		} else {
			mBuffer[mContentLength] = 0;
			mHttp.recv(mBuffer, mContentLength);
		}
	}
	else {
		finishedDownloading();
	}
}
Beispiel #5
0
extern "C" int MAMain() {
	int res;

	InitConsole();

	printf("CreateData\n");
	maCreateData(RES, 386);
	printf("DestroyObject\n");
	maDestroyObject(RES);

	printf("HTTP GET\n");
	//res = myHttpGetToObject(1, "http://dev.mopix.se:8080/testing/mobile.php");
	{
		MAUtil::HttpGetToObject http("http://link.astando.se/astwms/Request.aspx?REQUEST=GetMap&SERVICE=WMS&VERSION=1.1.1&LAYERS=TO_Vattenytor,TO_Oppen_mark,TO_Byggnadsytor,TO_Vagkonturer,Vagkanter,TO_Geografiska_texter,TO_Allm_byggnadsnamn,Gatunamn&STYLES=&FORMAT=png&BGCOLOR=0xFF0000&TRANSPARENT=TRUE&SRS=EPSG:3152&BBOX=78202.0228224145,99374.2252203757,79546.909389597,100413.827113304&WIDTH=240&HEIGHT=188&reaspect=false", RES);
		res = http.state();
		while(res == 0) {
			EVENT event;
			maWait(0);
			while(maGetEvent(&event)) {
				if(event.type == EVENT_TYPE_CLOSE ||
					(event.type == EVENT_TYPE_KEY_PRESSED && event.key == MAK_0))
				{
					maExit(0);
				} else if(event.type == EVENT_TYPE_CONN) {
					printf("cE %i %i %i\n", event.conn.handle, event.conn.opType, event.conn.result);
					http.notify(event.conn);
					res = http.state();
				}
			}
		}
	}
	printf("Got %i\n", res);
	FREEZE;
}
Beispiel #6
0
	int Syscall::loadBinaryStore(int resourceIndex, int size)
	{
		char* b = (char*)malloc(200);
		sprintf(b, "loadBinaryStore index:%d size:%d", resourceIndex, size);
		__android_log_write(ANDROID_LOG_INFO, "MoSync Syscall", b);
		free(b);
		return maCreateData(resourceIndex, size);
	}
Beispiel #7
0
MAHandle FacebookResponse::getImageData() const {
	MAHandle data = maCreatePlaceholder();
	maCreateData(data, getDataSize());
	maWriteData(data, getData(), 0, getDataSize());
	MAHandle image = maCreatePlaceholder();
	maCreateImageFromData(image, data, 0, getDataSize());
	maDestroyPlaceholder(data);
	return image;
}
MAHandle FacebookResponse::getImageData() const {
	MAHandle data = MAUtil::PlaceholderPool::alloc();
	maCreateData(data, getDataSize());
	maWriteData(data, getData(), 0, getDataSize());
	MAHandle image = MAUtil::PlaceholderPool::alloc();
	maCreateImageFromData(image, data, 0, getDataSize());
	MAUtil::PlaceholderPool::put(data);
	return image;
}
Beispiel #9
0
void Database::write(MAHandle h) const {
	maDestroyObject(h);
	maCreateData(h, calculateDatabaseSize());
	DataHandler data(h);

	int nDevices = devices.size();
	data.write(&nDevices, sizeof(int));
	for(int i=0; i<nDevices; i++) {
		writeDevice(data, devices[i]);
	}
}
Beispiel #10
0
// save status to a store, so the loader can read it.
// if this store is not saved, the loader will assume something went
// catastrophically wrong with the test.
static void exit_status_save(int status, void* dummy) {
	printf("exit_status_save\n");
	// we don't check for errors here, because there can be no reasonable error handling at this stage.
	// better then, to let the runtime's panic system deal with it.
	MAHandle data = maCreatePlaceholder();
	maCreateData(data, sizeof(int));
	maWriteData(data, &status, 0, sizeof(int));
	MAHandle store = maOpenStore("exit_status", MAS_CREATE_IF_NECESSARY);
	maWriteStore(store, data);
	maCloseStore(store, 0);
}
void HighLevelReaderThatReadsChunks::finishedDownloadingChunkedData()
{
    // Allocate big handle and copy the chunks to it.
    // mContentLength holds the accumulated size of read data.
    // We create a new placeholder here, not using the pool.
    // TODO: Consider using PlaceholderPool also for this handle.
    MAHandle dataHandle = maCreatePlaceholder();
    int errorCode = maCreateData(dataHandle, mContentLength);
    if (RES_OUT_OF_MEMORY == errorCode)
    {
        mConnection->downloadError(RES_OUT_OF_MEMORY);
        return;
    }

    // Copy chunks to the data object.
    int offset = 0;
    char* buf = new char[mDataChunkSize];
    while (0 < mDataChunks.size())
    {
        // Last chunk should only be partially written.
        int dataLeftToWrite = mContentLength - offset;

        // Set size to min(dataLeftToWrite, mDataChunkSize)
        int size = (dataLeftToWrite < mDataChunkSize
                    ? dataLeftToWrite : mDataChunkSize);

        // Copy first remaining chunk.
        MAHandle chunk = mDataChunks[0];
        maReadData(chunk, buf, 0, size);
        maWriteData(dataHandle, buf, offset, size);

        // Return chunk to pool.
        DeallocateHandle(chunk);

        // Remove chunk from list.
        mDataChunks.remove(0);

        // Increment offset.
        offset += mDataChunkSize;
    }
    delete[] buf;

    // Download is finished! Tell the connection about this.
    mConnection->downloadSuccess(dataHandle);
}
bool HighLevelReaderThatReadsChunks::readNextChunk()
{
    // Allocate new a chunk of data.
    MAHandle chunk = AllocateHandle();
    int result = maCreateData(chunk, mDataChunkSize);
    if (RES_OUT_OF_MEMORY == result)
    {
        return false;
    }
    else
    {
        // Start reading into the new chunk.
        mDataChunks.add(chunk);
        mDataChunkOffset = 0;
        mConnection->recvToData(chunk, mDataChunkOffset, mDataChunkSize);
        return true;
    }
}
void CBHttpConnection::httpFinished(MAUtil::HttpConnection *conn, int result) {
	if ( result >= 0) {
		String contentLengthStr;
		theConnection.getResponseHeader("Content-Length",
			&contentLengthStr);
		int contentLength = 0;
		contentLength = atoi(contentLengthStr.c_str());
		//printf("received content length: %s", contentLengthStr.c_str());
		mBuffer[contentLength] = 0;
		if (this->isDownload) {
			this->downloadFileData = maCreatePlaceholder();
			maCreateData(this->downloadFileData,(contentLength == NULL?CONNECTION_BUFFER_SIZE:contentLength));
			theConnection.readToData(this->downloadFileData, 0, (contentLength == NULL?CONNECTION_BUFFER_SIZE:contentLength));
		} else {
			theConnection.read(mBuffer, (contentLength == NULL?CONNECTION_BUFFER_SIZE:contentLength));
		}
	} else {
		theConnection.close();
	}
}
Beispiel #14
0
	void testPlaceHolder() {
		maCreateData(RES_PLACEHOLDER, 16);

		assert("maGetDataSize", (maGetDataSize(RES_PLACEHOLDER) == 16));

		for(int i = 0; i < 4; i++)
			maWriteData(RES_PLACEHOLDER, &i, i*4, 4);

		bool failed = false;

		for(int i = 0; i < 4; i++) {
			int dest;
			maReadData(RES_PLACEHOLDER, &dest, i*4, 4);
			if(dest != i) {
				failed = true;
				break;
			}
		}
		assert("Placeholder test", !failed);

	}
	/**
	 * Loads an image from a file and returns the handle to it.
	 *
	 * @param imagePath relative path to the image file.
	 */
	MAHandle ResourceMessageHandler::loadImageResource(const char* imagePath)
	{
		if (!getFileUtil())
		{
			return 0;
		}

		// Get the current apllication directory path.
		String appPath = getFileUtil()->getAppPath();

		// Construct image path.
		char completePath[2048];
		sprintf(completePath,
				"%s%s",
				appPath.c_str(),
				imagePath);

		// Load the image and create a data handle from it.
		MAHandle imageFile = maFileOpen(completePath, MA_ACCESS_READ);
		if (imageFile < 0)
		{
			return 0;
		}

		int fileSize = maFileSize(imageFile);
		if (fileSize < 1)
		{
			return 0;
		}

		// Create buffer to hold file data.
		MAHandle fileData = maCreatePlaceholder();
		int result = maCreateData(fileData, fileSize);
		if (RES_OK != result)
		{
			maDestroyPlaceholder(fileData);
			return 0;
		}

		// Read data from file.
		result = maFileReadToData(imageFile, fileData, 0, fileSize);
		maFileClose(imageFile);
		if (result < 0)
		{
			maDestroyPlaceholder(fileData);
			return 0;
		}

		// Create image.
		MAHandle imageHandle = maCreatePlaceholder();
		result = maCreateImageFromData(
				imageHandle,
				fileData,
				0,
				maGetDataSize(fileData));
		maDestroyPlaceholder(fileData);
		if (RES_OK != result)
		{
			maDestroyPlaceholder(imageHandle);
			return 0;
		}

		// Return the handle to the loaded image.
		return imageHandle;
	}
Beispiel #16
0
	/**
	 * Copy a file. Overwrites the destination file.
	 * @return 0 on success <0 on error.
	 */
	static int FileCopyFile(
		const String& sourcePath,
		const String& destinationPath)
	{
		// Open source file.
		MAHandle sourceFile = maFileOpen(sourcePath.c_str(), MA_ACCESS_READ_WRITE);
		if (sourceFile < 0)
		{
			return -1;
		}

		// Check that source file exists.
		int exists = maFileExists(sourceFile);
		if (1 != exists)
		{
			maFileClose(sourceFile);
			return -1;
		}

		// Get and check source size.
		int fileSize = maFileSize(sourceFile);
		if (fileSize < 0)
		{
			maFileClose(sourceFile);
			return -1;
		}

		// Create data object for source data to copy.
		MAHandle data = maCreatePlaceholder();
		int createDataResult = maCreateData(data, fileSize);
		if (RES_OK != createDataResult)
		{
			maFileClose(sourceFile);
			maDestroyPlaceholder(data);
			return -1;
		}

		int readResult = maFileReadToData(sourceFile, data, 0, fileSize);
		if (readResult < 0)
		{
			maFileClose(sourceFile);
			maDestroyPlaceholder(data);
			return -1;
		}

		// This deletes the destination file if it already exists.
		FileDeleteFile(destinationPath);

		// Create destination file.
		bool createSuccess = FileCreatePath(destinationPath);
		if (!createSuccess)
		{
			maFileClose(sourceFile);
			maDestroyPlaceholder(data);
			return -1;
		}

		// Open destination file.
		MAHandle destinationFile = maFileOpen(destinationPath.c_str(), MA_ACCESS_READ_WRITE);
		if (destinationFile < 0)
		{
			maFileClose(sourceFile);
			maDestroyPlaceholder(data);
			return -1;
		}

		// Write data to destination file.
		int writeResult = maFileWriteFromData(destinationFile, data, 0, fileSize);
		if (writeResult < 0)
		{
			maFileClose(sourceFile);
			maFileClose(destinationFile);
			maDestroyPlaceholder(data);
			return -1;
		}

		// Close files and free data object.
		maFileClose(sourceFile);
		maFileClose(destinationFile);
		maDestroyPlaceholder(data);

		// Success.
		return 0;
	}