VisitALE.c 16.4 KB
Newer Older
hrchilds's avatar
hrchilds committed
1 2
/*****************************************************************************
*
bonnell's avatar
bonnell committed
3
* Copyright (c) 2000 - 2015, Lawrence Livermore National Security, LLC
hrchilds's avatar
hrchilds committed
4
* Produced at the Lawrence Livermore National Laboratory
5
* LLNL-CODE-442911
hrchilds's avatar
hrchilds committed
6 7
* All rights reserved.
*
8
* This file is  part of VisIt. For  details, see https://visit.llnl.gov/.  The
hrchilds's avatar
hrchilds committed
9 10 11 12 13 14 15 16 17 18
* full copyright notice is contained in the file COPYRIGHT located at the root
* of the VisIt distribution or at http://www.llnl.gov/visit/copyright.html.
*
* Redistribution  and  use  in  source  and  binary  forms,  with  or  without
* modification, are permitted provided that the following conditions are met:
*
*  - Redistributions of  source code must  retain the above  copyright notice,
*    this list of conditions and the disclaimer below.
*  - Redistributions in binary form must reproduce the above copyright notice,
*    this  list of  conditions  and  the  disclaimer (as noted below)  in  the
19 20 21
*    documentation and/or other materials provided with the distribution.
*  - Neither the name of  the LLNS/LLNL nor the names of  its contributors may
*    be used to endorse or promote products derived from this software without
hrchilds's avatar
hrchilds committed
22 23 24 25 26
*    specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT  HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR  IMPLIED WARRANTIES, INCLUDING,  BUT NOT  LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE
27 28 29
* ARE  DISCLAIMED. IN  NO EVENT  SHALL LAWRENCE  LIVERMORE NATIONAL  SECURITY,
* LLC, THE  U.S.  DEPARTMENT OF  ENERGY  OR  CONTRIBUTORS BE  LIABLE  FOR  ANY
* DIRECT,  INDIRECT,   INCIDENTAL,   SPECIAL,   EXEMPLARY,  OR   CONSEQUENTIAL
hrchilds's avatar
hrchilds committed
30 31 32 33 34 35 36 37 38
* DAMAGES (INCLUDING, BUT NOT  LIMITED TO, PROCUREMENT OF  SUBSTITUTE GOODS OR
* SERVICES; LOSS OF  USE, DATA, OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER
* CAUSED  AND  ON  ANY  THEORY  OF  LIABILITY,  WHETHER  IN  CONTRACT,  STRICT
* LIABILITY, OR TORT  (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN ANY  WAY
* OUT OF THE  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*****************************************************************************/

hrchilds's avatar
hrchilds committed
39 40 41 42 43
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <sys/types.h>
hrchilds's avatar
hrchilds committed
44 45 46
#if defined(_WIN32)
#include <win32-regex.h>
#else
hrchilds's avatar
hrchilds committed
47
#include <regex.h>
hrchilds's avatar
hrchilds committed
48
#endif
hrchilds's avatar
hrchilds committed
49

hrchilds's avatar
hrchilds committed
50
#include <VisitALE.h>
hrchilds's avatar
hrchilds committed
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

static const NodeType ParentNode   = 0x01;
static const NodeType ViewNode     = 0x02;
static const NodeType FieldNode    = 0x04;
static const NodeType RelationNode = 0x08;
static const NodeType AnyNode      = 0xFF;

static void AddChildNode(Node *nd)
{
   int i ;
   Node **newChild = new Node *[nd->numChild+1] ;

   // expand child array 
   for (i=0; i<nd->numChild; ++i)
      newChild[i] = nd->child[i] ;
   if (nd->numChild != 0)
         delete [] nd->child ;
   nd->child = newChild ;
}

