コード例 #1
0
int ReadFile(ExtensionString filename, ExtensionString encoding, std::string& contents)
{
    if (encoding != "utf8") {
        return ERR_UNSUPPORTED_ENCODING;
    }

    int error = NO_ERROR;
    GError *gerror = NULL;
    gchar *file_get_contents = NULL;
    gsize len = 0;
    
    if (!g_file_get_contents(filename.c_str(), &file_get_contents, &len, &gerror)) {
        error = GErrorToErrorCode(gerror);
        if (error == ERR_NOT_FILE) {
            error = ERR_CANT_READ;
        }
    } else {
        if (has_utf16_32_BOM(file_get_contents, len)) {
            error = ERR_UNSUPPORTED_ENCODING;
        } else  if (has_utf8_BOM(file_get_contents, len)) {
            contents.assign(file_get_contents + utf8_BOM_Len, len);        
        } else if (!g_locale_to_utf8(file_get_contents, -1, NULL, NULL, &gerror)) {
            error = ERR_UNSUPPORTED_ENCODING;
        } else {
            contents.assign(file_get_contents, len);
        }
        g_free(file_get_contents);
    }

    return error;
}
コード例 #2
0
int _deleteFile(GFile *file)
{
    int error = NO_ERROR;
    GError *gerror = NULL;

    if (!g_file_delete(file, NULL, &gerror)) {
        error = GErrorToErrorCode(gerror);
    }

    return error;
}
コード例 #3
0
int32 MakeDir(ExtensionString path, int mode)
{
    const char *pathStr = path.c_str();
    GFile *file;
    GError *gerror = NULL;
    int32 error = NO_ERROR;

    if (g_file_test(pathStr, G_FILE_TEST_EXISTS)) {
        return ERR_FILE_EXISTS;
    }

    file = g_file_new_for_path(pathStr);
    mode = mode | 0777;

    if (!g_file_make_directory(file, NULL, &gerror)) {
        error = GErrorToErrorCode(gerror);
    }
    g_object_unref(file);

    return error;
}