/* Copyright 1997 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/***************************************************/
/* File   : MimeMap.c                              */
/*                                                 */
/* Purpose: Interfacing with the MimeMap module.   */
/*                                                 */
/* Author : A.D.Hodgkinson                         */
/*                                                 */
/* History: 07-Oct-97: Created.                    */
/***************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "swis.h"

#include "wimp.h"
#include "event.h"

#include "svcprint.h"
#include "Global.h"
#include "Utils.h"

#include "MimeMap.h"

/*************************************************/
/* mimemap_mime_to_riscos()                      */
/*                                               */
/* Converts a Mime type to a RISC OS filetype.   */
/*                                               */
/* Parameters: Pointer to a null-terminated Mime */
/*             type string;                      */
/*                                               */
/*             Pointer to an int, in which the   */
/*             RISC OS filetype is written.      */
/*************************************************/

_kernel_oserror * mimemap_mime_to_riscos(const char * mime, int * riscos)
{
  if (!mime || !riscos) return NULL;

  return _swix(MimeMap_Translate,
               _INR(0, 2) | _OUT(3),

               MimeMap_MimeType_Mime,
               mime,
               MimeMap_RISCOSType_Number,

               riscos);
}

/*************************************************/
/* mimemap_riscos_to_mime()                      */
/*                                               */
/* Converts a RISC OS filetype to a Mime type.   */
/* The maximum buffer size required is not       */
/* defined in the MimeMap module documentation,  */
/* so use MimeMap_MaximumBufferSizeRequired      */
/* if you want to be sure that the string will   */
/* not be truncated (it is safe to use shorter   */
/* buffers, though), defined in MimeMap.h.       */
/*                                               */
/* Parameters: The RISC OS filetype;             */
/*                                               */
/*             Pointer to a buffer, in which a   */
/*             null-terminated Mime type string  */
/*             will be written;                  */
/*                                               */
/*             Size of the buffer.               */
/*************************************************/

_kernel_oserror * mimemap_riscos_to_mime(int riscos, char * buffer, int buffer_size)
{
  char mime_buffer[MimeMap_MaximumBufferSizeRequired];

  if (!buffer || !buffer_size) return NULL;

  RetError(_swix(MimeMap_Translate,
                 _INR(0, 3),

                 MimeMap_RISCOSType_Number,
                 riscos,
                 MimeMap_MimeType_Mime,
                 mime_buffer));

  strncpy(buffer, mime_buffer, buffer_size - 1);
  buffer[buffer_size - 1] = 0;

  return NULL;
}

/*************************************************/
/* mimemap_extension_to_riscos()                 */
/*                                               */
/* Converts a filename extension to a RISC OS    */
/* filetype.                                     */
/*                                               */
/* Parameters: Pointer to a null-terminated      */
/*             filename extension, with or       */
/*             without the dot;                  */
/*                                               */
/*             Pointer to an int, in which the   */
/*             RISC OS filetype is written.      */
/*************************************************/

_kernel_oserror * mimemap_extension_to_riscos(const char * ext, int * riscos)
{
  if (!ext || !riscos) return NULL;

  return _swix(MimeMap_Translate,
               _INR(0, 2) | _OUT(3),

               MimeMap_MimeType_Extension,
               ext,
               MimeMap_RISCOSType_Number,

               riscos);
}

/*************************************************/
/* mimemap_extension_to_mime()                   */
/*                                               */
/* Converts a filename extension to a Mime type. */
/* The maximum buffer size required is not       */
/* defined in the MimeMap module documentation,  */
/* so use MimeMap_MaximumBufferSizeRequired      */
/* if you want to be sure that the string will   */
/* not be truncated (it is safe to use shorter   */
/* buffers, though), defined in MimeMap.h.       */
/*                                               */
/* Parameters: Pointer to a null-terminated      */
/*             filename extension, with or       */
/*             without the dot;                  */
/*                                               */
/*             Pointer to a buffer, in which a   */
/*             null-terminated Mime type string  */
/*             will be written;                  */
/*                                               */
/*             Size of the buffer.               */
/*************************************************/

_kernel_oserror * mimemap_extension_to_mime(const char * ext, char * buffer, int buffer_size)
{
  char mime_buffer[MimeMap_MaximumBufferSizeRequired];

  if (!ext || !buffer || !buffer_size) return NULL;

  RetError(_swix(MimeMap_Translate,
                 _INR(0, 3),

                 MimeMap_MimeType_Extension,
                 ext,
                 MimeMap_MimeType_Mime,
                 mime_buffer));

  strncpy(buffer, mime_buffer, buffer_size - 1);
  buffer[buffer_size - 1] = 0;

  return NULL;
}