Change license from GPLv2+ to GPLv3+.
[pspp-builds.git] / src / libpspp / msg-locator.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18 #include <stdlib.h>
19 #include <libpspp/alloc.h>
20 #include "msg-locator.h"
21 #include <libpspp/message.h>
22 #include <libpspp/assertion.h>
23 #include "getl.h"
24
25 /* File locator stack. */
26 static const struct msg_locator **file_loc;
27
28 static int nfile_loc, mfile_loc;
29
30 void
31 msg_locator_done (void)
32 {
33   free(file_loc);
34   file_loc = NULL;
35   nfile_loc = mfile_loc = 0;
36 }
37
38
39 /* File locator stack functions. */
40
41 /* Pushes F onto the stack of file locations. */
42 void
43 msg_push_msg_locator (const struct msg_locator *loc)
44 {
45   if (nfile_loc >= mfile_loc)
46     {
47       if (mfile_loc == 0)
48         mfile_loc = 8;
49       else
50         mfile_loc *= 2;
51
52       file_loc = xnrealloc (file_loc, mfile_loc, sizeof *file_loc);
53     }
54
55   file_loc[nfile_loc++] = loc;
56 }
57
58 /* Pops F off the stack of file locations.
59    Argument F is only used for verification that that is actually the
60    item on top of the stack. */
61 void
62 msg_pop_msg_locator (const struct msg_locator *loc)
63 {
64   assert (nfile_loc >= 0 && file_loc[nfile_loc - 1] == loc);
65   nfile_loc--;
66 }
67
68 /* Puts the current file and line number into LOC, or NULL and -1 if
69    none. */
70 void
71 get_msg_location (const struct source_stream *ss, struct msg_locator *loc)
72 {
73   if (nfile_loc)
74     {
75       *loc = *file_loc[nfile_loc - 1];
76     }
77   else
78     {
79       loc->file_name = getl_source_name (ss);
80       loc->line_number = getl_source_location (ss);
81     }
82 }