Previous 15994 Revisions Next

r2635 Friday 16th May, 2008 at 11:19:53 UTC by Nathan Woods
Now supporting encoding of tokenized BASIC files. (bug #1609)
[docs]messnew.txt
[src/mess/tools/imgtool]filtbas.c stream.c

src/mess/tools/imgtool/filtbas.c
r2634r2635
44
55   Filter for Microsoft-style tokenized BASIC files
66
7   BASIC files typically follow the following format:
8
9      int8     $ff
10      int16    <TOTAL LENGTH>
11      ...
12        int16    <PTR_NEXT_LINE>
13        int16    <LINE_NUM>
14        int8[]   <TOKENISED_DATA>
15        int8     $00     End of line delimiter
16
717*****************************************************************************/
818
919#include <string.h>
1020#include <stdarg.h>
1121#include <assert.h>
22#include <ctype.h>
23
1224#include "imgtool.h"
1325#include "utils.h"
1426
r2634r2635
5264    IMPLEMENTATION
5365***************************************************************************/
5466
67/*-------------------------------------------------
68    basic_readfile - reads a file and decodes
69   BASIC tokens into ASCII text
70-------------------------------------------------*/
71
5572static imgtoolerr_t basic_readfile(const basictokens *tokens,
5673   imgtool_partition *partition, const char *filename,
5774   const char *fork, imgtool_stream *destf)
r2634r2635
6683   const basictoken_tableent *token_table;
6784   const char *token;
6885
86   /* open a memory stream */
6987   mem_stream = stream_open_mem(NULL, 0);
70   if (!mem_stream)
88   if (mem_stream == NULL)
7189   {
7290      err = IMGTOOLERR_OUTOFMEMORY;
7391      goto done;
r2634r2635
7896   if (err)
7997      goto done;
8098
81   /* skip first bytes */
99   /* skip first few bytes */
82100   stream_seek(mem_stream, tokens->skip_bytes, SEEK_SET);
83101
102   /* keep reading line headers */
84103   while(stream_read(mem_stream, line_header, sizeof(line_header)) == sizeof(line_header))
85104   {
105      /* pluck the address and line number out */
86106      if (tokens->be)
87107      {
88108         address = (UINT16) pick_integer_be(line_header, 0, 2);
r2634r2635
94114         line_number = (UINT16) pick_integer_le(line_header, 2, 2);
95115      }
96116
117      /* write the line number */
97118      stream_printf(destf, "%u ", (unsigned) line_number);
98119      shift = 0x00;
99120
r2634r2635
139160   }
140161
141162done:
142   if (mem_stream)
163   if (mem_stream != NULL)
143164      stream_close(mem_stream);
144165   return err;
145166}
146167
147168
148169
170/*-------------------------------------------------
171    basic_writefile - translates ASCII text to
172   BASIC tokens and writes it to a file
173-------------------------------------------------*/
174
149175static imgtoolerr_t basic_writefile(const basictokens *tokens,
150176   imgtool_partition *partition, const char *filename,
151177   const char *fork, imgtool_stream *sourcef, option_resolution *opts)
152178{
153   return IMGTOOLERR_UNIMPLEMENTED;
179   imgtoolerr_t err;
180   imgtool_stream *mem_stream;
181   char buf[1024];
182   int eof = FALSE;
183   UINT32 len;
184   char c;
185   int i, j, pos, in_quotes;
186   UINT16 line_number;
187   UINT8 line_header[4];
188   UINT8 file_header[3];
189   const basictoken_tableent *token_table;
190   const char *token;
191   UINT8 token_shift, token_value;
192   UINT16 address;
193
194   /* open a memory stream */
195   mem_stream = stream_open_mem(NULL, 0);
196   if (mem_stream == NULL)
197   {
198      err = IMGTOOLERR_OUTOFMEMORY;
199      goto done;
200   }
201
202   /* skip first few bytes */
203   stream_fill(mem_stream, 0x00, tokens->skip_bytes);
204
205   /* loop until the file is complete */
206   while(!eof)
207   {
208      /* read a line */
209      pos = 0;
210      while((len = stream_read(sourcef, &c, 1)) > 0)
211      {
212         /* break if at end of line */
213         if ((c == '\r') || (c == '\n'))
214            break;
215
216         if (pos <= ARRAY_LENGTH(buf) - 1)
217         {
218            buf[pos++] = c;
219         }
220      }
221      eof = (len == 0);
222      buf[pos] = '\0';
223
224      /* ignore lines that don't start with digits */
225      if (isdigit(buf[0]))
226      {
227         /* start at beginning of line */
228         pos = 0;
229
230         /* read line number */
231         line_number = 0;
232         while(isdigit(buf[pos]))
233         {
234            line_number *= 10;
235            line_number += (buf[pos++] - '0');
236         }
237         
238         /* determine address */
239         if (tokens->baseaddress != 0)
240         {
241            address = tokens->baseaddress + (UINT16)stream_size(mem_stream) + 4;
242         }
243         else
244         {
245            address = 0x0000;
246         }
247
248         /* set up line header */
249         memset(&line_header, 0, sizeof(line_header));
250         if (tokens->be)
251         {
252            place_integer_be(line_header, 0, 2, address);
253            place_integer_be(line_header, 2, 2, line_number);
254         }
255         else
256         {
257            place_integer_be(line_header, 0, 2, address);
258            place_integer_be(line_header, 2, 2, line_number);
259         }
260         
261         /* emit line header */
262         stream_write(mem_stream, line_header, sizeof(line_header));
263
264         /* skip spaces */
265         while(isspace(buf[pos]))
266            pos++;
267
268         /* when we start out, we are not within quotation marks */
269         in_quotes = FALSE;
270
271         /* read until end of line */
272         while(buf[pos] != '\0')
273         {
274            token = NULL;
275            token_shift = 0;
276            token_value = 0;
277
278            if (buf[pos] == '\"')
279            {
280               /* flip quotation status */
281               in_quotes = !in_quotes;
282            }
283            else if (!in_quotes)
284            {
285               for (i = 0; (token == NULL) && (i < tokens->num_entries); i++)
286               {
287                  token_table = &tokens->entries[i];
288                  for (j = 0; (token == NULL) && (j < token_table->num_tokens); j++)
289                  {
290                     if (!strncmp(&buf[pos], token_table->tokens[j], strlen(token_table->tokens[j])))
291                     {
292                        token = token_table->tokens[j];
293                        token_shift = token_table->shift;
294                        token_value = token_table->base + j;
295                        pos += strlen(token);
296                     }
297                  }
298               }
299            }
300
301            /* did we find a token? */
302            if (token != NULL)
303            {
304               /* emit the token */
305               if (token_shift != 0)
306                  stream_write(mem_stream, &token_shift, 1);
307               stream_write(mem_stream, &token_value, 1);
308            }
309            else
310            {
311               /* no token; emit the byte */
312               stream_write(mem_stream, &buf[pos++], 1);
313            }
314         }
315
316         /* emit line terminator */
317         stream_fill(mem_stream, 0x00, 1);
318      }
319   }
320
321   /* emit program terminator */
322   stream_fill(mem_stream, 0x00, 2);
323
324   /* reset stream */
325   stream_seek(mem_stream, 0, SEEK_SET);
326
327   /* this is somewhat gross */
328   if (tokens->skip_bytes >= 3)
329   {
330      if (tokens->be)
331      {
332         place_integer_be(file_header, 0, 1, 0xFF);
333         place_integer_be(file_header, 1, 2, stream_size(mem_stream));
334      }
335      else
336      {
337         place_integer_le(file_header, 0, 1, 0xFF);
338         place_integer_le(file_header, 1, 2, stream_size(mem_stream));
339      }
340      stream_write(mem_stream, file_header, 3);
341      stream_seek(mem_stream, 0, SEEK_SET);
342   }
343
344   /* write actual file */
345   err = imgtool_partition_write_file(partition, filename, fork, mem_stream, opts, NULL);
346   if (err)
347      goto done;
348
349done:
350   if (mem_stream != NULL)
351      stream_close(mem_stream);
352   return err;
154353}
155354
156355
157356
158/* ----------------------------------------------------------------------- */
357/***************************************************************************
358    TOKEN DEFINITIONS
359***************************************************************************/
159360
160361static const char *const cocobas_statements[] =
161362{
r2634r2635
25902791};
25912792
25922793#endif
2593/*
2594      int8     $ff
2595      int16    <TOTAL LENGTH>
2596      ...
2597        int16    <PTR_NEXT_LINE>
2598        int16    <LINE_NUM>
2599        int8[]   <TOKENISED_DATA>
2600        int8     $00     End of line delimiter
2601 */
26022794
2603/*************************************
2604 *
2605 *  CoCo BASIC
2606 *
2607 *************************************/
26082795
2796
2797/***************************************************************************
2798    COCO BASIC
2799***************************************************************************/
2800
26092801static const basictoken_tableent cocobas_tokenents[] =
26102802{
26112803   { 0x00,   0x80,   cocobas_statements,   sizeof(cocobas_statements) / sizeof(cocobas_statements[0]) },
r2634r2635
26462838
26472839
26482840
2649/*************************************
2650 *
2651 *  Dragon BASIC
2652 *
2653 *************************************/
2841/***************************************************************************
2842    DRAGON BASIC
2843***************************************************************************/
26542844
26552845static const basictoken_tableent dragonbas_tokenents[] =
26562846{
r2634r2635
26902880   }
26912881}
26922882
2693/*************************************
2694 *
2695 *  VZBASIC
2696 *
2697 *************************************/
26982883
2884
2885/***************************************************************************
2886    VZBASIC
2887***************************************************************************/
2888
26992889static const basictoken_tableent vzbas_tokenents[] =
27002890{
2701   { 0x00,   0x80,   vzbas,   sizeof(vzbas) / sizeof(vzbas[0]) }
2891   { 0x00, 0x80,   vzbas,   sizeof(vzbas) / sizeof(vzbas[0]) }
27022892};
27032893
2894
2895
27042896static const basictokens vzbas_tokens =
27052897{
27062898   0x7ae9,
src/mess/tools/imgtool/stream.c
r2634r2635
313313            }
314314
315315            /* do we have to limit sz? */
316            if (sz < (s->filesize - s->position))
316            if (sz > (s->filesize - s->position))
317317               sz = (UINT32) (s->filesize - s->position);
318318
319319            memcpy(s->u.buffer + s->position, buf, sz);
docs/messnew.txt
r2634r2635
6565
6666Imgtool Changes:
6767----------------
68- Now supporting encoding of tokenized BASIC files.  (bug #1609) [Nate Woods]
69
6870- [COCO BASIC] Fixed tokenization for DSKO$ command and above.  (bug #1609)
6971  [Nate Woods]
7072

Previous 15994 Revisions Next


© 1998-2018 The MESS Team