1 | /* |
1 | /* |
2 | buffer.h - Part of psiconv, a PSION 5 file formats converter |
2 | buffer.h - Part of psiconv, a PSION 5 file formats converter |
3 | Copyright (c) 2000 Frodo Looijaard <frodol@dds.nl> |
3 | Copyright (c) 2000-2004 Frodo Looijaard <frodol@dds.nl> |
4 | |
4 | |
5 | This program is free software; you can redistribute it and/or modify |
5 | This program is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by |
6 | it under the terms of the GNU General Public License as published by |
7 | the Free Software Foundation; either version 2 of the License, or |
7 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
18 | */ |
18 | */ |
19 | |
19 | |
20 | #ifndef PSICONV_BUFFER_H |
20 | #ifndef PSICONV_BUFFER_H |
21 | #define PSICONV_BUFFER_H |
21 | #define PSICONV_BUFFER_H |
22 | |
22 | |
|
|
23 | /* A psiconv_buffer is a buffer of raw byte data. It is used when parsing |
|
|
24 | or generating a Psion file. You can use references within it that |
|
|
25 | are resolved at a later time. */ |
|
|
26 | |
23 | #include <psiconv/general.h> |
27 | #include <psiconv/general.h> |
24 | |
28 | |
25 | #ifdef __cplusplus |
29 | #ifdef __cplusplus |
26 | extern "C" { |
30 | extern "C" { |
27 | #endif /* __cplusplus */ |
31 | #endif /* __cplusplus */ |
28 | |
32 | |
|
|
33 | |
29 | /* Always use psiconv_buffer, never struct psiconv_buffer_s */ |
34 | /* Always use psiconv_buffer, never struct psiconv_buffer_s */ |
30 | /* No need to export the actual internal format */ |
35 | /* No need to export the actual internal format */ |
31 | typedef struct psiconv_buffer_s *psiconv_buffer; |
36 | typedef struct psiconv_buffer_s *psiconv_buffer; |
32 | |
37 | |
|
|
38 | /* Allocate a new buffer. Returns NULL when not enough memory is available. |
|
|
39 | All other functions assume you have called this function first! */ |
33 | extern psiconv_buffer psiconv_buffer_new(psiconv_u32 base_offset); |
40 | extern psiconv_buffer psiconv_buffer_new(void); |
|
|
41 | |
|
|
42 | /* Free a buffer and reclaim its memory. Never use a buffer again after |
|
|
43 | calling this (unless you do a psiconv_buffer_new on it first) */ |
34 | extern void psiconv_buffer_free(psiconv_buffer buf); |
44 | extern void psiconv_buffer_free(psiconv_buffer buf); |
35 | extern psiconv_u32 psiconv_buffer_base_offset(const psiconv_buffer buf); |
45 | |
|
|
46 | /* Get the length of the data */ |
36 | extern psiconv_u32 psiconv_buffer_length(const psiconv_buffer buf); |
47 | extern psiconv_u32 psiconv_buffer_length(const psiconv_buffer buf); |
|
|
48 | |
|
|
49 | /* Get one byte of data. Returns NULL if you are trying to read past |
|
|
50 | the end of the buffer. Do not use this; instead use psiconv_read_u8 |
|
|
51 | and friends */ |
37 | extern psiconv_u8 *psiconv_buffer_get(const psiconv_buffer buf, |
52 | extern psiconv_u8 *psiconv_buffer_get(const psiconv_buffer buf, |
38 | psiconv_u32 off); |
53 | psiconv_u32 off); |
|
|
54 | |
|
|
55 | /* Add one byte of data to the end. Returns 0 on success, and an error |
|
|
56 | code on failure. Do not use this; instead use psiconv_write_u8 and |
|
|
57 | friends */ |
39 | extern int psiconv_buffer_add(psiconv_buffer buf,psiconv_u8 data); |
58 | extern int psiconv_buffer_add(psiconv_buffer buf,psiconv_u8 data); |
|
|
59 | |
|
|
60 | /* Do an fread to the buffer. Returns the number of read bytes. See |
|
|
61 | fread(3) for more information. */ |
40 | extern size_t psiconv_buffer_fread(psiconv_buffer buf,size_t size, FILE *f); |
62 | extern size_t psiconv_buffer_fread(psiconv_buffer buf,size_t size, FILE *f); |
|
|
63 | |
|
|
64 | /* Read a complete file to the buffer. Returns 0 on success, and an |
|
|
65 | error code on failure. */ |
41 | extern int psiconv_buffer_fread_all(psiconv_buffer buf, FILE *f); |
66 | extern int psiconv_buffer_fread_all(psiconv_buffer buf, FILE *f); |
|
|
67 | |
|
|
68 | /* Write a complete buffer to file. Returns 0 on success, and an |
|
|
69 | error code on failure. */ |
42 | extern int psiconv_buffer_fwrite_all(const psiconv_buffer buf, FILE *f); |
70 | extern int psiconv_buffer_fwrite_all(const psiconv_buffer buf, FILE *f); |
|
|
71 | |
|
|
72 | /* Concatenate two buffers: the second buffer is appended to the first. |
|
|
73 | References are updated too. Buffer extra is untouched after this and must |
|
|
74 | still be freed if you want to get rid of it. */ |
43 | extern int psiconv_buffer_concat(psiconv_buffer buf, |
75 | extern int psiconv_buffer_concat(psiconv_buffer buf, |
44 | const psiconv_buffer extra); |
76 | const psiconv_buffer extra); |
|
|
77 | |
|
|
78 | /* Add a target to the reference list. This does not really change the |
|
|
79 | buffer data in any way. The id needs to be unique. The target is |
|
|
80 | added at the current end of the buffer. */ |
|
|
81 | extern int psiconv_buffer_add_target(psiconv_buffer buf, int id); |
|
|
82 | |
|
|
83 | /* Add a reference to a target to the reference list. The id does not |
|
|
84 | need to be defined already, though it must be by the time you call |
|
|
85 | psiconv_buffer_resolve. The reference is added to the current end |
|
|
86 | of the buffer, and space is allocated for it. References are always |
|
|
87 | longs (psiconv_u32). */ |
|
|
88 | extern int psiconv_buffer_add_reference(psiconv_buffer buf,int id); |
|
|
89 | |
|
|
90 | /* Resolve all references and empty the reference list. */ |
|
|
91 | extern int psiconv_buffer_resolve(psiconv_buffer buf); |
|
|
92 | |
|
|
93 | /* Get a unique reference id */ |
|
|
94 | extern psiconv_u32 psiconv_buffer_unique_id(void); |
|
|
95 | |
|
|
96 | /* Extract part of a buffer and put it into a new buffer. Note that |
|
|
97 | references and targets are not copied; you will have to resolve them |
|
|
98 | beforehand (but as this function is meant for reading buffers, they |
|
|
99 | will usually not be used). */ |
|
|
100 | extern int psiconv_buffer_subbuffer(psiconv_buffer *buf, |
|
|
101 | const psiconv_buffer org, |
|
|
102 | psiconv_u32 offset, psiconv_u32 length); |
|
|
103 | |
|
|
104 | |
45 | |
105 | |
46 | |
106 | |
47 | #ifdef __cplusplus |
107 | #ifdef __cplusplus |
48 | } |
108 | } |
49 | #endif /* __cplusplus */ |
109 | #endif /* __cplusplus */ |