]> git.sur5r.net Git - cc65/blob - src/cc65/dataseg.c
980246a05e48d674520bfbb6cc4f6f235cc531f6
[cc65] / src / cc65 / dataseg.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 dataseg.c                                 */
4 /*                                                                           */
5 /*                          Data segment structure                           */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2001     Ullrich von Bassewitz                                        */
10 /*              Wacholderweg 14                                              */
11 /*              D-70597 Stuttgart                                            */
12 /* EMail:       uz@musoftware.de                                             */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 /* common */
37 #include "check.h"
38 #include "xmalloc.h"
39 #include "xsprintf.h"
40
41 /* cc65 */
42 #include "error.h"
43 #include "dataseg.h"
44
45
46
47 /*****************************************************************************/
48 /*                                   Code                                    */
49 /*****************************************************************************/
50
51
52
53 DataSeg* NewDataSeg (const char* Name, SymEntry* Func)
54 /* Create a new data segment, initialize and return it */
55 {
56     /* Allocate memory */
57     DataSeg* S  = xmalloc (sizeof (DataSeg));
58
59     /* Initialize the fields */
60     S->SegName  = xstrdup (Name);
61     S->Func     = Func;
62     InitCollection (&S->Lines);
63
64     /* Return the new struct */
65     return S;
66 }
67
68
69
70 void FreeDataSeg (DataSeg* S)
71 /* Free a data segment including all line entries */
72 {
73     Internal ("Not implemented");
74 }
75
76
77
78 void AppendDataSeg (DataSeg* Target, const DataSeg* Source)
79 /* Append the data from Source to Target */
80 {
81     unsigned I;
82
83     /* Append all lines from Source to Target */
84     unsigned Count = CollCount (&Source->Lines);
85     for (I = 0; I < Count; ++I) {
86         CollAppend (&Target->Lines, xstrdup (CollConstAt (&Source->Lines, I)));
87     }
88 }
89
90
91
92 void AddDataEntry (DataSeg* S, const char* Format, va_list ap)
93 /* Add a line to the given data segment */
94 {
95     /* Format the line */
96     char Buf [256];
97     xvsprintf (Buf, sizeof (Buf), Format, ap);
98
99     /* Add a copy to the data segment */
100     CollAppend (&S->Lines, xstrdup (Buf));
101 }
102
103
104
105 void OutputDataSeg (const DataSeg* S, FILE* F)
106 /* Output the data segment data to a file */
107 {
108     unsigned I;
109
110     /* Get the number of entries in this segment */
111     unsigned Count = CollCount (&S->Lines);
112
113     /* If the segment is actually empty, bail out */
114     if (Count == 0) {
115         return;
116     }
117
118     /* Output the segment directive */
119     fprintf (F, ".segment\t\"%s\"\n\n", S->SegName);
120
121     /* Output all entries */
122     for (I = 0; I < Count; ++I) {
123         fprintf (F, "%s\n", (const char*) CollConstAt (&S->Lines, I));
124     }
125
126     /* Add an additional newline after the segment output */
127     fprintf (F, "\n");
128 }
129
130
131