Exemplo n.º 1
0
Arquivo: stack.c Projeto: gtill/mlib
stk_t *stack_alloc(unsigned int count, void *(*stk_alloc)(unsigned int),
                   void (*stk_free)(stk_t *))
{
    stk_t *stk = NULL;

    if (stk_alloc) {
        stk = stk_alloc(sizeof(*stk) + count * sizeof(stk->item[0]));
    }

    if (stk) {
        stk->capacity = count;
        stk->noccupied = 0;
        stk->stk_alloc = stk_alloc;
        stk->stk_free = stk_free;
    }

    return stk;
}
Exemplo n.º 2
0
static Stack
stk_build_prepend(char *inputList, /* text description of desired stack */ 
                  int prepend  /* Prepend paths by default */ )
{
	Stack           stack;
	char            NextItem[MAX_ITEM_SIZE];
	stack = stk_alloc( STK_INIT_SIZE );
	
	if ( stack == NULL ) {
	  return( stack );
        }
	
	if (inputList == NULL) {
	  return (stack);
	}

        /* Get items from the input list, incrementing the string pointer */

	while ( stk_next_list_item( &inputList, NextItem, MAX_ITEM_SIZE-1 ) ) {
	  short retval;
	  retval = stk_append_item( stack, NextItem, prepend );
	  if (retval!=0) {
	    free(stack);
	    return(NULL);
	  }
        }
	if ( stack->size == 0 ) {
	/*
	 * if we have a null string, make it a one item null string stack
	 * this is to accomodate situations where a null string may be valid
	 * input as a single entry for a filename (i.e. enter null for a
	 * program-determined filename
	 */
	 stk_append_entry( stack, "" );
        }

	/* reset to first entry */
	stk_rewind(stack);

	return (stack);
}
Exemplo n.º 3
0
/*
 * Given an integer i and a file string containing a '#', creates a stack
 * of files from file string with the '#' replaced by 1,2,...,i padded by
 * leading zeroes. For example, (foo#.fits,11) would be expanded to
 * foo01.fits, foo02.fits,...,foo11.fits.  Omitting the '#' results in a
 * the stack foo.fits being returned. If i is outside the range
 * (1,STK_INIT_SIZE), i is reset to 1.
 */
Stack 
stk_expand_n(  char* in_string, long int_suf_num )
{
  Stack stack;                 /* stack to be created & returned */
  char *entry = NULL;          /* names of stack files generated */
  char *pound;                 /* location of '#' in in-string */
  char *prefix;                /* string for chars prior to '#' */ 
  char *suffix;                /* string for chars after '#' */ 
  char *ii_suf_num;            /* chars for each number in file name */
  long i_strlen = 0, ii = 0, jj = 0;
  int tot_num_of_digits = 0;   /* total number of digits in filename */
  int num_of_lead_zeros = 0;   /* number of leading padding zeros */ 
  int num_of_digits_tra = 0;   /* number of trailing digits */ 
  int prepend = 1;  
  stack = stk_alloc( int_suf_num );
  if ( stack == NULL ) {
    fprintf(stderr, "ERROR: not enough memory\n");
    return( stack );
  }
  if ( in_string == NULL ) {
     return ( stack );
  }
  
  /* verify that input integer is within bounds */
  if ( int_suf_num < 1 )  { 
    fprintf( stderr,"Number of input stack items reset to 1 \n");
    int_suf_num = 1;
  }  

  /* locate '#' in input string.  If NULL, return input string  */
  pound = strchr( in_string,'#');
  if ( NULL == pound ) { /* no '#' found , so return in_string as stack */
    fprintf( stderr," No # given, so setting stack to input string\n");
    stack = stk_build( in_string );
    return (stack);
  }

  /* get total number of digits in and length for stack file names */
  tot_num_of_digits = ((log10( (double) int_suf_num )) + 1 ); 
  i_strlen = strlen( in_string) + tot_num_of_digits;

  /* allocate memory for strings */
  prefix = (char*) malloc( (i_strlen + 1) * sizeof( char ));
  suffix = (char*) malloc( (i_strlen + 1) * sizeof( char ));
  ii_suf_num = (char*) malloc( (i_strlen + 1) * sizeof( char ));
  entry = (char*) malloc( (i_strlen + 1) * sizeof( char ));  
  if (( NULL==prefix)||( NULL==suffix)||( NULL == ii_suf_num ||( NULL == entry ))){
    fprintf(stderr, "ERROR: not enough memory\n");
    return (stack);
  }
  
  /* create prefix by copying up to '#' and null-terminating */
  strncpy( prefix, in_string, (size_t)(pound - in_string) );
  prefix[ (size_t)(pound - in_string) ] = '\0';

  /* create suffix by copying past '#' */
  strcpy( suffix, pound + 1 );
  
  /* create each entry and append to stack */
  for ( ii = 1 ; ii <= int_suf_num; ii++ ) {
    num_of_digits_tra = log10( (double) ii ) + 1;
    
    /* pad entry with appropriate number of leading 0's */
    num_of_lead_zeros = tot_num_of_digits - num_of_digits_tra;
    strcpy( entry, prefix );
    for ( jj = 0; jj < num_of_lead_zeros; jj++ ) { 
      strcat( entry, "0");
    }
    sprintf( ii_suf_num,"%ld", ii );
    strcat( entry, ii_suf_num );
    strcat( entry, suffix );
    stk_append_item( stack, entry, prepend ); 
  }
  
  if ( prefix)
    free( prefix );
  if ( suffix)
    free( suffix );
  if( ii_suf_num ) 
    free( ii_suf_num ) ;
  if( entry )
    free( entry );
  
  /* reset to first entry */
  stk_rewind(stack);
  
  return (stack);
}