Example #1
0
File: mark.c Project: adsr/mlbuf
// Replace data between marks
int mark_replace_between_mark(mark_t* self, mark_t* other, char* data, bint_t data_len) {
    bint_t offset_a;
    bint_t offset_b;
    buffer_get_offset(self->bline->buffer, self->bline, self->col, &offset_a);
    buffer_get_offset(other->bline->buffer, other->bline, other->col, &offset_b);
    if (offset_a < offset_b) {
        return buffer_replace(self->bline->buffer, offset_a, offset_b - offset_a, data, data_len);
    }
    return buffer_replace(self->bline->buffer, offset_b, offset_a - offset_b, data, data_len);
}
Example #2
0
File: mark.c Project: adsr/mlbuf
// Delete data between self and other
int mark_delete_between_mark(mark_t* self, mark_t* other) {
    bint_t offset_a;
    bint_t offset_b;
    buffer_get_offset(self->bline->buffer, self->bline, self->col, &offset_a);
    buffer_get_offset(other->bline->buffer, other->bline, other->col, &offset_b);
    if (offset_a == offset_b) {
        return MLBUF_OK;
    } else if (offset_a > offset_b) {
        return buffer_delete(self->bline->buffer, offset_b, offset_a - offset_b);
    }
    return buffer_delete(self->bline->buffer, offset_a, offset_b - offset_a);
}
Example #3
0
static void subst_list_eval_funcs____(const subst_list_type * subst_list , const parser_type * parser , buffer_type * buffer) {
  {
    int index;
    for (index = 0; index < vector_get_size( subst_list->func_data); index++) {
      const subst_list_func_type * subst_func = vector_iget_const( subst_list->func_data , index );
      const char                 * func_name  = subst_func->name;
      
      bool match;
      buffer_rewind( buffer );
      do {
        size_t match_pos;
        match     = buffer_strstr( buffer , func_name );
        match_pos = buffer_get_offset( buffer );
        
        if (match) {
          bool   update     = false;
          char * arg_start  = buffer_get_data( buffer );
          arg_start        += buffer_get_offset( buffer ) + strlen( func_name );
          
          if (arg_start[0] == '(') {  /* We require that an opening paren follows immediately behind the function name. */
            char * arg_end = strchr( arg_start , ')');
            if (arg_end != NULL) {
              /* OK - we found an enclosing () pair. */
              char            * arg_content = util_alloc_substring_copy( arg_start, 1 , arg_end - arg_start - 1);
              stringlist_type * arg_list    = parser_tokenize_buffer( parser , arg_content , true);
              char            * func_eval   = subst_list_func_eval( subst_func , arg_list );
              int               old_len     = strlen(func_name) + strlen( arg_content) + 2;       
              
              if (func_eval != NULL) {
                buffer_memshift( buffer , match_pos + old_len , strlen( func_eval ) - old_len);
                buffer_fwrite( buffer , func_eval , strlen( func_eval ) , sizeof * func_eval );
                free( func_eval );
                update = true;
              }
              
              free( arg_content );
              stringlist_free( arg_list );
            } 
          } 
          
          if (!update) 
            buffer_fseek( buffer , match_pos + strlen( func_name ) , SEEK_SET);
        }
      } while (match);
    }
  }
  if (subst_list->parent != NULL) 
    subst_list_eval_funcs____( subst_list->parent , parser , buffer );
}
char * util_alloc_envvar( const char * value ) {
  if (value == NULL)
    return NULL;
  else {
    buffer_type * buffer = buffer_alloc( 1024 );               /* Start by filling up a buffer instance with 
                                                                  the current content of @value. */
    buffer_fwrite_char_ptr( buffer , value );
    buffer_fwrite_char( buffer , '\0' );
    buffer_rewind( buffer );
    
    
    while (true) {
      if (buffer_strchr( buffer , '$')) {
        const char * data = buffer_get_data( buffer );
        int offset        = buffer_get_offset( buffer ) + 1;    /* Points at the first character following the '$' */
        int var_length = 0;
        
        /* Find the length of the variable name */
        while (true) {
          char c;
          c = data[offset + var_length];
          if (!(isalnum( c ) || c == '_'))      /* Any character which is NOT in the set [a-Z,0-9_] marks the end of the variable. */
            break;             
          
          if (c == '\0')                        /* The end of the string. */
            break;
          
          var_length += 1;
        }

        {
          char * var_name        = util_alloc_substring_copy( data , offset - 1 , var_length + 1);  /* Include the leading $ */
          const char * var_value = getenv( &var_name[1] );
          
          if (var_value != NULL)
            buffer_search_replace( buffer , var_name , var_value);                                      /* The actual string replacement. */
          else  
            buffer_fseek( buffer , var_length , SEEK_CUR );                                      /* The variable is not defined, and we leave the $name. */
          
          free( var_name );
        }
      } else break;  /* No more $ to replace */
    }
    
    
    buffer_shrink_to_fit( buffer );
    {
      char * expanded_value = buffer_get_data( buffer );
      buffer_free_container( buffer );
      return expanded_value;
    }
  }
}
Example #5
0
bool buffer_search_replace( buffer_type * buffer , const char * old_string , const char * new_string) {
  const int shift = strlen( new_string ) - strlen( old_string );
  bool match = buffer_strstr( buffer , old_string ); 
  if (match) {
    size_t offset = buffer_get_offset( buffer ) + strlen( old_string );
    if (shift != 0)
      buffer_memshift( buffer , offset , shift );
    
    /** Search continues at the end of the newly inserted string - i.e. no room for recursions. */
    buffer_fwrite( buffer , new_string , strlen( new_string ) , sizeof * new_string );
  }
  return match;
}
Example #6
0
bool buffer_search_replace( buffer_type * buffer , const char * old_string , const char * new_string) {
  bool match = buffer_strstr( buffer , old_string );
  if (match) {
    size_t offset = buffer_get_offset( buffer ) + strlen( old_string );
    const int shift = strlen( new_string ) - strlen( old_string );
    if (shift != 0)
      buffer_memshift( buffer , offset , shift );

    buffer_fwrite( buffer , new_string , 1 , strlen(new_string));
    buffer_terminate_char_ptr( buffer );
  }
  return match;
}
Example #7
0
File: mark.c Project: adsr/mlbuf
// Move mark by a character delta
int mark_move_by(mark_t* self, bint_t char_delta) {
    bint_t offset;
    buffer_get_offset(self->bline->buffer, self->bline, self->col, &offset);
    return mark_move_offset(self, offset + char_delta);
}
Example #8
0
File: mark.c Project: adsr/mlbuf
// Get mark offset
int mark_get_offset(mark_t* self, bint_t* ret_offset) {
    return buffer_get_offset(self->bline->buffer, self->bline, self->col, ret_offset);
}