Esempio n. 1
0
/*!
	Save the screenshot to the file with the specified filename and type.
	Note that any existing file with the same filename will be overwritten
	without warning.
*/
status_t
Utility::Save(BBitmap** screenshot, const char* fileName, uint32 imageType)
	const
{
	BString fileNameString(fileName);

	// Generate a default filename when none is given
	if (fileNameString.Compare("") == 0) {
		BPath homePath;
		if (find_directory(B_USER_DIRECTORY, &homePath) != B_OK)
			return B_ERROR;

		BEntry entry;
		int32 index = 1;
		BString extension = GetFileNameExtension(imageType);
		do {
			fileNameString.SetTo(homePath.Path());
			fileNameString << "/" << B_TRANSLATE(sDefaultFileNameBase) << index++ 
				<< extension;
			entry.SetTo(fileNameString.String());
		} while (entry.Exists());
	}

	// Create the file
	BFile file(fileNameString, B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY);
	if (file.InitCheck() != B_OK)
		return B_ERROR;

	// Write the screenshot bitmap to the file
	BBitmapStream stream(*screenshot);
	BTranslatorRoster* roster = BTranslatorRoster::Default();
	roster->Translate(&stream, NULL, NULL, &file, imageType,
		B_TRANSLATOR_BITMAP);
	*screenshot = NULL;

	// Set the file MIME attribute
	BNodeInfo nodeInfo(&file);
	if (nodeInfo.InitCheck() != B_OK)
		return B_ERROR;

	nodeInfo.SetType(_GetMimeString(imageType));

	return B_OK;
}
Esempio n. 2
0
	std::string FixFilePath(const char* fileName, const char* basePath, const char* extension)
	{
		if ( !g_Files->Exists(fileName) )
		{
			std::string fileNameString(fileName);
			std::string basePathString(basePath);
			std::string extensionString(extension);

			if (fileNameString.substr( 0, basePathString.length() ) != basePathString)
				fileNameString = basePathString + fileNameString;

			if (g_Files->GetExt(fileName) == "")
				fileNameString.append(extensionString);
			
			return fileNameString;
		}

		return std::string(fileName);
	}
Esempio n. 3
0
FileMap::FileMap(const char *fileName) : fd(0), ptr(NULL) {
#ifdef WIN32

#ifdef UNICODE
    int stringLength = (int)strlen(fileName)+1;
    int bufferLength = MultiByteToWideChar(CP_ACP, 0, fileName, stringLength, 0, 0);
    wchar_t *buf = new wchar_t[bufferLength];
    MultiByteToWideChar(CP_ACP,0, fileName, stringLength, buf, bufferLength);
    wstring fileNameString(buf);
    delete [] buf;
#else
    string fileNameString(fileName);
#endif

    // Open file
    fd = CreateFile( (WCHAR*)fileNameString.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);
    if (fd==INVALID_HANDLE_VALUE) {
        throw "Error creating file for mapping.";
    }

    // Guess size
    DWORD lower, upper;
    lower = GetFileSize(fd,&upper);
    mappedSize = lower | ((uint64_t)upper<<32);

    // Create mapping
    h = CreateFileMapping (fd, NULL, PAGE_READONLY, upper, lower, NULL);
    if (h==NULL) {
        CloseHandle(fd);
        throw "Error creating mapping on file";
    }

    // Get pointer
    ptr = (unsigned char*) MapViewOfFile (h, FILE_MAP_READ, 0, 0, 0);
    if (ptr==NULL) {
        CloseHandle(fd);
        CloseHandle(h);
        DWORD err = GetLastError();
        cerr << "Error getting pointer: " << err << endl;
        throw "Error getting pointer of the mapped file";
    }
#else
    // Linux and OSX

	// Open file
	fd = open(fileName, O_RDONLY);
	if(fd<=0) {
		perror("mmap");
		throw "Error opening HDT file for mapping.";
	}

	// Guess size
	struct stat statbuf;
	if(stat(fileName,&statbuf)!=0) {
		perror("Error on stat()");
		throw "Error trying to guess the file size";
	}
	mappedSize = statbuf.st_size;

	// Do mmap
	ptr = (unsigned char *) mmap(0, mappedSize, PROT_READ, MAP_PRIVATE, fd, 0);
	if(ptr==MAP_FAILED) {
		perror("Error on mmap");
		throw "Error trying to mmap HDT file";
	}

	// Mark as needed so the OS keeps as much as possible in memory
    madvise((void*)ptr, mappedSize, MADV_WILLNEED);

#endif
}