home *** CD-ROM | disk | FTP | other *** search
- /*
- * Title : Convert text files.
- * System : N/A */
- #define Version "2.2"
- /* Copyright : (c) John Winters
- * Date : 5th November, 1989
- * Author : John H. Winters
- *
- * Function : Convert text files between Risc-Os and
- * IBM format.
- *
- * Description :
- *
- * IBM text files use carriage return, line
- * feed pairs as line seperators. Risc-Os (and
- * Unix) use just a line feed. MS-Dos also
- * uses tabs which tend to mess up a Risc-OS
- * display. This program converts between the
- * two formats.
- *
- * The type of conversion required is decided
- * on the basis of the original file type.
- * Files which are apparently text (text,
- * exec, obey etc.) are converted to IBM
- * format. Files which are of type "Data" or "DOS",
- * which is the type created by putfile, are
- * assumed to be in IBM format and are
- * converted to Risc-OS form.
- *
- * Files which aren't text at all will probably
- * be screwed up by this utility.
- *
- *
- * Modification history.
- *
- * Version : 1.01
- * Date : 22nd November, 1989
- * Author : John H. Winters
- * Changes : Make sure Archimedes format files end with
- * a line feed. (MS-DOS ones sometimes don't.)
- *
- * Version : 2.0
- * Date : 16th May, 1993
- * Author : John H. Winters
- * Changes : Removed the use of RiscOSLib. Recognize DOS type files.
- *
- * Version : 2.1
- * Date : 25th May, 1993
- * Author : John H. Winters
- * Changes : Updated for Risc OS 3
- *
- * Version : 2.2
- * Date : 21st November, 1993
- * Author : John H. Winters
- * Changes : Updated for new message handling.
- *
- * Version :
- * Date :
- * Author :
- * Changes :
- *
- */
-
- #include <stddef.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include "kernel.h"
- #include "swis.h"
- #include "winapp.h"
- #include "wimplib.h"
- #include "global.h"
- #include "logging.h"
-
- /*
- *============================================================================
- *
- * Hash defines.
- *
- *============================================================================
- */
-
- #define INFO_NAME "ProgInfo"
- #define MENU_INFO 0
- #define MENU_QUIT 1
-
- #define TFN_VERSION 4
-
- #define TEXT_FILE 0x0fff
- #define EXEC_FILE 0x0ffe
- #define DATA_FILE 0x0ffd
- #define OBEY_FILE 0x0feb
- #define DOS_FILE 0x0fe4
-
- #define WORK_FILE "<Convert$Dir>.workfile"
-
- /*
- *============================================================================
- *
- * Local data.
- *
- *============================================================================
- */
-
- static t_dbox InfoDbox ;
- static u8 version_string [] = Version " (21st Nov, 1993)" ;
-
- /*
- *============================================================================
- *
- * Global data.
- *
- *============================================================================
- */
-
- const u8 WA_resource_dir [] = "Convert" ;
- const u8 WA_task_name [] = "Text file converter" ;
-
- /*
- *============================================================================
- *
- * Forward declarations.
- *
- *============================================================================
- */
-
- /*
- static void AcknowledgeLoad (
- const t_message_block *mb) ;
-
- static uint ConvertFromIbm (
- const u8 *name) ;
-
- static uint ConvertToIbm (
- const u8 *name) ;
- */
- static uint FileHandler (
- t_poll_block *poll_block,
- void *reference) ;
-
- static void HourglassOff (void) ;
-
- static void HourglassOn (void) ;
-
- static void MenuHandler (
- void *reference,
- const int *hit) ;
-
- static uint LogMessageHandler (
- t_LOG_Severity /* severity */,
- const U8 * /* message */,
- const U8 * /* timestamp */) ;
-
- static void SetType (
- const u8 *name,
- const u8 *type) ;
-
- /*
- *============================================================================
- *
- * Externally visible routines.
- *
- *============================================================================
- */
-
- uint WA_BeginProcessing (void)
-
- /*
- * Function :
- * Routine to start processing.
- *
- * Parameters :
- * The usual.
- *
- * Returns :
- * TRUE for success, FALSE for failure.
- *
- */
-
- {
- t_menu_block *menu ;
- uint result ;
-
- result = FALSE ;
- menu = WA_BuildMenu ("Convert",
- ">Info,Quit") ;
- if (menu != NULL)
- {
- /*
- * Put an icon on the icon bar.
- */
- if (WA_IconToBar ("!Convert", TRUE))
- {
- /*
- * Register a menu to be triggered when the icon is clicked on.
- */
- if (WA_AttachMenu (ICON_BAR,
- menu,
- MenuHandler,
- NULL))
- {
- InfoDbox = WA_CreateDbox (INFO_NAME, FALSE, NULL, NULL) ;
- if (InfoDbox == NULL)
- {
- LOG_Error ("Can't create information dbox.\n") ;
- }
- else
- {
- WA_SetDboxField (InfoDbox,
- TFN_VERSION,
- version_string) ;
- /*
- * Register a handler to deal with files dragged on to the icon.
- */
- if (WA_ClaimMessage (Message_DataLoad,
- FileHandler,
- NULL))
- {
- result = TRUE ;
- }
- }
- }
- }
- }
- return (result) ;
- }
-
-
- uint WA_LoadResources (void)
-
- /*
- * Function :
- * Load our resources as required.
- *
- * Parameters :
- * None.
- *
- * Returns :
- * TRUE for success, FALSE for failure.
- *
- */
-
- {
- t_LOG_HandlerMask mask ;
-
- mask.value = 0 ;
- mask.b.LHM_Debug = TRUE ;
- mask.b.LHM_Info = TRUE ;
- mask.b.LHM_Warning = TRUE ;
- mask.b.LHM_Error = TRUE ;
- mask.b.LHM_SevereError = TRUE ;
- if ((LOG_AddHandler (mask,
- LogMessageHandler)) &&
- (WA_LoadSprites ("!Sprites")) &&
- (WA_LoadTemplate ("Templates", INFO_NAME)))
- {
- return (TRUE) ;
- }
- else
- {
- return (FALSE) ;
- }
- }
-
- /*
- *============================================================================
- *
- * Local routines.
- *
- *============================================================================
- */
-
- static void AcknowledgeLoad (
- const t_message_block *mb)
-
- /*
- * Function :
- * Send a data load ack message.
- *
- * Parameters :
- * mb Incoming data load message.
- *
- * Returns :
- * None.
- *
- */
-
- {
- t_destinee destinee ;
- t_message_block reply ;
-
- destinee.value = mb->sender ;
- reply = *mb ;
- reply.my_ref = 0 ;
- reply.your_ref = mb->my_ref ;
- reply.action = Message_DataLoadAck ;
- LOG_OSError (WIMP_SendMessage (ET_UserMessage,
- &reply,
- destinee,
- 0)) ;
- }
-
-
- static uint ConvertFromIbm (
- const u8 *name)
-
- /*
- * Function :
- * Convert a named text file which is in IBM format to
- * Archimedes format.
- *
- * Parameters :
- * name The name of the file to convert.
- *
- * Returns :
- * TRUE if it succeeds, FALSE otherwise.
- *
- */
-
- {
- sint character ;
- FILE *in_stream ;
- uint offset = 0 ;
- FILE *out_stream ;
-
- in_stream = fopen (name, "rb") ;
- if (in_stream == NULL)
- {
- LOG_Error ("Can't find file - \"%s\".\n",
- name) ;
- return (FALSE) ;
- }
- else
- {
- out_stream = fopen (WORK_FILE, "w") ;
- if (out_stream == NULL)
- {
- LOG_Error ("Can't open work file.\n") ;
- fclose (in_stream) ;
- return (FALSE) ;
- }
- else
- {
- /*
- * Process from source to work file.
- */
- while (character = getc (in_stream),
- ((character != EOF) &&
- (character != '\x1A')))
- {
- switch (character)
- {
- case '\r' :
- /*
- * Throw these away.
- */
- break ;
-
- case '\t' :
- /*
- * Have to inject spaces up to the next tab
- * position. N.B. Always insert at least one.
- */
- do
- {
- putc (' ', out_stream) ;
- offset++ ;
- }
- while (((offset / 8) * 8) != offset) ;
- break ;
-
- case '\n' :
- offset = 0 ;
- putc (character, out_stream) ;
- break ;
-
- default :
- offset++ ;
- putc (character, out_stream) ;
- break ;
-
- }
- }
- if (offset != 0)
- {
- putc ('\n', out_stream) ;
- }
- fclose (out_stream) ;
- fclose (in_stream) ;
- /*
- * Now copy the whole caboodle back again.
- */
- in_stream = fopen (WORK_FILE, "r") ;
- if (in_stream == NULL)
- {
- LOG_Error ("Can't open work file at second attempt.\n") ;
- return (FALSE) ;
- }
- else
- {
- out_stream = fopen (name, "w") ;
- if (out_stream == NULL)
- {
- LOG_Error ("Can't overwrite file - \"%s\".\n",
- name) ;
- fclose (in_stream) ;
- return (FALSE) ;
- }
- else
- {
- while ((character = getc (in_stream)) != EOF)
- {
- putc (character, out_stream) ;
- }
- fclose (in_stream) ;
- fclose (out_stream) ;
- /*
- * Delete the temporary file and return success.
- */
- remove (WORK_FILE) ;
- SetType (name, "Text") ;
- return (TRUE) ;
- }
- }
- }
- }
- }
-
-
- static uint ConvertToIbm (
- const u8 *name)
-
- /*
- * Function :
- * Convert a named text file to IBM format.
- *
- * Parameters :
- * name The name of the file to convert.
- *
- * Returns :
- * TRUE if it is succesfully converted, FALSE otherwise.
- *
- */
-
- {
- sint character ;
- FILE *in_stream ;
- FILE *out_stream ;
-
- in_stream = fopen (name, "r") ;
- if (in_stream == NULL)
- {
- LOG_Error ("Can't read file - \"%s\".\n",
- name) ;
- return (FALSE) ;
- }
- else
- {
- out_stream = fopen (WORK_FILE, "wb") ;
- if (out_stream == NULL)
- {
- LOG_Error ("Can't open work file.\n") ;
- fclose (in_stream) ;
- return (FALSE) ;
- }
- else
- {
- /*
- * Process from source to work file.
- */
- while ((character = getc (in_stream)) != EOF)
- {
- if (character == '\n')
- {
- putc ('\r', out_stream) ;
- }
- putc (character, out_stream) ;
- }
- putc ('\x1A', out_stream) ;
- fclose (out_stream) ;
- fclose (in_stream) ;
- /*
- * Now copy the whole caboodle back again.
- */
- in_stream = fopen (WORK_FILE, "rb") ;
- if (in_stream == NULL)
- {
- LOG_Error ("Can't open work file at second attempt.\n") ;
- return (FALSE) ;
- }
- else
- {
- out_stream = fopen (name, "wb") ;
- if (out_stream == NULL)
- {
- LOG_Error ("Can't overwrite file - \"%s\".\n",
- name) ;
- fclose (in_stream) ;
- return (FALSE) ;
- }
- else
- {
- while ((character = getc (in_stream)) != EOF)
- {
- putc (character, out_stream) ;
- }
- fclose (in_stream) ;
- fclose (out_stream) ;
- /*
- * Delete the temporary file and return success.
- */
- remove (WORK_FILE) ;
- SetType (name, "DOS") ;
- return (TRUE) ;
- }
- }
- }
- }
- }
-
-
- static uint FileHandler (
- t_poll_block *poll_block,
- void *reference)
-
- /*
- * Function :
- * Handle a file load indication.
- *
- * Parameters :
- * poll_block Poll block containing relevant event.
- *
- * Returns :
- * None.
- *
- */
-
- {
- uint result ;
-
- result = FALSE ;
- reference = reference ;
- /*
- * This is potentially one for us.
- */
- switch (poll_block->data.mb.data.da.file_type)
- {
- case TEXT_FILE :
- case OBEY_FILE :
- case EXEC_FILE :
- HourglassOn () ;
- if (ConvertToIbm (poll_block->data.mb.data.da.file_name))
- {
- AcknowledgeLoad (&poll_block->data.mb) ;
- result = TRUE ;
- }
- HourglassOff () ;
- break ;
-
- case DATA_FILE :
- case DOS_FILE :
- HourglassOn () ;
- if (ConvertFromIbm (poll_block->data.mb.data.da.file_name))
- {
- AcknowledgeLoad (&poll_block->data.mb) ;
- result = TRUE ;
- }
- HourglassOff () ;
- break ;
-
- default :
- LOG_Error ("File type %04.4x is unsuitable for conversion.\n",
- poll_block->data.mb.data.da.file_type) ;
- break ;
-
- }
- return (result) ;
- }
-
-
- static void HourglassOff (void)
-
- /*
- * Function :
- * Turn the hourglass off.
- *
- * Parameters :
- * None.
- *
- * Returns :
- * None.
- *
- */
-
- {
- _kernel_swi_regs r ;
-
- _kernel_swi (Hourglass_Off, &r, &r) ;
- }
-
-
- static void HourglassOn (void)
-
- /*
- * Function :
- * Turn the hourglass on.
- *
- * Parameters :
- * None.
- *
- * Returns :
- * None.
- *
- */
-
- {
- _kernel_swi_regs r ;
-
- _kernel_swi (Hourglass_On, &r, &r) ;
- }
-
-
- static void MenuHandler (
- void *reference,
- const int *hit)
-
- /*
- * Function :
- * Handle a user selection from a menu.
- *
- * Parameters :
- * handle The handle of the menu the user has selected
- * from.
- * hit Pointer to indication of user's selection.
- *
- * Returns :
- * None.
- *
- */
-
- {
- reference = reference ;
-
- switch (*hit)
- {
- case MENU_INFO :
- WA_DisplayDbox (InfoDbox) ;
- break ;
-
- case MENU_QUIT :
- WA_WindUp () ;
- break ;
-
- }
- }
-
-
- static uint LogMessageHandler (
- t_LOG_Severity severity,
- const U8 * message,
- const U8 * timestamp)
-
- /*
- * Function :
- * Display error messages.
- *
- * Parameters :
- * severity Severity of the error.
- * message Text of message.
- * timestamp Time stamp.
- *
- * Returns :
- * TRUE
- *
- */
-
- {
- timestamp = timestamp ;
- if ((severity == LS_Error) ||
- (severity == LS_SevereError))
- {
- WA_Report (message) ;
- }
- return (TRUE) ;
- }
-
-
- static void SetType (
- const u8 *name,
- const u8 *type)
-
- /*
- * Function :
- * Set the type of a file (not the best way).
- *
- * Parameters :
- * name Name of file to change.
- * type Type to set it to.
- *
- * Returns :
- * None.
- *
- */
-
- {
- u8 buffer [512] ;
-
- sprintf (buffer, "settype %s %s", name, type) ;
- system (buffer) ;
- }
-