[Oberon] C program deoberon.c for converting Oberon .Mod files to plain ASCII .Mod.txt files.

Srinivas Nayak sinu.nayak2001 at gmail.com
Sun Apr 17 20:05:00 CEST 2016


Dear Friends,

Here is an updated C program deoberon.c for converting Oberon .Mod files to Plain ASCII .Mod.txt files.
The resulted file can be opened using Geany on Linux.
Also this file can be opened using Edit.Open on Oberon. [rename again .Mod.txt to .Mod]
This program can also convert .Mod files created by TextDocs.NewDoc.

This program is used as follows:
# gcc deoberon.c
# ./a.out Displays.Display.Mod
to generate Displays.Display.Mod.txt
Or
# find . -name '*.Mod' -exec ./a.out "{}" \;
to generate .Mod.txt for each .Mod files present in current directory.


=================
deoberon.c
=================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*
A program to convert an Oberon text file to an ASCII text file.

An Oberon text file consists of a variable-sized header followed
by the ASCII representation of the text. The header contains attribute
information like fonts and colors.

Notes :
OS EndOfLine
Unix 10
DOS 13,10
Mac 13,10
Oberon 13
*/

int Eof = 0;
int skipTail = 0;

int RdCh(FILE* src)
{
	int byte;

	byte = fgetc(src);
	if(feof(src))
	{
		Eof = 1;
	}

	return byte;
}

void SkipHeader(FILE* src)
{
	int word[4];
	int skip;

	RdCh(src); RdCh(src); /* skip 2 bytes */

	/* read a long word in little endian format */
	word[0] = RdCh(src);
	word[1] = RdCh(src);
	word[2] = RdCh(src);
	word[3] = RdCh(src);
	//printf("word[0] = %x\n", word[0]);
	//printf("word[1] = %x\n", word[1]);
	//printf("word[2] = %x\n", word[2]);
	//printf("word[3] = %x\n", word[3]);
	if(word[0] == 'T' && word[1] == 'e' && word[2] == 'x' && word[3] == 't') //if it is "TextDocs.NewDoc"
	{
		for (skip=96; skip > 0; skip--) RdCh(src); //skip 60 more bytes
		RdCh(src); RdCh(src); /* skip 2 bytes */
		/* read a long word in little endian format */
		word[0] = RdCh(src);
		word[1] = RdCh(src);
		word[2] = RdCh(src);
		word[3] = RdCh(src);

		printf("This is a TextDocs file\n");

	}

	/* seek to start of ascii text */
	skip =  (word[3]<<24) + (word[2]<<16) + (word[1]<<8) + (word[0]) - 6;
	printf("Header skip = %d bytes\n", skip);

	for (; skip > 0; skip--) RdCh(src);
}


int main(int argc, char *argv[])
{
	FILE *src, *dst;
	int c;
	char dst_file_name[50];

	if(argc != 2)
	{
		printf("Usage: ./a.out <file name>\n");
		printf("This will generate <file name>.txt\n");
		exit(0);
	}

	src = fopen(argv[1], "r");
	if (src == NULL)
	{
		printf("Error opening file %s\n", argv[1]);
		exit(0);
	}


	strcpy(dst_file_name, argv[1]);
	strcat(dst_file_name, ".txt");

	dst = fopen(dst_file_name, "w");
	if (dst == NULL)
	{
		printf("Error creating file %s\n", dst_file_name);
		fclose(src);
		exit(0);
	}

	printf("Source file %s\n", argv[1]);

	SkipHeader(src);

	c = RdCh(src);
	while (Eof != 1)
	{
		if ( c == 13 )
		{
			c = 13;
			fputc(c, dst);
		}
		else if ( c == '\t' )
		{
			fputc(' ', dst);
			fputc(' ', dst);
			fputc(' ', dst);
			fputc(' ', dst);
		}
		else if ( iscntrl(c) )
		{
			fputc(' ', dst);
		}
		else
		{
			if(c == 219)
			{
				skipTail = 1;
			}

			if(skipTail != 1)
			{
				fputc(c, dst);
			}
		}

		c = RdCh(src);
	}

	fclose(src);
	fclose(dst);

	printf("Generated file %s\n", dst_file_name);

	return 0;
}
=================


With thanks and best regards,

Yours sincerely,
Srinivas Nayak

Home: http://www.mathmeth.com/sn/
Blog: http://srinivas-nayak.blogspot.in/


More information about the Oberon mailing list