static void VisitParseInternal(char *buf, Node **result)
{
   int candidate ;
   Node *nd = new Node(ParentNode) ;
   char *vname ;

   while (*buf != '\0') {
      while (isspace(*buf)) ++buf ;

      switch (*buf) {
         case '(' :     // Views
         {
            ++buf ; ++buf ; // advance to next token
            vname = buf ;

            // advance to next token
            while (!isspace(*buf)) ++buf ;
            *buf++ = '\0' ;

            for (candidate=0; candidate<nd->numChild; ++candidate)
               if (!strcmp(vname, nd->child[candidate]->text))
                  break ;

            if (candidate == nd->numChild) {

               AddChildNode(nd) ;

               // create the node for the subview
               nd->child[nd->numChild] = new Node(ViewNode, nd) ;
               ++nd->numChild ;

               // set the current node to the subview node
               nd = nd->child[nd->numChild-1] ;

               // get the name of the subview
               nd->text = vname ;
            }
            else {
               /* this view already exists */
               nd = nd->child[candidate] ;
            }
         }
         break ;

         case '{' :     // Relations
         {
            ++buf ; ++buf ; // advance to next token

            AddChildNode(nd) ;

            // create the node for the relation
            nd->child[nd->numChild] = new Node(RelationNode, nd) ;
            ++nd->numChild ;

            // set the current node to the relation node
            nd = nd->child[nd->numChild-1] ;

            // get the name of the relation
            nd->text = buf ;

            // advance to next token
            while (!isspace(*buf)) ++buf ;
            *buf++ = '\0' ;
         }
         break ;

         case '[' :     // Fields
         {
            ++buf ; ++buf ; // advance to next token

            AddChildNode(nd) ;

            // create the node for the field
            nd->child[nd->numChild] = new Node(FieldNode, nd) ;
            ++nd->numChild ;

            // set the current node to the field node
            nd = nd->child[nd->numChild-1] ;

            // get the name of the field
            nd->text = buf ;

            // advance to next token
            while (!isspace(*buf)) ++buf ;
            *buf++ = '\0' ;
         }
         break ;

         case ')' :
         case '}' :
         case ']' :
            // go up a level of scope
            nd = nd->parent ;

            // advance to next token
            ++buf ; ++buf ;
            break ;

         default: // add sibling to list
            nd->next = new Node(nd->type, nd->parent) ;
            nd = nd->next ;

            // point at current text
            nd->text = buf ;

            // advance to next token
            while (!isspace(*buf)) ++buf ;
            *buf++ = '\0' ;
      }
   }
   *result = nd ;
}

static void ExtractAttr(Node *nd)
{
   int i ;

hrchilds's avatar
hrchilds committed
188
   if (nd == 0)
hrchilds's avatar
hrchilds committed
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
      return ;

   if (nd->type != ParentNode) {
      char *scan = nd->text ;
      while(*scan != ':') ++scan ;
      *scan++ = '\0' ;

      switch(nd->type) {
         case ViewNode:
            nd->len = atoi(scan) ;
            if (nd->len == 0)
               nd->len = nd->parent->len ;
            break ;

         case RelationNode:
            // get the length
            nd->size = atoi(scan) ;
            nd->len = nd->parent->len ;
            while(*scan != ':') ++scan ;
            ++scan ;
            nd->otherView = scan ;
            break ;

         case FieldNode:
            nd->datatype = *scan ;
            scan += 2 ;
            nd->min = atof(scan) ;
            while(*scan != ':') ++scan ;
            ++scan ;
            nd->max = atof(scan) ;
            nd->len = nd->parent->len ;
            break ;

         default: ;
      }


      ExtractAttr(nd->next) ;
   }

   for (i=0; i<nd->numChild; ++i)
      ExtractAttr(nd->child[i]) ;
}

static void VisitDumpTreeRecurse(Node *nd)
{
   int i ;

   if (nd->type != ParentNode) { 
      Node *sibling ;
      printf ("( %s ", nd->text) ;

hrchilds's avatar
hrchilds committed
241
      for (sibling = nd->next ; sibling != 0; sibling = sibling->next)
hrchilds's avatar
hrchilds committed
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
         printf("%s ", sibling->text) ;
   }

   for (i=0; i<nd->numChild; ++i)
      VisitDumpTreeRecurse(nd->child[i]) ;

   if (nd->type != ParentNode)
      printf(") ") ;
}

