2008-09-13 22:46:03 -05:00
|
|
|
/*
|
2010-11-23 21:54:39 -08:00
|
|
|
* moofilewriter.c
|
2008-09-13 22:46:03 -05:00
|
|
|
*
|
2010-12-21 20:15:45 -08:00
|
|
|
* Copyright (C) 2004-2010 by Yevgen Muntyan <emuntyan@users.sourceforge.net>
|
2008-09-13 22:46:03 -05:00
|
|
|
*
|
|
|
|
* This file is part of medit. medit is free software; you can
|
|
|
|
* redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2.1 of the License,
|
|
|
|
* or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with medit. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
2010-11-23 21:54:39 -08:00
|
|
|
#include "mooutils/moofilewriter-private.h"
|
2008-09-13 22:46:03 -05:00
|
|
|
#include "mooutils/mootype-macros.h"
|
|
|
|
#include "mooutils/mooutils-fs.h"
|
2010-01-18 23:58:33 -08:00
|
|
|
#include "mooutils/mooutils-misc.h"
|
2008-09-13 22:46:03 -05:00
|
|
|
#include "mooutils/mooi18n.h"
|
2010-11-07 03:32:15 -08:00
|
|
|
#include "mooutils/moocompat.h"
|
2010-09-23 01:20:26 -07:00
|
|
|
#include <mooutils/mooutils-tests.h>
|
2008-09-13 22:46:03 -05:00
|
|
|
#include <stdio.h>
|
2009-11-01 12:44:46 -08:00
|
|
|
#include <string.h>
|
2015-12-25 18:07:33 -08:00
|
|
|
#include <mooglib/moo-glib.h>
|
2008-09-14 03:01:18 -05:00
|
|
|
#include <gio/gio.h>
|
2008-09-13 22:46:03 -05:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
using namespace moo;
|
|
|
|
|
2008-09-13 22:46:03 -05:00
|
|
|
/************************************************************************/
|
|
|
|
/* MooFileReader
|
|
|
|
*/
|
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
MooFileReader::MooFileReader()
|
|
|
|
: file(nullptr)
|
2008-09-13 22:46:03 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
void MooFileReader::close_file ()
|
2008-09-13 22:46:03 -05:00
|
|
|
{
|
2016-01-19 03:54:06 -08:00
|
|
|
if (file)
|
2008-09-13 22:46:03 -05:00
|
|
|
{
|
2016-01-19 03:54:06 -08:00
|
|
|
mgw_fclose (file);
|
|
|
|
file = nullptr;
|
2008-09-13 22:46:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
MooFileReader::~MooFileReader()
|
2008-09-13 22:46:03 -05:00
|
|
|
{
|
2016-01-19 03:54:06 -08:00
|
|
|
close_file();
|
2008-09-13 22:46:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
moo_file_reader_close (MooFileReader *reader)
|
|
|
|
{
|
2016-01-19 03:54:06 -08:00
|
|
|
delete reader;
|
2008-09-13 22:46:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static MooFileReader *
|
|
|
|
moo_file_reader_new_real (const char *filename,
|
|
|
|
gboolean binary,
|
2016-01-19 03:54:06 -08:00
|
|
|
gerrp& error)
|
2008-09-13 22:46:03 -05:00
|
|
|
{
|
|
|
|
const char *mode = binary ? "rb" : "r";
|
2015-12-25 21:30:53 -08:00
|
|
|
MGW_FILE *file;
|
2008-09-13 22:46:03 -05:00
|
|
|
MooFileReader *reader;
|
2015-12-25 21:30:53 -08:00
|
|
|
mgw_errno_t err;
|
2008-09-13 22:46:03 -05:00
|
|
|
|
|
|
|
g_return_val_if_fail (filename != NULL, NULL);
|
2016-01-19 03:54:06 -08:00
|
|
|
g_return_val_if_fail (!error, NULL);
|
2008-09-13 22:46:03 -05:00
|
|
|
|
2015-12-25 21:30:53 -08:00
|
|
|
if (!(file = mgw_fopen (filename, mode, &err)))
|
2008-09-13 22:46:03 -05:00
|
|
|
{
|
2016-01-19 03:54:06 -08:00
|
|
|
g_set_error (&error, MOO_FILE_ERROR,
|
2008-09-13 22:46:03 -05:00
|
|
|
_moo_file_error_from_errno (err),
|
|
|
|
_("Could not open %s: %s"), filename,
|
2015-12-25 21:30:53 -08:00
|
|
|
mgw_strerror (err));
|
2008-09-13 22:46:03 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
reader = new MooFileReader();
|
2008-09-13 22:46:03 -05:00
|
|
|
reader->file = file;
|
|
|
|
|
|
|
|
return reader;
|
|
|
|
}
|
|
|
|
|
|
|
|
MooFileReader *
|
2016-01-19 03:54:06 -08:00
|
|
|
moo_file_reader_new (const char* filename,
|
|
|
|
gerrp& error)
|
2008-09-13 22:46:03 -05:00
|
|
|
{
|
|
|
|
return moo_file_reader_new_real (filename, TRUE, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
MooFileReader *
|
2016-01-19 03:54:06 -08:00
|
|
|
moo_text_reader_new (const char* filename,
|
|
|
|
gerrp& error)
|
2008-09-13 22:46:03 -05:00
|
|
|
{
|
|
|
|
return moo_file_reader_new_real (filename, FALSE, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
moo_file_reader_read (MooFileReader *reader,
|
|
|
|
char *buf,
|
|
|
|
gsize buf_size,
|
|
|
|
gsize *size_read_p,
|
2016-01-19 03:54:06 -08:00
|
|
|
gerrp& error)
|
2008-09-13 22:46:03 -05:00
|
|
|
{
|
|
|
|
gsize size_read;
|
2015-12-25 21:30:53 -08:00
|
|
|
mgw_errno_t err;
|
2008-09-13 22:46:03 -05:00
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
g_return_val_if_fail (reader != nullptr, FALSE);
|
2008-09-13 22:46:03 -05:00
|
|
|
g_return_val_if_fail (size_read_p != NULL, FALSE);
|
2016-01-19 03:54:06 -08:00
|
|
|
g_return_val_if_fail (!error, FALSE);
|
2008-09-13 22:46:03 -05:00
|
|
|
g_return_val_if_fail (reader->file != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (buf_size == 0 || buf != NULL, FALSE);
|
|
|
|
|
|
|
|
if (buf_size == 0)
|
|
|
|
return TRUE;
|
|
|
|
|
2015-12-25 21:30:53 -08:00
|
|
|
size_read = mgw_fread (buf, 1, buf_size, reader->file, &err);
|
2008-09-13 22:46:03 -05:00
|
|
|
|
2015-12-25 21:30:53 -08:00
|
|
|
if (size_read != buf_size && mgw_ferror (reader->file))
|
2008-09-13 22:46:03 -05:00
|
|
|
{
|
2016-01-19 03:54:06 -08:00
|
|
|
g_set_error (&error, MOO_FILE_ERROR,
|
2008-09-13 22:46:03 -05:00
|
|
|
_moo_file_error_from_errno (err),
|
|
|
|
"error reading file: %s",
|
2015-12-25 21:30:53 -08:00
|
|
|
mgw_strerror (err));
|
2008-09-13 22:46:03 -05:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*size_read_p = size_read;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
/* MooFileWriter
|
|
|
|
*/
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
moo_file_writer_write (MooFileWriter *writer,
|
|
|
|
const char *data,
|
|
|
|
gssize len)
|
|
|
|
{
|
2016-01-19 03:54:06 -08:00
|
|
|
g_return_val_if_fail (writer != nullptr, FALSE);
|
2008-09-13 22:46:03 -05:00
|
|
|
g_return_val_if_fail (data != NULL, FALSE);
|
|
|
|
|
|
|
|
if (len < 0)
|
|
|
|
len = strlen (data);
|
|
|
|
if (!len)
|
|
|
|
return TRUE;
|
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
return writer->write (data, len);
|
2008-09-13 22:46:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
moo_file_writer_printf (MooFileWriter *writer,
|
|
|
|
const char *fmt,
|
|
|
|
...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
gboolean ret;
|
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
g_return_val_if_fail (writer != nullptr, FALSE);
|
2008-09-13 22:46:03 -05:00
|
|
|
g_return_val_if_fail (fmt != NULL, FALSE);
|
|
|
|
|
|
|
|
va_start (args, fmt);
|
2016-01-19 03:54:06 -08:00
|
|
|
ret = writer->printf (fmt, args);
|
2008-09-13 22:46:03 -05:00
|
|
|
va_end (args);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-12-29 02:15:14 -08:00
|
|
|
gboolean
|
|
|
|
moo_file_writer_printf_markup (MooFileWriter *writer,
|
|
|
|
const char *fmt,
|
|
|
|
...)
|
|
|
|
{
|
2016-01-19 03:54:06 -08:00
|
|
|
g_return_val_if_fail (writer != nullptr, FALSE);
|
2010-12-29 02:15:14 -08:00
|
|
|
g_return_val_if_fail (fmt != NULL, FALSE);
|
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
va_list args;
|
2010-12-29 02:15:14 -08:00
|
|
|
va_start (args, fmt);
|
2016-01-19 03:54:06 -08:00
|
|
|
gstr string = gstr::wrap_new (g_markup_vprintf_escaped (fmt, args));
|
2010-12-29 02:15:14 -08:00
|
|
|
va_end (args);
|
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
g_return_val_if_fail (!string.is_null(), FALSE);
|
2010-12-29 02:15:14 -08:00
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
return moo_file_writer_write (writer, string, -1);
|
2010-12-29 02:15:14 -08:00
|
|
|
}
|
|
|
|
|
2008-09-13 22:46:03 -05:00
|
|
|
gboolean
|
2016-01-19 03:54:06 -08:00
|
|
|
moo_file_writer_close (MooFileWriter* writer,
|
|
|
|
gerrp& error)
|
2008-09-13 22:46:03 -05:00
|
|
|
{
|
|
|
|
gboolean ret;
|
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
g_return_val_if_fail (writer != nullptr, FALSE);
|
|
|
|
g_return_val_if_fail (!error, FALSE);
|
2008-09-13 22:46:03 -05:00
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
ret = writer->close (error);
|
2008-09-13 22:46:03 -05:00
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
delete writer;
|
2008-09-13 22:46:03 -05:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
gboolean
|
|
|
|
moo_file_writer_close (MooFileWriter *writer,
|
|
|
|
GError **errorp)
|
|
|
|
{
|
|
|
|
gerrp error(errorp);
|
|
|
|
return moo_file_writer_close (writer, error);
|
|
|
|
}
|
|
|
|
|
2008-09-13 22:46:03 -05:00
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
/* MooLocalFileWriter
|
|
|
|
*/
|
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
MooLocalFileWriter::MooLocalFileWriter()
|
|
|
|
: flags(MOO_FILE_WRITER_FLAGS_NONE)
|
|
|
|
{
|
|
|
|
}
|
2008-09-13 22:46:03 -05:00
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
MooLocalFileWriter::~MooLocalFileWriter()
|
|
|
|
{
|
|
|
|
}
|
2008-09-13 22:46:03 -05:00
|
|
|
|
|
|
|
static MooFileWriter *
|
2016-01-19 03:54:06 -08:00
|
|
|
moo_local_file_writer_new (g::File file,
|
|
|
|
MooFileWriterFlags flags,
|
|
|
|
gerrp& error)
|
2008-09-13 22:46:03 -05:00
|
|
|
{
|
2008-09-14 03:01:18 -05:00
|
|
|
if (flags & MOO_FILE_WRITER_CONFIG_MODE)
|
2008-09-13 22:46:03 -05:00
|
|
|
{
|
2015-12-25 21:30:53 -08:00
|
|
|
mgw_errno_t err;
|
2008-09-13 22:46:03 -05:00
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
gstr filename = file.get_path ();
|
|
|
|
gstr dirname = !filename.empty() ? gstr::wrap_new (g_path_get_dirname (filename)) : gstr();
|
2008-09-13 22:46:03 -05:00
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
if (!dirname.empty() && _moo_mkdir_with_parents (dirname, &err) != 0)
|
2008-09-14 03:01:18 -05:00
|
|
|
{
|
2016-01-19 03:54:06 -08:00
|
|
|
gstr display_name = gstr::wrap_new (g_filename_display_name (dirname));
|
|
|
|
g_set_error (&error, G_FILE_ERROR,
|
2015-12-25 21:30:53 -08:00
|
|
|
mgw_file_error_from_errno (err),
|
2008-09-14 03:01:18 -05:00
|
|
|
_("Could not create folder %s: %s"),
|
2015-12-25 21:30:53 -08:00
|
|
|
display_name, mgw_strerror (err));
|
2016-01-19 03:54:06 -08:00
|
|
|
return nullptr;
|
2008-09-14 03:01:18 -05:00
|
|
|
}
|
2008-09-13 22:46:03 -05:00
|
|
|
}
|
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
g::FilePtr file_copy = file.dup ();
|
|
|
|
g::FileOutputStreamPtr stream = file_copy->replace (NULL,
|
|
|
|
(flags & MOO_FILE_WRITER_SAVE_BACKUP) != 0,
|
|
|
|
G_FILE_CREATE_NONE,
|
|
|
|
NULL, error);
|
2008-09-13 22:46:03 -05:00
|
|
|
|
2008-09-14 03:01:18 -05:00
|
|
|
if (!stream)
|
2016-01-19 03:54:06 -08:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
auto writer = make_unique<MooLocalFileWriter>();
|
2008-09-13 22:46:03 -05:00
|
|
|
|
2008-09-15 03:59:22 -05:00
|
|
|
writer->file = file_copy;
|
2016-01-19 03:54:06 -08:00
|
|
|
writer->stream = stream;
|
2008-09-13 22:46:03 -05:00
|
|
|
writer->flags = flags;
|
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
return writer.release ();
|
2008-09-13 22:46:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
MooFileWriter *
|
2016-01-19 03:54:06 -08:00
|
|
|
moo_file_writer_new (const char* filename,
|
|
|
|
MooFileWriterFlags flags,
|
|
|
|
gerrp& error)
|
2008-09-13 22:46:03 -05:00
|
|
|
{
|
2016-01-19 03:54:06 -08:00
|
|
|
g_return_val_if_fail (filename != nullptr, nullptr);
|
|
|
|
g_return_val_if_fail (!error, nullptr);
|
2008-09-15 03:59:22 -05:00
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
g::FilePtr file = g::File::new_for_path (filename);
|
|
|
|
g_return_val_if_fail (file != nullptr, nullptr);
|
2008-09-15 03:59:22 -05:00
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
return moo_local_file_writer_new (*file, flags, error);
|
2008-09-15 03:59:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
MooFileWriter *
|
2016-01-19 03:54:06 -08:00
|
|
|
moo_file_writer_new_for_file (g::File file,
|
|
|
|
MooFileWriterFlags flags,
|
|
|
|
gerrp& error)
|
2008-09-15 03:59:22 -05:00
|
|
|
{
|
2016-01-19 03:54:06 -08:00
|
|
|
g_return_val_if_fail (!error, NULL);
|
2008-09-15 03:59:22 -05:00
|
|
|
return moo_local_file_writer_new (file, flags, error);
|
2008-09-13 22:46:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
MooFileWriter *
|
|
|
|
moo_config_writer_new (const char *filename,
|
|
|
|
gboolean save_backup,
|
2016-01-19 03:54:06 -08:00
|
|
|
gerrp& error)
|
2008-09-13 22:46:03 -05:00
|
|
|
{
|
|
|
|
MooFileWriterFlags flags;
|
|
|
|
|
|
|
|
g_return_val_if_fail (filename != NULL, NULL);
|
2016-01-19 03:54:06 -08:00
|
|
|
g_return_val_if_fail (!error, NULL);
|
2008-09-13 22:46:03 -05:00
|
|
|
|
2010-01-18 23:58:33 -08:00
|
|
|
flags = MOO_FILE_WRITER_CONFIG_MODE | MOO_FILE_WRITER_TEXT_MODE;
|
2008-09-13 22:46:03 -05:00
|
|
|
if (save_backup)
|
|
|
|
flags |= MOO_FILE_WRITER_SAVE_BACKUP;
|
|
|
|
|
|
|
|
return moo_file_writer_new (filename, flags, error);
|
|
|
|
}
|
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
MooFileWriter *
|
|
|
|
moo_config_writer_new (const char *filename,
|
|
|
|
gboolean save_backup,
|
|
|
|
GError **errorp)
|
2008-09-13 22:46:03 -05:00
|
|
|
{
|
2016-01-19 03:54:06 -08:00
|
|
|
gerrp error(errorp);
|
|
|
|
return moo_config_writer_new(filename, save_backup, error);
|
|
|
|
}
|
2008-09-13 22:46:03 -05:00
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
|
|
|
|
bool MooLocalFileWriter::write (const char* data, gsize len)
|
|
|
|
{
|
|
|
|
if (error)
|
2008-09-13 22:46:03 -05:00
|
|
|
return FALSE;
|
|
|
|
|
2010-01-18 23:58:33 -08:00
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
gsize chunk_len = len;
|
|
|
|
gsize next_chunk = len;
|
|
|
|
gsize bytes_written;
|
|
|
|
#ifdef __WIN32__
|
|
|
|
gboolean need_le = FALSE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __WIN32__
|
2016-01-19 03:54:06 -08:00
|
|
|
if (flags & MOO_FILE_WRITER_TEXT_MODE)
|
2010-01-18 23:58:33 -08:00
|
|
|
{
|
|
|
|
gsize le_start, le_len;
|
|
|
|
if (moo_find_line_end (data, len, &le_start, &le_len))
|
|
|
|
{
|
|
|
|
need_le = TRUE;
|
|
|
|
chunk_len = le_start;
|
|
|
|
next_chunk = le_start + le_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2008-09-13 22:46:03 -05:00
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
if (!stream->write_all (data, chunk_len,
|
|
|
|
&bytes_written, NULL,
|
|
|
|
error))
|
2010-01-18 23:58:33 -08:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
data += next_chunk;
|
|
|
|
len -= next_chunk;
|
|
|
|
|
|
|
|
#ifdef __WIN32__
|
2016-01-19 03:54:06 -08:00
|
|
|
if (need_le && !stream->write_all ("\r\n", 2,
|
|
|
|
&bytes_written, NULL,
|
|
|
|
error))
|
2010-01-18 23:58:33 -08:00
|
|
|
return FALSE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
2008-09-13 22:46:03 -05:00
|
|
|
}
|
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
bool MooLocalFileWriter::printf (const char* fmt, va_list args)
|
2008-09-13 22:46:03 -05:00
|
|
|
{
|
2016-01-19 03:54:06 -08:00
|
|
|
if (error)
|
2008-09-13 22:46:03 -05:00
|
|
|
return FALSE;
|
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
gstr text = gstr::wrap_new (g_strdup_vprintf (fmt, args));
|
|
|
|
return write (text, strlen (text));
|
2008-09-13 22:46:03 -05:00
|
|
|
}
|
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
bool MooLocalFileWriter::close (gerrp& out_error)
|
2008-09-13 22:46:03 -05:00
|
|
|
{
|
2016-01-19 03:54:06 -08:00
|
|
|
g_return_val_if_fail (stream != nullptr, FALSE);
|
2008-09-14 03:01:18 -05:00
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
if (!error)
|
2008-09-13 22:46:03 -05:00
|
|
|
{
|
2016-01-19 03:54:06 -08:00
|
|
|
stream->flush (NULL, error);
|
|
|
|
gerrp second;
|
|
|
|
stream->close (NULL, error ? second : error);
|
|
|
|
stream.reset ();
|
|
|
|
file.reset ();
|
2008-09-13 22:46:03 -05:00
|
|
|
}
|
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
out_error = std::move (error);
|
|
|
|
return !out_error;
|
2008-09-13 22:46:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
/* MooStringWriter
|
|
|
|
*/
|
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
bool MooStringWriter::write (const char* data, gsize len)
|
2008-09-13 22:46:03 -05:00
|
|
|
{
|
2016-01-19 03:54:06 -08:00
|
|
|
g_string_append_len (string, data, len);
|
2008-09-13 22:46:03 -05:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
bool MooStringWriter::printf (const char* fmt, va_list args)
|
2008-09-13 22:46:03 -05:00
|
|
|
{
|
2016-01-19 03:54:06 -08:00
|
|
|
strp buf;
|
|
|
|
gint len = g_vasprintf (buf.pp(), fmt, args);
|
2008-09-14 00:13:42 -05:00
|
|
|
|
|
|
|
if (len >= 0)
|
2016-01-19 03:54:06 -08:00
|
|
|
g_string_append_len (string, buf.get(), len);
|
2008-09-14 00:13:42 -05:00
|
|
|
|
2008-09-13 22:46:03 -05:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
bool MooStringWriter::close (G_GNUC_UNUSED gerrp& error)
|
2008-09-13 22:46:03 -05:00
|
|
|
{
|
2016-01-19 03:54:06 -08:00
|
|
|
g_string_free (string, TRUE);
|
|
|
|
string = NULL;
|
2008-09-13 22:46:03 -05:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
MooStringWriter::MooStringWriter()
|
|
|
|
: string (g_string_new (NULL))
|
2008-09-13 22:46:03 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-01-19 03:54:06 -08:00
|
|
|
MooStringWriter::~MooStringWriter()
|
2008-09-13 22:46:03 -05:00
|
|
|
{
|
2016-01-19 03:54:06 -08:00
|
|
|
g_assert (!string);
|
2008-09-13 22:46:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
MooFileWriter *
|
|
|
|
moo_string_writer_new (void)
|
|
|
|
{
|
2016-01-19 03:54:06 -08:00
|
|
|
return new MooStringWriter();
|
2008-09-13 22:46:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-14 03:01:18 -05:00
|
|
|
static gboolean
|
|
|
|
same_content (const char *filename1,
|
|
|
|
const char *filename2)
|
|
|
|
{
|
|
|
|
GMappedFile *file1, *file2;
|
|
|
|
char *content1, *content2;
|
|
|
|
gsize len;
|
|
|
|
gboolean equal = FALSE;
|
|
|
|
|
|
|
|
file1 = g_mapped_file_new (filename1, FALSE, NULL);
|
|
|
|
file2 = g_mapped_file_new (filename2, FALSE, NULL);
|
|
|
|
|
|
|
|
if (!file1 || !file2 ||
|
|
|
|
g_mapped_file_get_length (file1) != g_mapped_file_get_length (file2))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
len = g_mapped_file_get_length (file1);
|
|
|
|
content1 = g_mapped_file_get_contents (file1);
|
|
|
|
content2 = g_mapped_file_get_contents (file2);
|
|
|
|
|
|
|
|
if (memcmp (content1, content2, len) == 0)
|
|
|
|
equal = TRUE;
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (file1)
|
2014-12-06 12:16:16 -08:00
|
|
|
g_mapped_file_unref (file1);
|
2008-09-14 03:01:18 -05:00
|
|
|
if (file2)
|
2014-12-06 12:16:16 -08:00
|
|
|
g_mapped_file_unref (file2);
|
2008-09-14 03:01:18 -05:00
|
|
|
return equal;
|
|
|
|
}
|
|
|
|
|
2008-09-13 22:46:03 -05:00
|
|
|
static gboolean
|
|
|
|
check_file_contents (const char *filename,
|
|
|
|
const char *contents)
|
|
|
|
{
|
|
|
|
gboolean equal;
|
|
|
|
char *real_contents;
|
|
|
|
|
|
|
|
if (!g_file_get_contents (filename, &real_contents, NULL, NULL))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
equal = strcmp (real_contents, contents) == 0;
|
|
|
|
|
|
|
|
g_free (real_contents);
|
|
|
|
return equal;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_moo_file_writer (void)
|
|
|
|
{
|
|
|
|
const char *dir;
|
|
|
|
char *my_dir, *filename, *bak_filename;
|
|
|
|
MooFileWriter *writer;
|
2016-01-19 03:54:06 -08:00
|
|
|
gerrp error;
|
2008-09-13 22:46:03 -05:00
|
|
|
|
|
|
|
dir = moo_test_get_working_dir ();
|
|
|
|
my_dir = g_build_filename (dir, "cfg-writer", NULL);
|
|
|
|
filename = g_build_filename (my_dir, "configfile", NULL);
|
2008-09-14 04:05:58 -05:00
|
|
|
bak_filename = g_strdup_printf ("%s~", filename);
|
2008-09-13 22:46:03 -05:00
|
|
|
|
|
|
|
writer = moo_config_writer_new (filename, TRUE, &error);
|
|
|
|
TEST_ASSERT_MSG (writer != NULL,
|
|
|
|
"moo_cfg_writer_new failed: %s",
|
2011-01-14 01:31:03 -08:00
|
|
|
moo_error_message (error));
|
2016-01-19 03:54:06 -08:00
|
|
|
error.clear ();
|
2008-09-13 22:46:03 -05:00
|
|
|
|
|
|
|
if (writer)
|
|
|
|
{
|
|
|
|
moo_file_writer_write (writer, "first line\n", -1);
|
|
|
|
moo_file_writer_printf (writer, "second line #%d\n", 2);
|
|
|
|
moo_file_writer_write (writer, "third\nlalalala\n", 6);
|
|
|
|
TEST_ASSERT_MSG (moo_file_writer_close (writer, &error),
|
|
|
|
"moo_file_writer_close failed: %s",
|
2011-01-14 01:31:03 -08:00
|
|
|
moo_error_message (error));
|
2016-01-19 03:54:06 -08:00
|
|
|
error.clear ();
|
2008-09-13 22:46:03 -05:00
|
|
|
|
|
|
|
#ifdef __WIN32__
|
|
|
|
#define LE "\r\n"
|
|
|
|
#else
|
|
|
|
#define LE "\n"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TEST_ASSERT (g_file_test (filename, G_FILE_TEST_EXISTS));
|
|
|
|
TEST_ASSERT (!g_file_test (bak_filename, G_FILE_TEST_EXISTS));
|
|
|
|
TEST_ASSERT (check_file_contents (filename, "first line" LE "second line #2" LE "third" LE));
|
|
|
|
}
|
|
|
|
|
|
|
|
writer = moo_config_writer_new (filename, TRUE, &error);
|
|
|
|
TEST_ASSERT_MSG (writer != NULL,
|
|
|
|
"moo_config_writer_new failed: %s",
|
2011-01-14 01:31:03 -08:00
|
|
|
moo_error_message (error));
|
2008-09-13 22:46:03 -05:00
|
|
|
if (writer)
|
|
|
|
{
|
|
|
|
moo_file_writer_write (writer, "First line\n", -1);
|
|
|
|
moo_file_writer_printf (writer, "Second line #%d\n", 2);
|
|
|
|
moo_file_writer_write (writer, "Third\nlalalala\n", 6);
|
|
|
|
TEST_ASSERT_MSG (moo_file_writer_close (writer, &error),
|
|
|
|
"moo_file_writer_close failed: %s",
|
2011-01-14 01:31:03 -08:00
|
|
|
moo_error_message (error));
|
2016-01-19 03:54:06 -08:00
|
|
|
error.clear ();
|
2008-09-13 22:46:03 -05:00
|
|
|
|
|
|
|
TEST_ASSERT (g_file_test (filename, G_FILE_TEST_EXISTS));
|
|
|
|
TEST_ASSERT (g_file_test (bak_filename, G_FILE_TEST_EXISTS));
|
|
|
|
TEST_ASSERT (check_file_contents (filename, "First line" LE "Second line #2" LE "Third" LE));
|
|
|
|
TEST_ASSERT (check_file_contents (bak_filename, "first line" LE "second line #2" LE "third" LE));
|
|
|
|
TEST_ASSERT (!same_content (bak_filename, filename));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_ASSERT (_moo_remove_dir (my_dir, TRUE, NULL));
|
|
|
|
|
|
|
|
#ifndef __WIN32__
|
|
|
|
writer = moo_config_writer_new ("/usr/test-mooutils-fs", TRUE, &error);
|
|
|
|
#else
|
|
|
|
writer = moo_config_writer_new ("K:\\nowayyouhaveit\\file.ini", TRUE, &error);
|
|
|
|
#endif
|
|
|
|
TEST_ASSERT (writer == NULL);
|
2016-01-19 03:54:06 -08:00
|
|
|
TEST_ASSERT (error);
|
2008-09-13 22:46:03 -05:00
|
|
|
|
|
|
|
g_free (bak_filename);
|
|
|
|
g_free (filename);
|
|
|
|
g_free (my_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
moo_test_moo_file_writer (void)
|
|
|
|
{
|
|
|
|
MooTestSuite *suite;
|
|
|
|
|
2010-10-18 23:19:20 -07:00
|
|
|
suite = moo_test_suite_new ("MooFileWriter", "MooFileWriter tests", NULL, NULL, NULL);
|
2008-09-13 22:46:03 -05:00
|
|
|
|
2010-10-18 23:19:20 -07:00
|
|
|
moo_test_suite_add_test (suite, "MooFileWriter", "MooFileWriter tests",
|
2008-09-13 22:46:03 -05:00
|
|
|
(MooTestFunc) test_moo_file_writer, NULL);
|
|
|
|
}
|