void codeslayer_utils_save_gobjects (GList *objects, const gchar *file_path, gpointer name, ...) { gchar *contents; GHashTable *table; va_list var_arg; GFile *file; file = g_file_new_for_path (file_path); if (!g_file_query_exists (file, NULL)) { GFileIOStream *stream; stream = g_file_create_readwrite (file, G_FILE_CREATE_NONE, NULL, NULL); if (g_io_stream_close (G_IO_STREAM (stream), NULL, NULL)) g_object_unref (stream); } va_start (var_arg, name); table = codeslayer_xml_create_hashtable (var_arg); va_end (var_arg); contents = codeslayer_xml_serialize_gobjects (objects, name, table); g_file_set_contents (file_path, contents, -1, NULL); g_object_unref (file); g_free (contents); codeslayer_xml_free_hashtable (table); }
String openTemporaryFile(const String& prefix, PlatformFileHandle& handle) { GUniquePtr<gchar> filename(g_strdup_printf("%s%s", prefix.utf8().data(), createCanonicalUUIDString().utf8().data())); GUniquePtr<gchar> tempPath(g_build_filename(g_get_tmp_dir(), filename.get(), NULL)); GRefPtr<GFile> file = adoptGRef(g_file_new_for_path(tempPath.get())); handle = g_file_create_readwrite(file.get(), G_FILE_CREATE_NONE, 0, 0); if (!isHandleValid(handle)) return String(); return String::fromUTF8(tempPath.get()); }
PlatformFileHandle openFile(const String& path, FileOpenMode mode) { GUniquePtr<gchar> filename = unescapedFilename(path); if (!filename) return invalidPlatformFileHandle; GRefPtr<GFile> file = adoptGRef(g_file_new_for_path(filename.get())); GFileIOStream* ioStream = 0; if (mode == OpenForRead) ioStream = g_file_open_readwrite(file.get(), 0, 0); else if (mode == OpenForWrite) { if (g_file_test(filename.get(), static_cast<GFileTest>(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) ioStream = g_file_open_readwrite(file.get(), 0, 0); else ioStream = g_file_create_readwrite(file.get(), G_FILE_CREATE_NONE, 0, 0); } return ioStream; }
gchar* codeslayer_utils_get_file_path (const gchar *folder_path, const gchar *file_name) { gchar *file_path; GFile *file; file_path = g_build_filename (folder_path, file_name, NULL); file = g_file_new_for_path (file_path); if (!g_file_query_exists (file, NULL)) { GFileIOStream *stream; stream = g_file_create_readwrite (file, G_FILE_CREATE_NONE, NULL, NULL); g_io_stream_close (G_IO_STREAM (stream), NULL, NULL); g_object_unref (stream); } g_object_unref (file); return file_path; }