static void VisitDumpTree(Node *nd)
{
    VisitDumpTreeRecurse(nd);
    printf("\n");
}

void VisitFreeVistaInfo(Node *nd)
{
   int i ;
   Node *tmp ;

   for (i=0; i<nd->numChild; ++i)
      VisitFreeVistaInfo(nd->child[i]) ;

   do {
      tmp = nd ;
      nd = nd->next ;
      delete tmp ;
hrchilds's avatar
hrchilds committed
270
   } while (nd != 0) ;
hrchilds's avatar
hrchilds committed
271 272
}

hrchilds's avatar
hrchilds committed
273
static void VisitFindNodesTopDown(const Node *root, const int nlevels,
hrchilds's avatar
hrchilds committed
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
    const regex_t *compres, const NodeType *nodetypes, int level,
    Node ***results, int *nmatches, int *maxmatches)
{
   int i,j;

   if (level == nlevels)
      return;

   /* make a pass over children that do not have next pointers */
   for (i=0; i<root->numChild; ++i)
   {
      if ((root->child[i]->next == 0) &&
          ((root->child[i]->type & nodetypes[level]) != 0x0) &&
          (regexec(&compres[level], root->child[i]->text, 0, NULL, 0) == 0))
      {
         if (level == nlevels - 1)
         {
            /* if necessary, make room for more results */
            if (*nmatches == *maxmatches)
            {
               *maxmatches = (*maxmatches)*2 + 1;
               Node **newresults = new Node*[*maxmatches];
               for (j = 0; j < *nmatches; j++)
                  newresults[j] = (*results)[j];
               if (*results != 0)
                  delete [] *results;
               *results = newresults;
            }

            /* put in the new result */
            (*results)[*nmatches] = root->child[i];
            (*nmatches)++;
         }
         else
         {
hrchilds's avatar
hrchilds committed
309
            VisitFindNodesTopDown(root->child[i], nlevels, compres, nodetypes,
hrchilds's avatar
hrchilds committed
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
               level+1, results, nmatches, maxmatches);
         }
      }
   }

   /* make a pass over children that do have next pointers */
   for (i=0; i<root->numChild; ++i)
   {
      if ((level == nlevels - 1) && (root->child[i]->next != 0))
      {
         Node *nd = root->child[i];

         while (nd != 0)
         {
            if (((nd->type & nodetypes[level]) != 0x0) &&
                (regexec(&compres[level], nd->text, 0, NULL, 0) == 0))
            {
               /* if necessary, make room for more results */
               if (*nmatches == *maxmatches)
               {
                  *maxmatches = (*maxmatches)*2 + 1;
                  Node **newresults = new Node*[*maxmatches];
                  for (j = 0; j < *nmatches; j++)
                     newresults[j] = (*results)[j];
                  if (*results != 0)
                     delete [] *results;
                  *results = newresults;
               }

               /* put in the new result */
               (*results)[*nmatches] = nd;
               (*nmatches)++;
            }

            nd = nd->next;
         }
      }
   }
}

hrchilds's avatar
hrchilds committed
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 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
// walk up the tree from given node and confirm all path components match
static bool BottomUpMatch(const Node *bottom, int nlevels,
    const regex_t *compres, const NodeType *nodetypes)
{
   int level = nlevels - 1;
   bool match = true;

   while ((level >= 0) && match)
   {
      if ( !(((bottom->type & nodetypes[level]) != 0x0) &&
            (regexec(&compres[level], bottom->text, 0, NULL, 0) == 0)) )
         match = false;

      // move up the tree and path to match
      level--;
      bottom = bottom->parent;

      if ((bottom == 0) && (level >= 0))
          match = false;
   }  

   return match;
}

