]> git.sur5r.net Git - cc65/blob - src/cc65/asmline.c
Added the io module
[cc65] / src / cc65 / asmline.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 asmline.h                                 */
4 /*                                                                           */
5 /*                     Internal assembler line structure                     */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000     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 #include <stdio.h>
37
38 #include "../common/xmalloc.h"
39 #include "../common/xsprintf.h"
40
41 #include "error.h"
42 #include "asmline.h"
43
44
45
46 /*****************************************************************************/
47 /*                                   Data                                    */
48 /*****************************************************************************/
49
50
51
52 /* Number used to index lines */
53 static unsigned long LineIndex = 0;
54
55 /* The line list */
56 Line*  FirstLine = 0;           /* Pointer to first line */
57 Line*  LastLine  = 0;           /* Pointer to last line */
58
59
60
61 /*****************************************************************************/
62 /*                                   Code                                    */
63 /*****************************************************************************/
64
65
66
67 static Line* NewLine (const char* Format, va_list ap)
68 /* Interal routine to create a new line from the given text */
69 {
70     char        Buf [8192];
71     unsigned    Len;
72     Line*       L;
73
74
75     /* Make a string from the given format and arguments */
76     xvsprintf (Buf, sizeof (Buf), Format, ap);
77
78     /* Get the length of the line */
79     Len = strlen (Buf);
80
81     /* Allocate memory */
82     L = xmalloc (sizeof (Line) + Len);
83
84     /* Partially initialize the struct (the remaining fields are initialized
85      * by the caller).
86      */
87     L->Flags    = 0;
88     L->Size     = 0;
89     L->Len      = Len;
90     memcpy (L->Line, Buf, Len+1);
91
92     /* Return the new line */
93     return L;
94 }
95
96
97
98 Line* NewCodeLine (const char* Format, va_list ap)
99 /* Create a new code line and return it */
100 {
101     /* Create a new line struct */
102     Line* L = NewLine (Format, ap);
103
104     /* Initialize struct fields */
105     L->Index    = LineIndex++;
106
107     /* Insert the line into the list */
108     if (FirstLine == 0) {
109         /* The list is empty */
110         L->Next = L->Prev = 0;
111         FirstLine = LastLine = L;
112     } else {
113         /* There are entries in the list, add the new one at the end */
114         LastLine->Next = L;
115         L->Prev = LastLine;
116         L->Next = 0;
117         LastLine = L;
118     }
119
120     /* Return the new line */
121     return L;
122 }
123
124
125
126 Line* NewCodeLineAfter (Line* LineBefore, const char* Format, va_list ap)
127 /* Create a new line, insert it after L and return it. */
128 {
129     /* Create a new line struct */
130     Line* L = NewLine (Format, ap);
131
132     /* Initialize struct fields. We use the same index for the inserted line
133      * as for its predecessor, since we cannot create new numbers on the
134      * fly and the index is only used to determine sort order.
135      */
136     L->Index    = LineBefore->Index;
137
138     /* Insert the line after its predecessor */
139     L->Next = LineBefore->Next;
140     LineBefore->Next = L;
141     L->Prev = LineBefore;
142     if (L->Next) {
143         L->Next->Prev = L;
144     } else {
145         /* This is the last line */
146         LastLine = L;
147     }
148
149     /* Return the new line */
150     return L;
151 }
152
153
154
155 void FreeCodeLine (Line* L)
156 /* Remove a line from the list and free it */
157 {
158     /* Unlink the line */
159     if (L->Prev == 0) {
160         /* No line before this one */
161         FirstLine = L->Next;
162     } else {
163         L->Prev->Next = L->Next;
164     }
165     if (L->Next == 0) {
166         /* No line after this one */
167         LastLine = L->Prev;
168     } else {
169         L->Next->Prev = L->Prev;
170     }
171
172     /* Free the struct */
173     xfree (L);
174 }
175
176
177