Skip to content

Instantly share code, notes, and snippets.

@mad
Created April 30, 2011 15:41
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mad/949761 to your computer and use it in GitHub Desktop.
struct number_rule {
char *before_mask;
char *after_mask;
char am_len;
char bm_len;
int amp_len;
int bmp_len;
};
struct number_rules {
struct number_rule **nrules1;
struct number_rule **nrules2;
int n1;
int n2;
};
void TEST_number_correction()
{
char *result = (char*) malloc(100);
char **elems;
int i;
elems = (char **)malloc(10 * sizeof(char**));
for (i = 0; i < 10; i++) {
elems[i] = (char*) malloc(20);
}
#define NUMBER 1
strcpy(elems[NUMBER], "91234567891");
printf("Number: %s\n", elems[NUMBER]);
struct number_rules nrules;
// Create 10 rule
nrules.nrules1 = (struct number_rule **) malloc(10 * sizeof(struct number_rule **));
struct number_rule nrule = {
.after_mask = "8__________",
.before_mask = "9__________",
.am_len = 11,
.bm_len = 11,
.amp_len = 1,
.bmp_len = 1,
};
nrules.nrules1[0] = &nrule;
nrules.n1 = 1;
nrules.nrules2 = NULL;
nrules.n2 = 0;
memset(result, 0, 100);
number_correction(elems, 1, result, ';', &nrules);
printf("%s\n", result);
/* memset(result, 0, 100);
/\* Change *\/
bm = "9__________";
am = "8__________";
number_correction(number, bm, am, result);
printf("%s\n", result);
memset(result, 0, 100);
/\* Trim *\/
bm = "9__________";
am = "___";
number_correction(number, bm, am, result);
printf("%s\n", result);
memset(result, 0, 100);
/\* Change *\/
bm = "9__________";
am = "855________";
number_correction(number, bm, am, result);
printf("%s\n", result);
memset(result, 0, 100);
/\* Add *\/
bm = "91_________";
am = "222222________";
number_correction(number, bm, am, result);
printf("%s\n", result); */
}
/* struct number_rule *fill_nrule(char *after_mask, char *before_mask)
{
struct number_rule *nrule = (number_rule*) malloc(sizeof(struct number_rule));
char a_len, b_len;
a_len = strlen(a
nrule->after_mask = (char*) malloc(
} */
/*
* number = NNNNNNNNN
* a_mask = N________
* b_mask = N________
*/
inline void number_correction(char **elements, int idx, char *result, char delimiter, struct number_rules *nrules)
{
char n_len;
char *number = elements[idx];
struct number_rule *nrule;
int rule, found = 0;
n_len = strlen(number);
for (rule = 0; rule < nrules->n1; rule++) {
nrule = nrules->nrules1[0];
if (n_len != nrule->bm_len) {
continue;
}
if (strncmp(number, nrule->before_mask, nrule->bmp_len) == 0) {
/* OK. Pattern match */
strncat(result, nrule->after_mask, nrule->amp_len);
strncat(result, &number[nrule->bmp_len], nrule->am_len - nrule->amp_len);
found = 1;
break;
}
/* Try next rule */
}
/* TODO: Check for second rule */
if (!found) {
strncat(result, number, n_len);
}
result[strlen(result)] = delimiter;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment