File: util.c

package info (click to toggle)
cbmconvert 2.1.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 380 kB
  • sloc: ansic: 5,084; perl: 42; makefile: 4
file content (80 lines) | stat: -rw-r--r-- 2,264 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
 * @file util.c
 * Utility functions
 * @author Marko Mäkelä (marko.makela at iki.fi)
 */

/*
** Copyright © 1993-1997,2001,2021 Marko Mäkelä
**
**     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 2 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, write to the Free Software
**     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <stdio.h>
#include <string.h>
#include "util.h"

/** Convert a file name to a printable null-terminated string.
 * @param name  the PETSCII file name to be converted
 * @return      the corresponding ASCII file name
 */
const char*
getFilename (const struct Filename* name)
{
  static char buf[sizeof(name->name) + 5];
  int i;

  if (!name)
    return 0;

  /* remove trailing shifted spaces */

  for (i = sizeof(name->name); i-- && name->name[i] == 0xA0; );
  buf [++i] = 0;
  while (i--)
    if (name->name[i] >= 0x41 && name->name[i] <= 0x5A)
      buf[i] = (char) (name->name[i] - 0x41 + 'a');
    else if (name->name[i] >= 0xC1 && name->name[i] <= 0xDA)
      buf[i] = (char) (name->name[i] - 0xC1 + 'A');
    else if (name->name[i] >= 0x61 && name->name[i] <= 0x7A)
      buf[i] = (char) (name->name[i] - 0x61 + 'A');
    else if (name->name[i] >= 0x20 && name->name[i] <= 0x5F)
      buf[i] = (char) name->name[i];
    else
      buf[i] = '_'; /* non-ASCII character */

  switch (name->type) {
  case DEL:
    strcat (buf, ",del");
    break;
  case SEQ:
    strcat (buf, ",seq");
    break;
  case PRG:
    strcat (buf, ",prg");
    break;
  case USR:
    strcat (buf, ",usr");
    break;
  case REL:
    sprintf (buf + strlen(buf), ",l%02X", name->recordLength);
    break;
  case CBM:
    strcat (buf, ",cbm");
    break;
  }

  return buf;
}