File: rawfile.c

package info (click to toggle)
vice 1.19-1etch1
  • links: PTS
  • area: contrib
  • in suites: etch
  • size: 27,132 kB
  • ctags: 33,406
  • sloc: ansic: 257,145; cpp: 13,395; sh: 3,674; makefile: 3,380; perl: 1,801; yacc: 622; lex: 258; asm: 4
file content (174 lines) | stat: -rw-r--r-- 4,199 bytes parent folder | download | duplicates (4)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * rawfile.c - Raw file handling.
 *
 * Written by
 *  Andreas Boose <viceteam@t-online.de>
 *
 * This file is part of VICE, the Versatile Commodore Emulator.
 * See README for copyright notice.
 *
 *  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., 59 Temple Place, Suite 330, Boston, MA
 *  02111-1307  USA.
 *
 */

#include "vice.h"

#include <stdio.h>

#include "archdep.h"
#include "fileio.h"
#include "ioutil.h"
#include "lib.h"
#include "rawfile.h"
#include "util.h"


struct rawfile_info_s {
    FILE *fd;
    char *name;
    char *path;
    unsigned int readonly;
};
typedef struct rawfile_info_s rawfile_info_t;


rawfile_info_t *rawfile_open(const char *file_name, const char *path,
                             unsigned int command)
{
    rawfile_info_t *info;
    char *complete;
    FILE *fd;
    const char *mode = NULL;

    if (path == NULL)
        complete = lib_stralloc(file_name);
    else
        complete = util_concat(path, FSDEV_DIR_SEP_STR, file_name, NULL);

    switch (command) {
      case FILEIO_COMMAND_READ:
        mode = MODE_READ;
        break;
      case FILEIO_COMMAND_WRITE:
        mode = MODE_WRITE;
        break;
      case FILEIO_COMMAND_APPEND:
        mode = MODE_APPEND;
        break;
      case FILEIO_COMMAND_APPEND_READ:
        mode = MODE_APPEND_READ_WRITE;
        break;
      default:
        return NULL;
    }

    fd = fopen(complete, mode);

    if (fd == NULL) {
        lib_free(complete);
        return NULL;
    }

    info = (rawfile_info_t *)lib_malloc(sizeof(rawfile_info_t));

    info->fd = fd;
    util_fname_split(complete, &(info->path), &(info->name));
    info->readonly = 0;

    lib_free(complete);

    return info;
}

void rawfile_destroy(rawfile_info_t *info)
{
    if (info != NULL) {
        fclose(info->fd);
        lib_free(info->name);
        lib_free(info->path);
        lib_free(info);
    }
}

unsigned int rawfile_read(rawfile_info_t *info, BYTE *buf, unsigned int len)
{
    return (unsigned int)fread(buf, 1, len, info->fd);
}

unsigned int rawfile_write(rawfile_info_t *info, BYTE *buf, unsigned int len)
{
    return (unsigned int)fwrite(buf, 1, len, info->fd);
}

int rawfile_seek_set(rawfile_info_t *info, int offset)
{
    return fseek(info->fd, offset, SEEK_SET);
}

unsigned int rawfile_ferror(rawfile_info_t *info)
{
    return (unsigned int)ferror(info->fd);
}

unsigned int rawfile_rename(const char *src_name, const char *dst_name,
                            const char *path)
{
    char *complete_src, *complete_dst;
    int rc;

    if (path == NULL) {
        complete_src = lib_stralloc(src_name);
        complete_dst = lib_stralloc(dst_name);
    } else {
        complete_src = util_concat(path, FSDEV_DIR_SEP_STR, src_name, NULL);
        complete_dst = util_concat(path, FSDEV_DIR_SEP_STR, dst_name, NULL);
    }

    /*ioutil_remove(dst_name);*/
    rc = ioutil_rename(complete_src, complete_dst);

    lib_free(complete_src);
    lib_free(complete_dst);

    if (rc < 0) {
        if (ioutil_errno(IOUTIL_ERRNO_EPERM))
            return FILEIO_FILE_PERMISSION;
        return FILEIO_FILE_NOT_FOUND;
    }

    return FILEIO_FILE_OK;
}
 
unsigned int rawfile_remove(const char *src_name, const char *path)
{
    char *complete_src;
    int rc;

    if (path == NULL)
        complete_src = lib_stralloc(src_name);
    else
        complete_src = util_concat(path, FSDEV_DIR_SEP_STR, src_name, NULL);

    rc = ioutil_remove(complete_src);

    lib_free(complete_src);

    if (rc < 0)
        return FILEIO_FILE_NOT_FOUND;

    return FILEIO_FILE_SCRATCHED;
}