File: archive.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 (115 lines) | stat: -rw-r--r-- 2,990 bytes parent folder | download
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
 * @file archive.c
 * A collection of files
 * @author Marko Mäkelä (marko.makela at iki.fi)
 */

/*
** Copyright © 1998, 2022, 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 <stdlib.h>
#include <string.h>
#include <errno.h>

#include "output.h"

/** Allocate an archive data structure.
 * @return      a newly allocated empty archive structure
 */
struct Archive*
newArchive (void)
{
  return calloc (1, sizeof (struct Archive));
}
/** Deallocate an archive data structure.
 * @param archive       the archive to be deallocated
 */
void deleteArchive (struct Archive* archive)
{
  while (archive->first) {
    struct ArchiveEntry* ae = archive->first->next;
    free (archive->first->data);
    free (archive->first);
    archive->first = ae;
  }

  free (archive);
}

/** Write a file to an archive.
 * @param name          native (PETSCII) name of the file
 * @param data          the contents of the file
 * @param length        length of the file contents
 * @param archive       the archive the file is written to
 * @param log           Call-back function for diagnostic output
 * @return              status of the operation
 */
enum WrStatus
WriteArchive (const struct Filename* name,
              const byte_t* data,
              size_t length,
              struct Archive* archive,
              log_t log)
{
  struct ArchiveEntry* ae;

  switch (name->type) {
  default:
    (*log) (Errors, name, "Unsupported file type.");
    return WrFail;
  case DEL:
  case SEQ:
  case PRG:
  case USR:
  case REL:
    break;
  }

  /* check for duplicate file names */
  if (!allowDuplicates)
    for (ae = archive->first; ae; ae = ae->next)
      if (!memcmp (&ae->name.name, name->name, sizeof name->name))
        return WrFileExists;

  if (!(ae = malloc (sizeof (*ae)))) {
    (*log) (Errors, name, "Out of memory.");
    return WrNoSpace;
  }

  if (!(ae->data = malloc (length))) {
    free (ae);
    (*log) (Errors, name, "Out of memory.");
    return WrNoSpace;
  }

  memcpy (&ae->name, name, sizeof (*name));
  if (length)
    memcpy (ae->data, data, length);
  ae->length = length;
  ae->next = 0;

  if (archive->last)
    archive->last->next = ae;
  else
    archive->first = ae;

  archive->last = ae;

  return WrOK;
}