]> git.sur5r.net Git - cc65/blob - src/cc65/dataseg.c
72f7828532db504d7154e8b72a6556f3a2edd97f
[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 "dataseg.h"
43
44
45
46 /*****************************************************************************/
47 /*                                   Data                                    */
48 /*****************************************************************************/
49
50
51
52 /* Pointer to current data segment */
53 DataSeg* DS = 0;
54
55
56
57 /*****************************************************************************/
58 /*                                   Code                                    */
59 /*****************************************************************************/
60
61
62
63 DataSeg* NewDataSeg (const char* Name)
64 /* Create a new data segment, initialize and return it */
65 {
66     /* Allocate memory */
67     DataSeg* S = xmalloc (sizeof (DataSeg));
68
69     /* Initialize the fields */
70     S->Next = 0;
71     S->Name = xstrdup (Name);
72     InitCollection (&S->Lines);
73
74     /* Return the new struct */
75     return S;
76 }
77
78
79
80 void FreeDataSeg (DataSeg* S)
81 /* Free a data segment including all line entries */
82 {
83     unsigned I, Count;
84                              
85     /* Free the name */
86     xfree (S->Name);
87
88     /* Free the lines */
89     Count = CollCount (&S->Lines);
90     for (I = 0; I < Count; ++I) {
91         xfree (CollAt (&S->Lines, I));
92     }
93
94     /* Free the collection */
95     DoneCollection (&S->Lines);
96
97     /* Free the struct */
98     xfree (S);
99 }
100
101
102
103 void PushDataSeg (DataSeg* S)
104 /* Push the given data segment onto the stack */
105 {
106     /* Push */
107     S->Next = DS;
108     DS      = S;
109 }
110
111
112
113 DataSeg* PopDataSeg (void)
114 /* Remove the current data segment from the stack and return it */
115 {
116     /* Remember the current data segment */
117     DataSeg* S = DS;
118
119     /* Cannot pop on empty stack */
120     PRECONDITION (S != 0);
121
122     /* Pop */
123     DS = S->Next;
124
125     /* Return the popped data segment */
126     return S;
127 }
128
129
130
131 void AppendDataSeg (DataSeg* Target, const DataSeg* Source)
132 /* Append the data from Source to Target */
133 {
134     unsigned I;
135
136     /* Append all lines from Source to Target */
137     unsigned Count = CollCount (&Source->Lines);
138     for (I = 0; I < Count; ++I) {
139         CollAppend (&Target->Lines, xstrdup (CollConstAt (&Source->Lines, I)));
140     }
141 }
142
143
144
145 void AddDataSegLine (DataSeg* S, const char* Format, ...)
146 /* Add a line to the given data segment */
147 {
148     va_list ap;
149     char Buf [256];
150
151     /* Format the line */
152     va_start (ap, Format);
153     xvsprintf (Buf, sizeof (Buf), Format, ap);
154     va_end (ap);
155
156     /* Add a copy to the data segment */
157     CollAppend (&S->Lines, xstrdup (Buf));
158 }
159
160
161
162 void OutputDataSeg (FILE* F, const DataSeg* S)
163 /* Output the data segment data to a file */
164 {
165     unsigned I;
166
167     /* Get the number of entries in this segment */
168     unsigned Count = CollCount (&S->Lines);
169
170     /* Output all entries */
171     for (I = 0; I < Count; ++I) {
172         fprintf (F, "%s\n", (const char*) CollConstAt (&S->Lines, I));
173     }
174 }
175
176
177