Skip to content

Instantly share code, notes, and snippets.

@moebiuseye
Created December 25, 2012 14:21
Show Gist options
  • Save moebiuseye/4373447 to your computer and use it in GitHub Desktop.
Save moebiuseye/4373447 to your computer and use it in GitHub Desktop.
The output (static char *substitution) of function substitute() prematurely ends when the input (static char *selection) has a comma in it. Oo
/*
* substitution.h
*
* Copyright 2012 Samir Chaouki <moebius.eye@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <search.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
GeanyPlugin *geany_plugin;
GeanyData *geany_data;
GeanyFunctions *geany_functions;
#define NELEMS(x) (sizeof(x) / sizeof(x[0]))
static gchar *selection ;
static gchar *substitution ;
/* HTML_ENTITIES { { const char* to, const char* from },
* ... }
* */
struct corres {
char *to;
char *from;
};
static struct corres HTML_ENTITIES[] = {
{ "%%", "%" },
{ "&AElig;", "Æ" },
/* There's more, but let's not have a 10000 lines of code file */
};
struct rules {
int id;
int pos;
int len;
};
static struct rules *sub_rules;
/*
*
* name: generate_substitution_rules
* @param struct *corres corr, int n_corr, string haysack
* @return void
*/
void generate_substitution_rules ( struct corres *corr , int n_corr, char *haysack ) {
int i = 0, j = 0;
int rule_n = 0;
int match = TRUE;
sub_rules = (struct rules*)malloc(strlen(haysack) * sizeof(struct rules));
sub_rules[0].id = -1;
sub_rules[0].pos = -1;
g_return_if_fail( strlen(haysack) > 0 );
for (i = 0; i < strlen(haysack) ; i++){
/* for each position in haysack */
g_return_if_fail( n_corr > 0 );
for (j = 0; j < n_corr ; j++){
/* for each corr entry */
match = strncmp( corr[j].from, strndup( haysack+i, strlen(corr[j].from) ) , strlen(corr[j].from) );
if ( match == 0 ) {
sub_rules[rule_n].id = j;
sub_rules[rule_n].pos = i;
sub_rules[rule_n].len = strlen(corr[j].from);
rule_n += 1;
sub_rules[rule_n].id = -1;
sub_rules[rule_n].pos = -1;
sub_rules[rule_n].len = -1;
}
}
}
}
void datatest ( struct corres *corr , int n_corr, char *haysack ) {
msgwin_status_add(_( "datatest" ) );
int i = 0;
for (i = 0; sub_rules[i].id != -1 ; i++){
msgwin_status_add(_("id: %d"), sub_rules[i].id );
msgwin_status_add(_("pos: %d"), sub_rules[i].pos );
msgwin_status_add(_("len: %d"), sub_rules[i].len );
msgwin_status_add(_("[%d,%d]"),
sub_rules[i-1].pos+sub_rules[i-1].len,
sub_rules[i].pos-sub_rules[i].len );
msgwin_status_add(_("%s -> %s"), corr[sub_rules[i].id].from, corr[sub_rules[i].id].to );
msgwin_status_add(_("________"));
}
}
/*
*
* name: substitute
* @param struct corres *corr, int n_corr
* @return void
*/
void substitute( struct corres *corr , int n_corr ) {
int i=0;
unsigned int to_allocate = 0;
int gap_start = 0, gap_len = 0;
char *gap = malloc( CHAR_BIT/8 * strlen(selection) );
to_allocate += CHAR_BIT/8 * strlen(selection) +1 ;
while( sub_rules[i].id != -1 ){
to_allocate += sizeof( corr[sub_rules[i].id].to ) +1 ;
//~ to_allocate -= sizeof( corr[sub_rules[i].id].from );
i++;
}
msgwin_status_add(_("to_allocate: %d"), to_allocate );
substitution = malloc( to_allocate );
strcpy(substitution, "");
i=0;
while ( sub_rules[i].id != -1 ){
strcpy( gap, "");
if ( i==0 ){
gap = strndup( selection, sub_rules[i].pos);
} else {
gap_start = sub_rules[i-1].pos+sub_rules[i-1].len;
gap_len = sub_rules[i].pos-gap_start;
gap = strndup(
selection+gap_start ,
gap_len );
}
strcat( substitution, gap );
strcat( substitution, corr[sub_rules[i].id].to );
msgwin_status_add(_("gap: %s"), gap );
msgwin_status_add(_("substitution: %s"), substitution );
i++;
}
gap_start = sub_rules[i].pos+sub_rules[i].len;
gap = strdup( selection+gap_start );
strcat( substitution, gap );
}
/*
*
* name: substitute_with_list
* @param struct corres *with , int nelem_with
* @return int
*/
int substitute_with_list ( struct corres *with , int nelem_with ) {
int size;
size = NELEMS( sub_rules );
//~ datatest( with, selection );
/* BEGIN SUBSTITUTION RULES GENERATION */
generate_substitution_rules ( with, nelem_with, selection );
/* If no substitution rules were generated, don't bother.
* Nicely say goodbye and return EXIT_SUCCESS */
g_return_val_if_fail ( NELEMS( sub_rules ) == size, EXIT_SUCCESS );
datatest( with, nelem_with, selection);
/* BEGIN SUBSTITUTION */
substitute( with, nelem_with );
/* Now, for the actual substitution in the text buffer */
msgwin_status_add(_( "EXIT_SUCCESS" ) );
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment