Changeset 38 for trunk/src/reglookup.c


Ignore:
Timestamp:
07/31/05 11:00:59 (19 years ago)
Author:
tim
Message:

Cleaned up path2Stack. Added several supporting functions in void_stack code.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/reglookup.c

    r37 r38  
    2828#include "../include/void_stack.h"
    2929
    30 /* XXX: this needs to be rewritten to malloc each resulting string, instead of
    31  *      altering them in place
    32  */
     30
     31void bailOut(int code, char* message)
     32{
     33  fprintf(stderr, message);
     34  exit(code);
     35}
     36
     37
    3338void_stack* path2Stack(const char* s)
    3439{
    35   void_stack* ret_val = void_stack_new(1024);
     40  void_stack* ret_val;
     41  void_stack* rev_ret = void_stack_new(1024);
     42  const char* cur = s;
    3643  char* next = NULL;
    37   char* cur;
     44  char* copy;
     45
     46  if (rev_ret == NULL)
     47    return NULL;
    3848  if (s == NULL)
    39     return ret_val;
    40   else
    41     cur = strdup(s);
    42 
    43   while((next = strrchr(cur, '/')) != NULL)
    44   {
    45     next[0] = '\0';
    46     if(strlen(next+1) > 0)
    47       void_stack_push(ret_val, next+1);
     49    return rev_ret;
     50 
     51  while((next = strchr(cur, '/')) != NULL)
     52  {
     53    if ((next-cur) > 0)
     54    {
     55      copy = (char*)malloc((next-cur+1)*sizeof(char));
     56      if(copy == NULL)
     57        bailOut(2, "ERROR: Memory allocation problem.\n");
     58         
     59      memcpy(copy, cur, next-cur);
     60      copy[next-cur] = '\0';
     61      void_stack_push(rev_ret, copy);
     62    }
     63    cur = next+1;
    4864  }
    4965  if(strlen(cur) > 0)
    50     void_stack_push(ret_val, cur);
     66  {
     67    copy = strdup(cur);
     68    void_stack_push(rev_ret, copy);
     69  }
     70
     71  ret_val = void_stack_copy_reverse(rev_ret);
     72  void_stack_destroy(rev_ret);
    5173
    5274  return ret_val;
     
    284306  if(argc < 2)
    285307  {
    286     printf("ERROR: Requires 1 argument.\n");
    287308    usage();
    288     exit(1);
     309    bailOut(1, "ERROR: Requires 1 argument.\n");
    289310  }
    290311 
     
    295316      if(++argi > argc)
    296317      {
    297         fprintf(stderr, "ERROR: '-f' option requires parameter.\n");
    298318        usage();
    299         exit(1);
     319        bailOut(1, "ERROR: '-f' option requires parameter.\n");
    300320      }
    301321      if((prefix_filter = strdup(argv[argi])) == NULL)
    302       {
    303         fprintf(stderr, "ERROR: Memory allocation problem.\n");
    304         exit(2);
    305       }
     322        bailOut(2, "ERROR: Memory allocation problem.\n");
     323
    306324      prefix_filter_enabled = true;
    307325    }
     
    310328      if(++argi > argc)
    311329      {
    312         fprintf(stderr, "ERROR: '-t' option requires parameter.\n");
    313330        usage();
    314         exit(1);
     331        bailOut(1, "ERROR: '-t' option requires parameter.\n");
    315332      }
    316333      if((prefix_filter = strdup(argv[argi])) == NULL)
    317       {
    318         fprintf(stderr, "ERROR: Memory allocation problem.\n");
    319         exit(2);
    320       }
     334        bailOut(2, "ERROR: Memory allocation problem.\n");
     335
    321336      type_filter_enabled = true;
    322337    }
     
    327342    else if (argv[argi][0] == '-')
    328343    {
     344      usage();
    329345      fprintf(stderr, "ERROR: Unrecognized option: %s\n", argv[argi]);
    330       usage();
    331       exit(1);
     346      bailOut(1, "");
    332347    }
    333348    else
    334349    {
    335350      if((registry_file = strdup(argv[argi])) == NULL)
    336       {
    337         fprintf(stderr, "ERROR: Memory allocation problem.\n");
    338         exit(2);
    339       }     
     351        bailOut(2, "ERROR: Memory allocation problem.\n");
    340352    }
    341353  }
     
    345357  {
    346358    fprintf(stderr, "ERROR: Couldn't open registry file: %s\n", registry_file);
    347     exit(1);
    348   }
     359    bailOut(3, "");
     360  }
     361
    349362  root = regfio_rootkey(f);
    350363  nk_stack = void_stack_new(1024);
     
    361374        fprintf(stderr, "WARNING: specified path not found.\n");
    362375      else if(retr_path_ret != 0)
    363         fprintf(stderr, "ERROR:\n");
     376        bailOut(4, "ERROR:\n");
    364377    }
    365378  }
    366379  else
    367   {
    368     fprintf(stderr, "ERROR: Memory allocation problem.\n");
    369     exit(2);
    370   }
    371   void_stack_destroy(nk_stack);
    372 
     380    bailOut(2, "ERROR: Memory allocation problem.\n");
     381
     382  void_stack_destroy_deep(nk_stack);
    373383  regfio_close(f);
    374384
Note: See TracChangeset for help on using the changeset viewer.