ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
file_storage.c
Go to the documentation of this file.
1/*****************************************************************************
2 * ugBASIC - an isomorphic BASIC language compiler for retrocomputers *
3 *****************************************************************************
4 * Copyright 2021-2026 Marco Spedaletti (asimov@mclink.it)
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *----------------------------------------------------------------------------
18 * Concesso in licenza secondo i termini della Licenza Apache, versione 2.0
19 * (la "Licenza"); è proibito usare questo file se non in conformità alla
20 * Licenza. Una copia della Licenza è disponibile all'indirizzo:
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Se non richiesto dalla legislazione vigente o concordato per iscritto,
25 * il software distribuito nei termini della Licenza è distribuito
26 * "COSÌ COM'È", SENZA GARANZIE O CONDIZIONI DI ALCUN TIPO, esplicite o
27 * implicite. Consultare la Licenza per il testo specifico che regola le
28 * autorizzazioni e le limitazioni previste dalla medesima.
29 ****************************************************************************/
30
31/****************************************************************************
32 * INCLUDE SECTION
33 ****************************************************************************/
34
35#include "../../ugbc.h"
36
37/****************************************************************************
38 * CODE SECTION
39 ****************************************************************************/
40
47/* <usermanual>
48@keyword FILE (storage)
49
50@english
51
52Inside a ''BEGIN STORAGE...END STORAGE'' block, the ''FILE'' command plays
53a crucial role in the process of embedding external data directly into the
54storage medium you are defining while compiling your program. Imagine you
55want to include a set of configuration data or any other static file
56directly into your program's storage medium, the ''FILE'' command allows
57you to do just that.
58
59The basic syntax is to indicate the name of the source file that will be
60inserted into the media. If you do not want to use the same name, you can
61indicate an alias (''AS target'').
62
63The addition of the ''CSV OF type'' statement is a powerful feature that
64allows you to include Comma Separated Values (CSV) files and specify the
65data type of each column. Instead of treating the CSV file as a simple
66block of bytes (as the ''FILE'' statement alone would), the compiler will
67parse the contents of the CSV file and convert it into a more structured
68internal representation that is ready for use by your program. The kind
69of structure will be described by the datatype ''type''.
70
71@italian
72All'interno di un blocco ''BEGIN STORAGE...END STORAGE'', il comando
73''FILE'' svolge un ruolo cruciale nel processo di inserimento di dati
74esterni direttamente nel supporto di memorizzazione che si sta definendo
75durante la compilazione del programma. Immaginate di voler includere un set
76di dati di configurazione o qualsiasi altro file statico direttamente nel
77supporto di memorizzazione del programma: il comando ''FILE'' vi permette
78di fare proprio questo.
79
80La sintassi di base consiste nell'indicare il nome del file sorgente che
81verrà inserito nel supporto. Se non si desidera utilizzare lo stesso nome,
82è possibile indicare un alias (''AS target'').
83
84L'aggiunta dell'istruzione ''CSV OF type'' è una potente funzionalità che
85consente di includere file con valori separati da virgola (CSV) e di
86specificare il tipo di dati di ciascuna colonna. Invece di trattare il
87file CSV come un semplice blocco di byte (come farebbe la sola istruzione
88''FILE''), il compilatore analizzerà il contenuto del file CSV e lo
89convertirà in una rappresentazione interna più strutturata, pronta per l'uso
90da parte del programma. Il tipo di struttura sarà descritto dal tipo
91di dati ''type''.
92
93@syntax FILE source [AS target] [CSV OF type]
94
95@example FILE "examples/data.dat"
96@example FILE "sprites.png" AS "sprites.dat"
97@example FILE "examples/data.csv" CSV OF BYTE
98@example BEGIN TYPE point
99@example x AS POSITION
100@example x AS POSITION
101@example END TYPE point
102@example FILE "examples/structure.csv" CSV OF point
103
104@usedInExample storage_example_01.bas
105
106@seeAlso BEGIN STORAGE
107@target all
108</usermanual> */
109void file_storage( Environment * _environment, char * _source_name, char * _target_name, FileStorageFormat _format, VariableType _type ) {
110
111 if ( !_environment->currentStorage ) {
113 }
114
115 FileStorage * fileStorage = malloc( sizeof( FileStorage ) );
116 memset( fileStorage, 0, sizeof ( FileStorage ) );
117
118 fileStorage->sourceName = strdup( _source_name );
119 if ( _target_name ) {
120 fileStorage->targetName = strdup( _target_name );
121 } else {
122 fileStorage->targetName = basename( _source_name );
123 }
124
125 switch( _format ) {
126 case FSF_BINARY: {
127 FILE * file = fopen( _source_name, "rb" );
128 if ( !file ) {
129 CRITICAL_DLOAD_MISSING_FILE( _source_name );
130 }
131 fseek( file, 0, SEEK_END );
132 int size = ftell( file );
133 fseek( file, 0, SEEK_SET );
134 fclose( file );
135 fileStorage->size = size;
136 break;
137 }
138 case FSF_CSV: {
139 int size, count;
140 fileStorage->content = file_read_csv( _environment, _source_name, _type, &size, &count );
141 fileStorage->size = size;
142 break;
143 }
144 }
145
146 if ( ! _environment->currentStorage->files ) {
147 _environment->currentStorage->files = fileStorage;
148 } else {
149 FileStorage * first = _environment->currentStorage->files;
150 while( first->next ) {
151 first = first->next;
152 }
153 first->next = fileStorage;
154 }
155
156 _environment->currentFileStorage = fileStorage;
157
158}
char * file_read_csv(Environment *_environment, char *_filename, VariableType _type, int *_size, int *_count)
char * basename(char *_path)
int size
Definition _optimizer.c:678
void file_storage(Environment *_environment, char *_source_name, char *_target_name, FileStorageFormat _format, VariableType _type)
Emit code for FILE ... AS ....
FileStorage * currentFileStorage
Definition ugbc.h:2536
Storage * currentStorage
Definition ugbc.h:2531
char * targetName
Definition ugbc.h:201
int size
Definition ugbc.h:204
char * sourceName
Definition ugbc.h:198
char * content
Definition ugbc.h:210
struct _FileStorage * next
Definition ugbc.h:213
FileStorage * files
Definition ugbc.h:232
void * malloc(YYSIZE_T)
#define CRITICAL_DLOAD_MISSING_FILE(f)
Definition ugbc.h:3578
#define CRITICAL_STORAGE_NOT_OPENED()
Definition ugbc.h:3577
@ FSF_CSV
Definition ugbc.h:252
@ FSF_BINARY
Definition ugbc.h:251
struct _Environment Environment
Structure of compilation environment.
enum _FileStorageFormat FileStorageFormat
struct _FileStorage FileStorage
Structure of a single file inside a storage.
enum _VariableType VariableType
Type of variables.