static void VisitFindNodesBottomUp(const Node *root, const int nlevels,
    const regex_t *compres, const NodeType *nodetypes, int level,
    Node ***results, int *nmatches, int *maxmatches)
{
   int i,j;

   if (root == 0)
      return;

   // make a pass over children that do NOT have next pointers
   for (i=0; i<root->numChild; ++i)
   {
      if ((root->child[i]->next == 0) &&
          BottomUpMatch(root->child[i], nlevels, compres, nodetypes))
      {
         /* if necessary, make room for more results */
         if (*nmatches == *maxmatches)
         {
            *maxmatches = (*maxmatches)*2 + 1;
            Node **newresults = new Node*[*maxmatches];
            for (j = 0; j < *nmatches; j++)
               newresults[j] = (*results)[j];
            if (*results != 0)
               delete [] *results;
            *results = newresults;
         }

         /* put in the new result */
         (*results)[*nmatches] = root->child[i];
         (*nmatches)++;
      }

      // we have to inspect the children too
      VisitFindNodesBottomUp(root->child[i], nlevels, compres, nodetypes,
         level+1, results, nmatches, maxmatches);
   }

   /* make a pass over children that do have next pointers */
   for (i=0; i<root->numChild; ++i)
   {
      if (root->child[i]->next != 0)
      {
         Node *nd = root->child[i];

         while (nd != 0)
         {
            if (BottomUpMatch(nd, nlevels, compres, nodetypes))
            {
               /* if necessary, make room for more results */
               if (*nmatches == *maxmatches)
               {
                  *maxmatches = (*maxmatches)*2 + 1;
                  Node **newresults = new Node*[*maxmatches];
                  for (j = 0; j < *nmatches; j++)
                     newresults[j] = (*results)[j];
                  if (*results != 0)
                     delete [] *results;
                  *results = newresults;
               }

               /* put in the new result */
               (*results)[*nmatches] = nd;
               (*nmatches)++;
            }

            nd = nd->next;
         }
      }
   }
}

hrchilds's avatar
hrchilds committed
445
static void VisitFindNodes(const Node *root, const char *slash_delimited_re,
hrchilds's avatar
hrchilds committed
446
   Node ***results, int *nmatches, RecurseMode rmode)
hrchilds's avatar
hrchilds committed
447 448
{
   const char *p = slash_delimited_re;
hrchilds's avatar
hrchilds committed
449
   int i, nlevels = 0;
hrchilds's avatar
hrchilds committed
450 451 452 453 454 455 456
   static char tmpstr[256];
   static regex_t compres[64];
   static NodeType nodetypes[64];

   /* compile each component's regular expression */
   while (*p != '\0')
   {
hrchilds's avatar
hrchilds committed
457
      i = 0;
hrchilds's avatar
hrchilds committed
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
      if (*p == '/') p++;
      nodetypes[nlevels] = AnyNode;

      /* for any component that begins with '%' character, the
         following V,F and/or R character(s) are used to specify
         the node type we are interested in */
      if (*p == '%')
      {
         nodetypes[nlevels] = 0;
         p++;
         while ((*p == 'V') || (*p == 'F') || (*p == 'R'))
         {
            switch (*p)
            {
               case 'V': nodetypes[nlevels] |= ViewNode; break;
               case 'F': nodetypes[nlevels] |= FieldNode; break;
               case 'R': nodetypes[nlevels] |= RelationNode; break;
            }
            p++;
         }
      }

      /* copy this path component's re into tmpstr */ 
481
      while ((*p != '/') && (*p != '\0') && ((size_t)i < sizeof(tmpstr)-1))
hrchilds's avatar
hrchilds committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
         tmpstr[i++] = *p++;
      if (i == sizeof(tmpstr)-1)
         printf("WARNING: Exceeded maximum size of regular expression\n");
      tmpstr[i] = '\0';

      /* compile the re for this path component */
      if (regcomp(&compres[nlevels], tmpstr, REG_EXTENDED|REG_NOSUB))
      {
         printf("WARNING: Error compiling regular expression from %s\n", tmpstr);
         break;
      }

      /* incriment the level */
      nlevels++;
      if (nlevels == sizeof(compres) / sizeof(compres[0]))
      {
         printf("WARNING: Exceeded maximum number of levels\n");
         break;
      }
   }

   /* make the call into the recursion routine */
hrchilds's avatar
hrchilds committed
504 505 506 507 508 509 510 511
   int maxmatches = *nmatches;
   if (rmode == TopDown)
       VisitFindNodesTopDown(root, nlevels, compres, nodetypes, 0,
          results, nmatches, &maxmatches);
   else
       VisitFindNodesBottomUp(root, nlevels, compres, nodetypes, 0,
          results, nmatches, &maxmatches);

hrchilds's avatar
hrchilds committed
512 513 514
   /* free the compiled regular expressions */
   for (i = 0; i < nlevels; i++)
       regfree(&compres[i]);
hrchilds's avatar
hrchilds committed
515 516
}

