Changeset 38 for trunk


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.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/doc/Makefile

    r34 r38  
    11# $Id$
    22
    3 default:
     3BUILD_FILES=$(BUILD_DOC)/man/man1/reglookup.1.gz
     4
     5default: $(BUILD_FILES)
     6
     7$(BUILD_DOC)/man/man1/reglookup.1.gz: man/man1/reglookup.1.gz
    48        mkdir -p $(BUILD_DOC)/man/man1
    5         cp man/man1/reglookup.1.gz $(BUILD_DOC)/man/man1/reglookup.1.gz
     9        cp man/man1/reglookup.1.gz $@
    610
    711install:
  • trunk/include/void_stack.h

    r33 r38  
    3737
    3838void_stack* void_stack_new(unsigned short max_size);
     39void_stack* void_stack_copy(const void_stack* v);
     40void_stack* void_stack_copy_reverse(const void_stack* v);
    3941void void_stack_destroy(void_stack* stack);
     42void void_stack_destroy_deep(void_stack* stack);
    4043unsigned short void_stack_size(void_stack* stack);
    4144void* void_stack_pop(void_stack* stack);
  • trunk/lib/void_stack.c

    r33 r38  
    5050
    5151
     52void_stack* void_stack_copy(const void_stack* v)
     53{
     54  unsigned int i;
     55  void_stack* ret_val;
     56  if(v == NULL)
     57    return NULL;
     58
     59  ret_val = void_stack_new(v->max_size);
     60  if(ret_val == NULL)
     61    return NULL;
     62
     63  for(i = 0; i < v->top; i++)
     64    ret_val->elements[i] = v->elements[i];
     65  ret_val->top = v->top;
     66
     67  return ret_val;
     68}
     69
     70
     71void_stack* void_stack_copy_reverse(const void_stack* v)
     72{
     73  unsigned int i;
     74  void_stack* ret_val;
     75  if(v == NULL)
     76    return NULL;
     77
     78  ret_val = void_stack_new(v->max_size);
     79  if(ret_val == NULL)
     80    return NULL;
     81
     82  for(i = 0; i < v->top; i++)
     83    ret_val->elements[i] = v->elements[v->top-i-1];
     84  ret_val->top = v->top;
     85
     86  return ret_val;
     87}
     88
     89
    5290void void_stack_destroy(void_stack* stack)
     91{
     92  free(stack);
     93}
     94
     95
     96void void_stack_destroy_deep(void_stack* stack)
    5397{
    5498  unsigned short i;
  • 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.