/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <glib-object.h>

#include "core/core-types.h"

#include "xcf-private.h"
#include "xcf-seek.h"

#include "gimp-intl.h"

/**
 * SECTION:xcf-seek
 * @Short_description:XCF file seeker functions
 *
 * Functions to change the file position in the XCF file
 */

/**
 * xcf_seek_pos:
 * @info:  #XcfInfo structure of the file under work
 * @pos:   new position, relative to the beginning of the file
 * @error: Return location for errors
 *
 * Changes the file position in the input or output stream to the given
 * position.
 *
 * Returns: %TRUE in case of success; %FALSE otherwise
 */
gboolean
xcf_seek_pos (XcfInfo  *info,
              guint     pos,
              GError  **error)
{
  if (info->cp != pos)
    {
      info->cp = pos;
      if (fseek (info->fp, info->cp, SEEK_SET) == -1)
        {
          g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
                       _("Could not seek in XCF file: %s"),
                       g_strerror (errno));

          return FALSE;
        }
    }

  return TRUE;
}

/**
 * xcf_seek_end:
 * @info: #XcfInfo structure of the file under work
 * @error: Return location for errors
 *
 * Changes the file position in the input or output stream to the file end.
 *
 * Returns: %TRUE in case of success; %FALSE otherwise
 */
gboolean
xcf_seek_end (XcfInfo  *info,
              GError  **error)
{
  if (fseek (info->fp, 0, SEEK_END) == -1)
    {
      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
                   _("Could not seek in XCF file: %s"),
                   g_strerror (errno));

      return FALSE;
    }

  info->cp = ftell (info->fp);

  if (fseek (info->fp, 0, SEEK_END) == -1)
    {
      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
                   _("Could not seek in XCF file: %s"),
                   g_strerror (errno));

      return FALSE;
    }

  return TRUE;
}

/**
 * xcf_seek_pos_for_wrting:
 * @info:  #XcfInfo structure of the file under work
 * @pos:   new position, relative to the beginning of the file
 * @error: Return location for errors
 *
 * Changes the file position in the output stream to the given position.
 * This should be called only before writing in order to adjust a file size.
 * Non-POSIX fseek() does not define behavior when the postion greater than
 * the file size is specified. 
 *
 * Returns: %TRUE in case of success; %FALSE otherwise
 */
gboolean
xcf_seek_pos_for_wrting (XcfInfo  *info,
                         guint     pos,
                         GError  **error)
{
  int fd = fileno(info->fp);
  struct stat sb;
 
  // Check current file size
  if (fstat(fd, &sb) < 0)
    {
      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
                   _("Could not fstat XCF file: %s"),
                   g_strerror (errno));
 
      return FALSE;
    }
  else if (pos > sb.st_size)
    {
      // Expand file
      if (ftruncate(fd, pos) < 0)
        {
          g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
                       _("Could not ftruncate XCF file: %s"),
                       g_strerror (errno));
 
          return FALSE;
        }
    }

  return xcf_seek_pos(info, pos, error);
}