hrchilds's avatar
hrchilds committed
517
const Node *VisitGetNodeFromPath(const Node *start, const char *path)
hrchilds's avatar
hrchilds committed
518 519
{

hrchilds's avatar
hrchilds committed
520
   const Node *root = start;
hrchilds's avatar
hrchilds committed
521 522 523 524 525 526 527 528
   do {
      int i ;
      bool found = false ;

      if (*path == '/') ++path ;

      for (i=0; i<root->numChild; ++i) {
         int len = strlen(root->child[i]->text) ;
hrchilds's avatar
hrchilds committed
529 530 531 532
         int plen = strlen(path);
         if ((strncmp(path, root->child[i]->text, len) == 0) &&
            (len <= plen) && ((path[len] == '/') || (path[len] == '\0')))
         {
hrchilds's avatar
hrchilds committed
533 534 535 536 537 538 539
            root = root->child[i] ;
            path += len ;
            found = true ;
            break ;
         }
      }
      if (found == false) {
hrchilds's avatar
hrchilds committed
540
         root = 0 ;
hrchilds's avatar
hrchilds committed
541 542 543 544 545 546 547
         break ;
      }
   } while (*path != '\0') ;

   return root ;
}

548 549 550 551 552
//  Modifications:
//    Jeremy Meredith, Thu Aug  7 13:52:06 EDT 2008
//    Order of operations in one of the while loops might be undefined.
//    I defined it as best as I could discern the proper order was.
//
hrchilds's avatar
hrchilds committed
553
char *VisitGetPathFromNode(const Node *root, const Node *node)
hrchilds's avatar
hrchilds committed
554 555 556 557 558 559 560
{
   const Node *tmp;

   // make a pass, walking up the tree, and
   // compute size of returned string
   tmp = node;
   int len = 0;
hrchilds's avatar
hrchilds committed
561
   while ((tmp != 0) && (tmp != root))
hrchilds's avatar
hrchilds committed
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
   {
      if (tmp->text != 0)
          len += (strlen(tmp->text)+1); // +1 for '/'

      tmp = tmp->parent;
   }

   if (len == 0)
       return 0;

   char *retval = new char[len+1];
   retval[len] = '\0';

   // make a second pass and populate the returned string
   tmp = node;
hrchilds's avatar
hrchilds committed
577
   while ((tmp != 0) && (tmp != root))
hrchilds's avatar
hrchilds committed
578 579 580 581 582 583 584
   {
      if (tmp->text != 0)
      {
          int complen = strlen(tmp->text);
          len -= (complen+1);
          retval[len] = '/';
          while (complen)
585 586 587 588
          {
              --complen;
              retval[len+complen+1] = tmp->text[complen];
          }
hrchilds's avatar
hrchilds committed
589 590 591 592 593 594 595 596
      }

      tmp = tmp->parent;
   }

   return retval;

}