| 1 | /* listpos.c | 
|---|
| 2 | * SURVEX Cave surveying software: stuff to do with stn position output | 
|---|
| 3 | * Copyright (C) 1991-2002,2011,2012,2013 Olly Betts | 
|---|
| 4 | * | 
|---|
| 5 | * This program is free software; you can redistribute it and/or modify | 
|---|
| 6 | * it under the terms of the GNU General Public License as published by | 
|---|
| 7 | * the Free Software Foundation; either version 2 of the License, or | 
|---|
| 8 | * (at your option) any later version. | 
|---|
| 9 | * | 
|---|
| 10 | * This program is distributed in the hope that it will be useful, | 
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 13 | * GNU 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 St, Fifth Floor, Boston, MA  02110-1301  USA | 
|---|
| 18 | */ | 
|---|
| 19 |  | 
|---|
| 20 | #ifdef HAVE_CONFIG_H | 
|---|
| 21 | # include <config.h> | 
|---|
| 22 | #endif | 
|---|
| 23 |  | 
|---|
| 24 | #define PRINT_STN_POS_LIST 1 | 
|---|
| 25 | #define NODESTAT 1 | 
|---|
| 26 |  | 
|---|
| 27 | #include <limits.h> | 
|---|
| 28 |  | 
|---|
| 29 | #include "cavern.h" | 
|---|
| 30 | #include "datain.h" | 
|---|
| 31 | #include "debug.h" | 
|---|
| 32 | #include "filename.h" | 
|---|
| 33 | #include "message.h" | 
|---|
| 34 | #include "filelist.h" | 
|---|
| 35 | #include "netbits.h" | 
|---|
| 36 | #include "listpos.h" | 
|---|
| 37 | #include "out.h" | 
|---|
| 38 |  | 
|---|
| 39 | /* Traverse prefix tree depth first starting at from, and | 
|---|
| 40 | * calling function fn at each node */ | 
|---|
| 41 | static void | 
|---|
| 42 | traverse_prefix_tree(prefix *from, void (*fn)(prefix *)) | 
|---|
| 43 | { | 
|---|
| 44 | prefix *p; | 
|---|
| 45 |  | 
|---|
| 46 | fn(from); | 
|---|
| 47 |  | 
|---|
| 48 | p = from->down; | 
|---|
| 49 | if (!p) return; | 
|---|
| 50 |  | 
|---|
| 51 | while (1) { | 
|---|
| 52 | fn(p); | 
|---|
| 53 | if (p->down) { | 
|---|
| 54 | p = p->down; | 
|---|
| 55 | } else { | 
|---|
| 56 | if (!p->right) { | 
|---|
| 57 | do { | 
|---|
| 58 | p = p->up; | 
|---|
| 59 | if (p == from) return; /* got back to start */ | 
|---|
| 60 | } while (!p->right); | 
|---|
| 61 | } | 
|---|
| 62 | p = p->right; | 
|---|
| 63 | } | 
|---|
| 64 | } | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | static void | 
|---|
| 68 | check_node(prefix *p) | 
|---|
| 69 | { | 
|---|
| 70 | if (!p->pos) { | 
|---|
| 71 | if (!TSTBIT(p->sflags, SFLAGS_SURVEY)) { | 
|---|
| 72 | /* Could do away with the SFLAGS_SURVEY check and check | 
|---|
| 73 | * p->min_export instead of p->max_export I think ... */ | 
|---|
| 74 | if (TSTBIT(p->sflags, SFLAGS_ENTRANCE) || p->max_export) { | 
|---|
| 75 | /* p is a station which was referred to in "*entrance" and/or | 
|---|
| 76 | * "*export" but not elsewhere (otherwise it'd have a position). | 
|---|
| 77 | * p could also be a survey (SFLAGS_SURVEY) or be mentioned as | 
|---|
| 78 | * a station, but only in a line of data which was rejected | 
|---|
| 79 | * because of an error. | 
|---|
| 80 | */ | 
|---|
| 81 | warning_in_file(p->filename, p->line, | 
|---|
| 82 | /*Station “%s” referred to by *entrance or *export but never used*/190, | 
|---|
| 83 | sprint_prefix(p)); | 
|---|
| 84 | } | 
|---|
| 85 | } | 
|---|
| 86 | } else { | 
|---|
| 87 | /* Do we need to worry about export violations in hanging surveys? */ | 
|---|
| 88 | if (fExportUsed) { | 
|---|
| 89 | #if 0 | 
|---|
| 90 | printf("L min %d max %d pfx %s\n", | 
|---|
| 91 | p->min_export, p->max_export, sprint_prefix(p)); | 
|---|
| 92 | #endif | 
|---|
| 93 | if ((p->min_export > 1 && p->min_export != USHRT_MAX) || | 
|---|
| 94 | (p->min_export == 0 && p->max_export)) { | 
|---|
| 95 | char *s; | 
|---|
| 96 | const char *filename_store = file.filename; | 
|---|
| 97 | unsigned int line_store = file.line; | 
|---|
| 98 | prefix *where = p->up; | 
|---|
| 99 | SVX_ASSERT(where); | 
|---|
| 100 | s = osstrdup(sprint_prefix(where)); | 
|---|
| 101 | /* Report better when station called 2.1 for example */ | 
|---|
| 102 | while (!where->filename && where->up) where = where->up; | 
|---|
| 103 |  | 
|---|
| 104 | file.filename = where->filename; | 
|---|
| 105 | file.line = where->line; | 
|---|
| 106 | if (TSTBIT(where->sflags, SFLAGS_PREFIX_ENTERED)) { | 
|---|
| 107 | compile_error(/*Station “%s” not exported from survey “%s”*/26, | 
|---|
| 108 | sprint_prefix(p), s); | 
|---|
| 109 | } else { | 
|---|
| 110 | compile_error(/*Reference to station “%s” from non-existent survey “%s”*/286, | 
|---|
| 111 | sprint_prefix(p), s); | 
|---|
| 112 | } | 
|---|
| 113 | file.filename = filename_store; | 
|---|
| 114 | file.line = line_store; | 
|---|
| 115 | osfree(s); | 
|---|
| 116 | } | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | if (TSTBIT(p->sflags, SFLAGS_SUSPECTTYPO)) { | 
|---|
| 120 | warning_in_file(p->filename, p->line, | 
|---|
| 121 | /*Station “%s” referred to just once, with an explicit prefix - typo?*/70, | 
|---|
| 122 | sprint_prefix(p)); | 
|---|
| 123 | } | 
|---|
| 124 | } | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | #if NODESTAT | 
|---|
| 128 | static int *cOrder; | 
|---|
| 129 | static int icOrderMac; | 
|---|
| 130 |  | 
|---|
| 131 | static void | 
|---|
| 132 | node_stat(prefix *p) | 
|---|
| 133 | { | 
|---|
| 134 | if (p->pos) { | 
|---|
| 135 | int order; | 
|---|
| 136 | SVX_ASSERT(pfx_fixed(p)); | 
|---|
| 137 |  | 
|---|
| 138 | order = p->shape; | 
|---|
| 139 |  | 
|---|
| 140 | if (order >= icOrderMac) { | 
|---|
| 141 | int c = order * 2; | 
|---|
| 142 | cOrder = osrealloc(cOrder, c * ossizeof(int)); | 
|---|
| 143 | while (icOrderMac < c) cOrder[icOrderMac++] = 0; | 
|---|
| 144 | } | 
|---|
| 145 | cOrder[order]++; | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | check_node(p); | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | static void | 
|---|
| 152 | node_stats(prefix *from) | 
|---|
| 153 | { | 
|---|
| 154 | prefix * p; | 
|---|
| 155 | icOrderMac = 8; /* Start value */ | 
|---|
| 156 | cOrder = osmalloc(icOrderMac * ossizeof(int)); | 
|---|
| 157 | memset(cOrder, 0, icOrderMac * ossizeof(int)); | 
|---|
| 158 | traverse_prefix_tree(from, node_stat); | 
|---|
| 159 | for (p = anon_list; p; p = p->right) { | 
|---|
| 160 | node_stat(p); | 
|---|
| 161 | } | 
|---|
| 162 | } | 
|---|
| 163 | #endif | 
|---|
| 164 |  | 
|---|
| 165 | extern void | 
|---|
| 166 | print_node_stats(void) | 
|---|
| 167 | { | 
|---|
| 168 | #if NODESTAT | 
|---|
| 169 | int c; | 
|---|
| 170 | int max_c = 9999; /* Always allow space for 4 digits. */ | 
|---|
| 171 | int width; | 
|---|
| 172 | node_stats(root); | 
|---|
| 173 | for (c = 0; c < icOrderMac; c++) { | 
|---|
| 174 | if (cOrder[c] > max_c) { | 
|---|
| 175 | max_c = cOrder[c]; | 
|---|
| 176 | } | 
|---|
| 177 | } | 
|---|
| 178 | width = 1 + (int)log10((double)max_c); | 
|---|
| 179 | for (c = 0; c < icOrderMac; c++) { | 
|---|
| 180 | if (cOrder[c] > 0) { | 
|---|
| 181 | printf("%*d %d-%s.\n", width, cOrder[c], c, | 
|---|
| 182 | msg(cOrder[c] == 1 ? /*node*/176 : /*nodes*/177)); | 
|---|
| 183 | } | 
|---|
| 184 | } | 
|---|
| 185 | #endif | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | extern void | 
|---|
| 189 | check_node_stats(void) | 
|---|
| 190 | { | 
|---|
| 191 | traverse_prefix_tree(root, check_node); | 
|---|
| 192 | } | 
|---|