Example #1
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 #2
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 #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 );
}