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