1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
|
/*
* axislabel.c - linked list procedures building and maintaining lists
* of axis labels and positions
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <malloc.h>
#include "CNplot.h"
static void print_axislabel();
/*
* Given a string of axis label/value, parse it and fill the axislabel list
*/
void CNparse_axislabel_string(labelhead, labeltail, label)
CNaxislabelptr *labelhead, *labeltail;
char *label;
{
char *word[CN_MAXWORDS];
char pos[CN_MAXCHAR];
char name[CN_MAXCHAR];
int argfound = CN_FALSE;
int nw=0;
int i;
if (label == NULL || strlen(label) < 0) return;
/* Strip "(" or ")" */
for (i=0; label[i]!='\0'; i++) {
if (label[i] == '(') label[i] = ' ';
if (label[i] == ')') label[i] = ' ';
}
/* Parse line into lots of words */
if ((nw = CNgetwords(label,word,CN_MAXWORDS)) >= 2) {
/* Copy the first 2 words */
CNassign_string_keyword(pos ,word[0],"argument",0);
CNassign_string_keyword(name,word[1],"value ",0);
argfound = CN_TRUE;
} else {
(void) fprintf(stderr," Warning! axislabel needs 2 arguments: ");
(void) fprintf(stderr,"axislabel = (pos name)\n");
}
/* If the 2 arguments were found, then add the label to the list */
if (argfound) {
(void) CNinsert_axislabel(labelhead, labeltail, atof(pos), name);
}
/* Free the words */
if (nw > 0) CNfreewords(&nw,word);
}
/*
* Print the list of axis labels
*/
/*ARGSUSED*/
void CNwrite_xaxislabels(fp, labelhead, labeltail)
FILE *fp;
CNaxislabelptr labelhead, labeltail;
{
CNaxislabelptr A;
if (fp == NULL) return;
if (labelhead == NULL) return;
for (A=labelhead; A!=NULL; A=A->next) {
(void) fprintf(fp,"%% xaxislabel = (%g \"%s\")\n",A->pos, A->name);
}
}
/*
* Print the list of axis labels
*/
/*ARGSUSED*/
void CNwrite_yaxislabels(fp, labelhead, labeltail)
FILE *fp;
CNaxislabelptr labelhead, labeltail;
{
CNaxislabelptr A;
if (fp == NULL) return;
if (labelhead == NULL) return;
for (A=labelhead; A!=NULL; A=A->next) {
(void) fprintf(fp,"%% yaxislabel = (%g \"%s\")\n",A->pos, A->name);
}
}
/*
* Print the list of axis labels
*/
/*ARGSUSED*/
void CNwrite_zaxislabels(fp, labelhead, labeltail)
FILE *fp;
CNaxislabelptr labelhead, labeltail;
{
CNaxislabelptr A;
if (fp == NULL) return;
if (labelhead == NULL) return;
for (A=labelhead; A!=NULL; A=A->next) {
(void) fprintf(fp,"%% zaxislabel = (%g \"%s\")\n",A->pos, A->name);
}
}
/*
* AXISLABEL DATA STRUCTURE
* A axislabel-structure is used to store the position of the axis label
* and the axis label itself.
* Axis-labels are stored in an ordered list (sorted by axis position)
* so the list-insertion routines used here differ from the norm.
*/
/*
* Allocate room for an axislabel-structure
*/
CNaxislabelptr CNmake_axislabel(pos, name)
double pos;
char *name;
{
CNaxislabelptr newptr;
unsigned int size = sizeof(CNaxislabel);
if ((newptr = (CNaxislabelptr)malloc(size))!=NULL) {
/* position and name */
newptr->pos = pos;
newptr->name = CNcreate_string(name);
/* Set the linked lists */
newptr->next = NULL;
newptr->prev = NULL;
}
return(newptr);
}
/*
* Insert a axislabel at the tail of the current axislabel list
*/
static CNaxislabelptr store_axislabel(axislabel_listhead, axislabel_listtail,
L, B)
CNaxislabelptr *axislabel_listhead, *axislabel_listtail, L, B;
{
CNaxislabelptr next,A;
A = L;
if (B!=NULL) {
if (A==NULL) {
*axislabel_listhead = B;
*axislabel_listtail = B;
} else {
next = A->next;
B->next = next;
B->prev = A;
A->next = B;
if (next != NULL) next->prev = B;
if (B->next == NULL) *axislabel_listtail = B;
}
}
return(B);
}
/*
* Insert a axislabel at the head of the current axislabel list
*/
static CNaxislabelptr store_headaxislabel(axislabel_listhead,
axislabel_listtail,
B)
CNaxislabelptr *axislabel_listhead, *axislabel_listtail, B;
{
CNaxislabelptr A;
A = *axislabel_listhead;
if (B!=NULL) {
if (A==NULL) {
*axislabel_listhead = B;
*axislabel_listtail = B;
} else {
B->next = A;
A->prev = B;
*axislabel_listhead = B;
}
}
return(B);
}
/*
* Insert a axislabel at the tail of the current axislabel list
*/
static CNaxislabelptr insert_axislabel(axislabel_listhead, axislabel_listtail,
L, pos, name)
CNaxislabelptr *axislabel_listhead, *axislabel_listtail, L;
double pos;
char *name;
{
return(store_axislabel(axislabel_listhead, axislabel_listtail,
L, CNmake_axislabel(pos, name)));
}
/*
* Insert a axislabel at the tail of the current axislabel list
*/
static CNaxislabelptr insert_headaxislabel(axislabel_listhead,
axislabel_listtail,
pos, name)
CNaxislabelptr *axislabel_listhead, *axislabel_listtail;
double pos;
char *name;
{
return(store_headaxislabel(axislabel_listhead, axislabel_listtail,
CNmake_axislabel(pos, name)));
}
/*
* Insert a axislabel at the tail of the current axislabel list
*/
CNaxislabelptr CNinsert_axislabel(axislabel_listhead, axislabel_listtail,
pos, name)
CNaxislabelptr *axislabel_listhead, *axislabel_listtail;
double pos;
char *name;
{
CNaxislabelptr C, CL, Cptr=NULL;
int FOUND;
if ((*axislabel_listhead == NULL) || (pos < (*axislabel_listhead)->pos)) {
/* Insert at the head */
Cptr = insert_headaxislabel(axislabel_listhead, axislabel_listtail,
pos, name);
} else {
/*
* Search for the insertion point
* Identical axislabels are NOT added to the list
*/
FOUND = CN_FALSE;
for (C=(*axislabel_listhead); C!=NULL && !FOUND; C=C->next) {
if (C->next == NULL) {
CL = C;
FOUND = CN_TRUE;
} else if (C->pos < pos && C->next->pos > pos) {
CL = C;
FOUND = CN_TRUE;
} else if (C->pos == pos) {
CL = NULL;
FOUND = CN_TRUE;
}
}
if (CL!=NULL)
Cptr = insert_axislabel(axislabel_listhead, axislabel_listtail, CL,
pos, name);
}
return(Cptr);
}
/*
* Insert a axislabel in the current axislabel list
*/
CNaxislabelptr CNstore_axislabel(axislabel_listhead, axislabel_listtail, L)
CNaxislabelptr *axislabel_listhead, *axislabel_listtail, L;
{
CNaxislabelptr C, CL, Cptr=NULL;
int FOUND;
if (L==NULL) return(NULL);
if ((*axislabel_listhead == NULL) || (L->pos < (*axislabel_listhead)->pos)){
/* Insert at the head */
Cptr = store_headaxislabel(axislabel_listhead, axislabel_listtail, L);
} else {
/*
* Search for the insertion point
* Identical axislabels are NOT added to the list
*/
FOUND = CN_FALSE;
for (C=(*axislabel_listhead); C!=NULL && !FOUND; C=C->next) {
if (C->next == NULL) {
CL = C;
FOUND = CN_TRUE;
} else if (C->pos < L->pos && C->next->pos > L->pos) {
CL = C;
FOUND = CN_TRUE;
} else if (C->pos == L->pos) {
CL = NULL;
FOUND = CN_TRUE;
}
}
if (CL!=NULL)
Cptr = store_axislabel(axislabel_listhead, axislabel_listtail, CL, L);
}
return(Cptr);
}
/*
* Delete axislabel at address L
*/
void CNdelete_axislabel(axislabel_listhead, axislabel_listtail, L)
CNaxislabelptr *axislabel_listhead, *axislabel_listtail;
CNaxislabelptr L;
{
CNaxislabelptr prev,next;
/* Delete the name string */
if (L->name) free((char *)L->name);
prev = L->prev;
next = L->next;
if (prev!=NULL) prev->next = next;
if (next!=NULL) next->prev = prev;
if (L== *axislabel_listhead) *axislabel_listhead = next;
if (L== *axislabel_listtail) *axislabel_listtail = prev;
free ((char*)L);
L = NULL;
}
/*
* Delete all the axislabels in the list
*/
void CNdelete_axislabel_list(axislabel_listhead, axislabel_listtail)
CNaxislabelptr *axislabel_listhead, *axislabel_listtail;
{
CNaxislabelptr P;
while ((P = *axislabel_listhead) != NULL)
CNdelete_axislabel(axislabel_listhead, axislabel_listtail, P);
}
/*
* Print out the list of axislabels
*/
/*ARGSUSED*/
void CNprint_axislabel_list(axislabel_listhead, axislabel_listtail)
CNaxislabelptr axislabel_listhead, axislabel_listtail;
{
CNaxislabelptr C;
(void) printf("%d axislabels\n",
CNcount_axislabels(axislabel_listhead, axislabel_listtail));
for (C=axislabel_listhead; C!=NULL; C=C->next)
print_axislabel(C);
(void) printf("\n");
}
/*
* print the values of a axislabel
*/
static void print_axislabel(C)
CNaxislabelptr C;
{
(void) fprintf(stdout,"pos= %10.5f label=\"%s\"\n",C->pos, C->name);
}
/*
* Count the number of axislabels in the list
*/
/*ARGSUSED*/
int CNcount_axislabels(axislabel_listhead, axislabel_listtail)
CNaxislabelptr axislabel_listhead, axislabel_listtail;
{
CNaxislabelptr P;
int count = 0;
for (P=axislabel_listhead; P!=NULL; P=P->next) count++;
return(count);
}
/*
* Copy one axislabel list to another
*/
/*ARGSUSED*/
void CNcopy_axislabel_list(Cto_head, Cto_tail, Cfr_head, Cfr_tail)
CNaxislabelptr *Cto_head, *Cto_tail;
CNaxislabelptr Cfr_head, Cfr_tail;
{
CNaxislabelptr C;
/* Delete the old list */
CNdelete_axislabel_list(Cto_head, Cto_tail);
/* Create a new list */
for (C=Cfr_head; C!=NULL; C=C->next) {
(void) CNinsert_axislabel(Cto_head, Cto_tail, C->pos, C->name);
}
}
|