bool FeatureIndex::buildFeatures(TaggerImpl *tagger) { string_buffer os; std::vector <int> feature; tagger->set_feature_id(feature_cache_.size()); for (size_t cur = 0; cur < tagger->size(); ++cur) { for (std::vector<char *>::const_iterator it = unigram_templs_.begin(); it != unigram_templs_.end(); ++it) { CHECK_FALSE(apply_rule(&os, *it, cur, *tagger)) << " format error: " << *it; ADD; } feature_cache_.add(feature); feature.clear(); } for (size_t cur = 1; cur < tagger->size(); ++cur) { for (std::vector<char *>::const_iterator it = bigram_templs_.begin(); it != bigram_templs_.end(); ++it) { CHECK_FALSE(apply_rule(&os, *it, cur, *tagger)) << "format error: " << *it; ADD; } feature_cache_.add(feature); feature.clear(); } return true; }
int add_rule (char *rule_buf, int rule_len, rules_t *rules) { if (tfind (rule_buf, &root, compare_string) != NULL) return (-3); char in[BLOCK_SIZE]; char out[BLOCK_SIZE]; memset (in, 0, BLOCK_SIZE); memset (out, 0, BLOCK_SIZE); int result = apply_rule (rule_buf, rule_len, in, 1, out); if (result == -1) return (-1); char *next_rule = mystrdup (rule_buf); tsearch (next_rule, &root, compare_string); incr_rules_buf (rules); rules->rules_buf[rules->rules_cnt] = next_rule; rules->rules_len[rules->rules_cnt] = rule_len; rules->rules_cnt++; return (0); }
int main(int argc, char **argv) { if(argc < 2) { fprintf(stderr, "Usage: automata outfile\n"); return EXIT_SUCCESS; } struct cells_t *cells = cells_new(CELLS_WIDTH, OFF); cells_set_state(cells, CELLS_WIDTH / 2, ON); FILE *fptr = fopen(argv[1], "w"); cells_write_pbm_header(cells, fptr, CELLS_WIDTH, GENERATIONS); cell_state rule_30[] = { OFF, OFF, OFF, ON, ON, ON, ON, OFF }; struct cells_t *new_cells; for(int gen = 0; gen < GENERATIONS; gen++) { new_cells = apply_rule(rule_30, cells); cells_free(cells); cells = new_cells; cells_write_pbm_generation(cells, fptr); } cells_free(new_cells); fclose(fptr); return EXIT_SUCCESS; }
static gboolean match_message_full (C2Message *message, GSList *list) { GSList *l; gboolean retval = TRUE; for (l = list; l; l = g_slist_next (l)) { C2FilterRule *rule = ((C2FilterRule*)l->data); retval = apply_rule (message, rule); } return retval; }
static const response* validate_sender(str* sender, str* params) { struct rule* rule; const response* r; if (!loaded) return 0; copy_addr(sender, &saved_sender, &sender_domain); for (rule = sender_rules; rule != 0; rule = rule->next) if (matches(&rule->sender, &saved_sender, &sender_domain)) { r = apply_rule(rule); if (rule->code != 'n') return r; } return 0; (void)params; }
static int apply_file(const char *path, bool ignore_enoent) { FILE *f; int r = 0; assert(path); if (!(f = fopen(path, "re"))) { if (ignore_enoent && errno == ENOENT) return 0; log_error("Failed to open file '%s', ignoring: %m", path); return -errno; } log_debug("apply: %s\n", path); while (!feof(f)) { char l[LINE_MAX], *p; int k; if (!fgets(l, sizeof(l), f)) { if (feof(f)) break; log_error("Failed to read file '%s', ignoring: %m", path); r = -errno; goto finish; } p = strstrip(l); if (!*p) continue; if (strchr(COMMENTS, *p)) continue; if ((k = apply_rule(p)) < 0 && r == 0) r = k; } finish: fclose(f); return r; }
static const response* validate_recipient(str* recipient, str* params) { struct rule* rule; const response* r; if (!loaded) return 0; copy_addr(recipient, &laddr, &rdomain); for (rule = recip_rules; rule != 0; rule = rule->next) if (matches(&rule->sender, &saved_sender, &sender_domain) && matches(&rule->recipient, &laddr, &rdomain)) { str_cat(recipient, &rule->relayclient); r = apply_rule(rule); if (rule->code != 'n') return r; } return 0; (void)params; }
static PyObject * graph_apply(graphobject *self, PyObject *args) { PyObject *ruleobj; if (!PyArg_ParseTuple(args, "O", &ruleobj)) return NULL; if (!is_ruleobject(ruleobj)) { PyErr_SetString(PyExc_TypeError, "argument must be a rule"); } apply_rule(self->triples, &(self->pointer), self->size, ((ruleobject *)ruleobj)->rule); Py_INCREF(Py_None); return Py_None; return NULL; }
static int apply_file(const char *path, bool ignore_enoent) { _cleanup_fclose_ FILE *f = NULL; int r; assert(path); r = search_and_fopen_nulstr(path, "re", NULL, conf_file_dirs, &f); if (r < 0) { if (ignore_enoent && r == -ENOENT) return 0; log_error("Failed to open file '%s', ignoring: %s", path, strerror(-r)); return r; } log_debug("apply: %s", path); for (;;) { char l[LINE_MAX], *p; int k; if (!fgets(l, sizeof(l), f)) { if (feof(f)) break; log_error("Failed to read file '%s', ignoring: %m", path); return -errno; } p = strstrip(l); if (!*p) continue; if (strchr(COMMENTS "\n", *p)) continue; k = apply_rule(p); if (k < 0 && r == 0) r = k; } return r; }
static int apply_file(const char *path, bool ignore_enoent) { _cleanup_fclose_ FILE *f = NULL; int r; assert(path); r = search_and_fopen(path, "re", NULL, (const char**) CONF_PATHS_STRV("binfmt.d"), &f); if (r < 0) { if (ignore_enoent && r == -ENOENT) return 0; return log_error_errno(r, "Failed to open file '%s', ignoring: %m", path); } log_debug("apply: %s", path); for (;;) { char l[LINE_MAX], *p; int k; if (!fgets(l, sizeof(l), f)) { if (feof(f)) break; return log_error_errno(errno, "Failed to read file '%s', ignoring: %m", path); } p = strstrip(l); if (!*p) continue; if (strchr(COMMENTS "\n", *p)) continue; k = apply_rule(p); if (k < 0 && r == 0) r = k; } return r; }
static pa_hook_result_t process(struct userdata *u, pa_proplist *p) { struct rule *r; time_t now; const char *pn; pa_assert(u); pa_assert(p); if (!(pn = pa_proplist_gets(p, PA_PROP_APPLICATION_PROCESS_BINARY))) return PA_HOOK_OK; if (*pn == '.' || strchr(pn, '/')) return PA_HOOK_OK; time(&now); pa_log_debug("Looking for .desktop file for %s", pn); if ((r = pa_hashmap_get(u->cache, pn))) { if (now-r->timestamp > STAT_INTERVAL) { r->timestamp = now; update_rule(r); } } else { make_room(u->cache); r = pa_xnew0(struct rule, 1); r->process_name = pa_xstrdup(pn); r->timestamp = now; pa_hashmap_put(u->cache, r->process_name, r); update_rule(r); } apply_rule(r, p); return PA_HOOK_OK; }
/************************************************************** * NAME * applyrules * ARGS * rules - list of rules to apply * dicent - entry to apply rules to * DESC * Apply rules to dic entry to add allomorphs. * RETN * Pointer to modified entry. * * Major mods 5/95 0.2r BJY moved code into new func apply_rule() */ char *applyrules( Rule *rules, char *dicent ) { Allo *al; /* temporary allo pointer */ Rule *ru; /* Temp rule */ char *s, *t; /* Temp string pointer */ int i; /* Index for cat array */ char *base; /* Base form */ char remchar; /* Removed char */ char *alloname; if ( !rules ) /* If no rules, return */ return( dicent ); num_apps = 0; /* Clear number of rules applied */ allolist = NULL; /* Clear list of allos already in entry */ newallolist = NULL; /* 0.2r BJY */ s = strnlstr( dicent, basemark ); /* Find base */ /* 0.2l BJY */ if ( !s ) /* If no base, return */ return( dicent ); s += basemarklen; /* Move to start of base */ /* Find end of base */ for ( t = s; /* Stop at space or cutoff char */ *t && !myisspace( *t ) && !( *t == cut_char ); t++ ); remchar = *t; /* Remember char after base */ *t = '\0'; /* Terminate base */ base = mystrdup( s ); /* Copy base */ *t = remchar; /* Restore char after base */ s = strnlstr( dicent, catmark ); /* Find category line */ /* 0.2l BJY */ if ( !s ) /* If no cat, use null */ cat[0] = NULL; else { for ( t = s; *t && !(*t == comment_char) && !(*t == '\n'); t++ ) /* Find comment or end of line */ ; remchar = *t; /* Remember char */ *t = '\0'; /* Cut off commment, if any */ s = mystrdup( s ); /* Copy cat line */ *t = remchar; /* Restore char after cats */ i = 0; /* Init index */ while ( *(s = nextwd( s ) ) ) /* While another cat */ cat[i++] = s; /* Remember it */ cat[i] = NULL; /* Terminate list with null */ } /* Make a list of allos already in entry */ allolist = NULL; /* Clear list of allos */ s = dicent - 1; while ( (s = strnlstr( s + 1, allomark )) != NULL ) /* While another allo */ /* 0.2l BJY */ { s += allomarklen; /* Move to start of allo */ s = skipwhite(s); /* wm: 0.2k: skip white space */ for ( t = s; *t && !myisspace( *t ); t++ ) /* Find end of allo */ ; remchar = *t; /* Remember char after allo */ *t = '\0'; /* Terminate allo */ add_allo( s ); /* Add allo to list */ *t = remchar; /* Restore char after allo */ } for ( ru = rules; ru; ru = ru->next ) /* For each rule */ { /* begin 0.2r BJY */ if ( *ru->match || *ru->repl ) /* don't apply 0 -> 0 rule to new allos */ { for ( al = newallolist; al; al = al->next ) { /* now try on each PHONRULE generated allo */ /* for ( s = al->name; s && !myisspace( *s ); s++)*/ /* find end of name */ /* ;*/ s = strchr( al->name, ' ' ); if ( s ) *s = '\0'; alloname = mystrdup( al->name ); if ( s ) *s = ' '; /* restore space */ apply_rule( alloname, ru, dicent, al ); } } base = apply_rule( base, ru, dicent, NULL ); /* try rule on base word */ /* set base in case changed by base rule 0.2s BJY */ } /* end 0.2r BJY */ if ( base_becomes_allo /* If allo is base */ && !in_allolist( base ) ) /* And not already in */ insert_line( allomark, base, after_allos( dicent ) ); /* Insert it into the entry */ num_ents++; /* Count sentence */ if ( do_monitor ) /* If monitor, give dot */ { if (num_apps == 0) /* hab 0.2p Conformed to standard practice */ putc('.', stderr); else if (num_apps < 10) putc('0'+num_apps, stderr); else putc('>', stderr); if ( num_ents % 100 == 0 ) /* wm 0.2k: number by 100s */ fprintf( stderr, " %5d", num_ents ); if ( num_ents % 10 == 0 ) fprintf( stderr, " " ); if ( num_ents % 50 == 0 ) fprintf( stderr, "\n" ); } return( dicent ); /* Return modified sent */ } /* function applyrules */
//search (6) or (26) connected neighborhood for similiar voxel as starting point void Floodfill::search_neighborhood(int x, int y, int z, vector<Point3D>* stack, int rule, int connectivity, int visitmode) { //resolution of steps int steps = 1; //setup our searh indices int indexNorth = y + steps; int indexSouth = y - steps; int indexEast = x + steps; int indexWest = x - steps; int indexUp = z + steps; int indexDown = z - steps; //helper Point3D temp; // look around for connected voxels marked as data (ie: equal to -1) // 6 CONNECTED //----------------- //EAST if(indexEast < width ) //if we aint out of bounds { //if the voxel has not been visited if(isVisited(indexEast, y, z, visitmode)==false) { if(apply_rule(x,y,z, indexEast,y,z, rule)) { //then voxel is in the same region temp.x = indexEast; temp.y = y; temp.z = z; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info markVisited(indexEast, y, z, visitmode); } } } //WEST if(indexWest >= 0 ) //if we aint out of bounds { //if the voxel has not been visited if(isVisited(indexWest, y, z, visitmode)==false) { if( apply_rule(x,y,z, indexWest,y,z, rule)) { //then voxel is in the same region temp.x = indexWest; temp.y = y; temp.z = z; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info markVisited(indexWest, y, z, visitmode); } } } //NORTH if(indexNorth < height ) //if we aint out of bounds { //if the voxel has not been visited if(isVisited(x, indexNorth, z, visitmode)==false) { if(apply_rule(x,y,z, x,indexNorth,z, rule)) { //then voxel is in the same region temp.x = x; temp.y = indexNorth; temp.z = z; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info markVisited(x, indexNorth, z, visitmode); } } } //SOUTH if(indexSouth >= 0) //if we aint out of bounds { //if the voxel has not been visited if(isVisited(x, indexSouth, z, visitmode)==false) { if(apply_rule(x,y,z, x,indexSouth,z, rule)) { //then voxel is in the same region temp.x = x; temp.y = indexSouth; temp.z = z; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info markVisited(x, indexSouth, z, visitmode); } } } //UP if(indexUp < depth ) //if we aint out of bounds { //if the voxel has not been visited if(isVisited(x, y, indexUp, visitmode)==false) { if(apply_rule(x,y,z, x,y,indexUp, rule)) { //then voxel is in the same region temp.x = x; temp.y = y; temp.z = indexUp; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info markVisited(x, y, indexUp, visitmode); } } } //DOWN if(indexDown >= 0) //if we aint out of bounds { //if the voxel has not been visited if(isVisited(x, y, indexDown, visitmode)==false) { if(apply_rule(x,y,z, x,y,indexDown, rule)) { //then voxel is in the same region temp.x = x; temp.y = y; temp.z = indexDown; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info markVisited(x, y, indexDown, visitmode); } } } /* if(connectivity==CONNECT26) //CONNECT26 { // 26 CONNECTED (20 additional cases) //---------------------------------- //NORTH WEST if(indexNorth < height && indexWest >= 0) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexWest][indexNorth][z]==false) if(apply_rule(x,y,z, indexWest,indexNorth,z, rule)) { //then voxel is in the same region temp.x = indexWest; temp.y = indexNorth; temp.z = z; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexWest][indexNorth][z] = true; } } //SOUTH WEST if(indexSouth >= 0 && indexWest >= 0) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexWest][indexSouth][z]==false) if(apply_rule(x,y,z, indexWest,indexSouth,z, rule)) { //then voxel is in the same region temp.x = indexWest; temp.y = indexSouth; temp.z = z; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexWest][indexSouth][z] = true; } } //NORTH EAST if(indexNorth < height && indexEast < width) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexEast][indexNorth][z]==false) if(apply_rule(x,y,z, indexEast,indexNorth,z, rule)) { //then voxel is in the same region temp.x = indexEast; temp.y = indexNorth; temp.z = z; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexEast][indexNorth][z] = true; } } //SOUTH EAST if(indexSouth >= 0 && indexEast < width) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexEast][indexSouth][z]==false) if(apply_rule(x,y,z, indexEast,indexSouth,z, rule)) { //then voxel is in the same region temp.x = indexEast; temp.y = indexSouth; temp.z = z; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexEast][indexSouth][z] = true; } } //------------------------------------------------------------------------- //UP NORTH if(indexUp < depth && indexNorth < height) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[x][indexNorth][indexUp]==false) if(apply_rule(x,y,z, x,indexNorth,indexUp, rule)) { //then voxel is in the same region temp.x = x; temp.y = indexNorth; temp.z = indexUp; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[x][indexNorth][indexUp] = true; } } //UP SOUTH if(indexUp < depth && indexSouth >= 0) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[x][indexSouth][indexUp]==false) if(apply_rule(x,y,z, x,indexSouth,indexUp, rule)) { //then voxel is in the same region temp.x = x; temp.y = indexSouth; temp.z = indexUp; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[x][indexSouth][indexUp] = true; } } //UP EAST if(indexUp < depth && indexEast < width) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexEast][y][indexUp]==false) if(apply_rule(x,y,z, indexEast,y,indexUp, rule)) { //then voxel is in the same region temp.x = indexEast; temp.y = y; temp.z = indexUp; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexEast][y][indexUp] = true; } } //UP WEST if(indexUp < depth && indexWest >= 0) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexWest][y][indexUp]==false) if(apply_rule(x,y,z, indexWest,y,indexUp, rule)) { //then voxel is in the same region temp.x = indexWest; temp.y = y; temp.z = indexUp; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexWest][y][indexUp] = true; } } //UP SOUTH WEST if(indexUp < depth && indexSouth >= 0 && indexWest >= 0) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexWest][indexSouth][indexUp]==false) if(apply_rule(x,y,z, indexWest,indexSouth,indexUp, rule)) { //then voxel is in the same region temp.x = indexWest; temp.y = indexSouth; temp.z = indexUp; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexWest][indexSouth][indexUp] = true; } } //UP SOUTH EAST if(indexUp < depth && indexSouth >= 0 && indexEast < width) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexEast][indexSouth][indexUp]==false) if(apply_rule(x,y,z, indexEast,indexSouth,indexUp, rule)) { //then voxel is in the same region temp.x = indexEast; temp.y = indexSouth; temp.z = indexUp; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexEast][indexSouth][indexUp] = true; } } //UP NORTH EAST if(indexUp < depth && indexNorth < height && indexEast < width) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexEast][indexNorth][indexUp]==false) if(apply_rule(x,y,z, indexEast,indexNorth,indexUp, rule)) { //then voxel is in the same region temp.x = indexEast; temp.y = indexNorth; temp.z = indexUp; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexEast][indexNorth][indexUp] = true; } } //UP NORTH WEST if(indexUp < depth && indexNorth < height && indexWest >= 0) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexWest][indexNorth][indexUp]==false) if(apply_rule(x,y,z, indexWest,indexNorth,indexUp, rule)) { //then voxel is in the same region temp.x = indexWest; temp.y = indexNorth; temp.z = indexUp; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexWest][indexNorth][indexUp] = true; } } //------------------------------------------------------------------------- //DOWN NORTH if(indexDown >= 0 && indexNorth < height) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[x][indexNorth][indexDown]==false) if(apply_rule(x,y,z, x,indexNorth,indexDown, rule)) { //then voxel is in the same region temp.x = x; temp.y = indexNorth; temp.z = indexDown; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[x][indexNorth][indexDown] = true; } } //DOWN SOUTH if(indexDown >= 0 && indexSouth >= 0) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[x][indexSouth][indexDown]==false) if(apply_rule(x,y,z, x,indexSouth,indexDown, rule)) { //then voxel is in the same region temp.x = x; temp.y = indexSouth; temp.z = indexDown; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[x][indexSouth][indexDown] = true; } } //DOWN WEST if(indexDown >= 0 && indexWest >=0) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexWest][y][indexDown]==false) if(apply_rule(x,y,z, indexWest,y,indexDown, rule)) { //then voxel is in the same region temp.x = indexWest; temp.y = y; temp.z = indexDown; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexWest][y][indexDown] = true; } } //DOWN EAST if(indexDown >= 0 && indexEast < width) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexEast][y][indexDown]==false) if(apply_rule(x,y,z, indexEast,y,indexDown, rule)) { //then voxel is in the same region temp.x = indexEast; temp.y = y; temp.z = indexDown; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexEast][y][indexDown] = true; } } //DOWN SOUTH WEST if(indexDown >= 0 && indexSouth >= 0 && indexWest >=0) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexWest][indexSouth][indexDown]==false) if(apply_rule(x,y,z, indexWest,indexSouth,indexDown, rule)) { //then voxel is in the same region temp.x = indexWest; temp.y = indexSouth; temp.z = indexDown; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexWest][indexSouth][indexDown] = true; } } //DOWN SOUTH EAST if(indexDown >= 0 && indexSouth >= 0 && indexEast < width) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexEast][indexSouth][indexDown]==false) if(apply_rule(x,y,z, indexEast,indexSouth,indexDown, rule)) { //then voxel is in the same region temp.x = indexEast; temp.y = indexSouth; temp.z = indexDown; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexEast][indexSouth][indexDown] = true; } } //DOWN NORTH WEST if(indexDown >= 0 && indexNorth < height && indexWest >=0) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexWest][indexNorth][indexDown]==false) if(apply_rule(x,y,z, indexWest,indexNorth,indexDown, rule)) { //then voxel is in the same region temp.x = indexWest; temp.y = indexNorth; temp.z = indexDown; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexWest][indexNorth][indexDown] = true; } } //DOWN NORTH EAST if(indexDown >= 0 && indexNorth < height && indexEast < width) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexEast][indexNorth][indexDown]==false) if(apply_rule(x,y,z, indexEast,indexNorth,indexDown, rule)) { //then voxel is in the same region temp.x = indexEast; temp.y = indexNorth; temp.z = indexDown; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexEast][indexNorth][indexDown] = true; } } }*/ }
int main (int argc, char *argv[]) { if (argc != 2) { fprintf (stderr, "usage: %s rules-file\n", argv[0]); return (-1); } rules_t *rules = malloc (sizeof (rules_t)); memset (rules, 0, sizeof (rules_t)); char *file_rules; if ((file_rules = argv[1]) != NULL) { FILE *fp; if ((fp = fopen (file_rules, "rb")) != NULL) { char rule_buf[RP_RULE_BUFSIZ]; int rule_len; while ((rule_len = fgetl (fp, rule_buf)) != -1) { if (rule_len == 0) continue; if (rule_buf[0] == '#') continue; int rc = add_rule (rule_buf, rule_len, rules); if (rc == 0) { /* all ok */ } else if (rc == -1) { fprintf (stderr, "Skipping rule: %s (syntax error)\n", rule_buf); } else if (rc == -3) { fprintf (stderr, "Skipping rule: %s (duplicate rule)\n", rule_buf); } else if (rc == -4) { fprintf (stderr, "Skipping rule: %s (duplicate result)\n", rule_buf); } } fclose (fp); } else { fprintf (stderr, "%s: %s\n", file_rules, strerror (errno)); free (rules); return (-1); } } char word_buf[BLOCK_SIZE]; int word_len; while ((word_len = fgetl (stdin, word_buf)) != -1) { uint32_t rules_idx; for (rules_idx = 0; rules_idx < rules->rules_cnt; rules_idx++) { char next_buf[BLOCK_SIZE]; int next_len = apply_rule (rules->rules_buf[rules_idx], rules->rules_len[rules_idx], word_buf, word_len, next_buf); if (next_len < 0) continue; puts (next_buf); } } return 0; }
void terrain_builder::build_terrains() { log_scope("terrain_builder::build_terrains"); // Builds the terrain_by_type_ cache for(int x = -2; x <= map().w(); ++x) { for(int y = -2; y <= map().h(); ++y) { const map_location loc(x,y); const t_translation::t_terrain t = map().get_terrain(loc); terrain_by_type_[t].push_back(loc); } } int rule_index = 0; building_ruleset::const_iterator r; for(r = building_rules_.begin(); r != building_rules_.end(); ++r) { const building_rule& rule = r->second; // Find the constraint that contains the less terrain of all terrain rules. // We will keep a track of the matching terrains of this constraint // and later try to apply the rule only on them size_t min_size = INT_MAX; t_translation::t_list min_types; constraint_set::const_iterator min_constraint = rule.constraints.end(); for(constraint_set::const_iterator constraint = rule.constraints.begin(); constraint != rule.constraints.end(); ++constraint) { const t_translation::t_match& match = constraint->second.terrain_types_match; t_translation::t_list matching_types; size_t constraint_size = 0; for (terrain_by_type_map::iterator type_it = terrain_by_type_.begin(); type_it != terrain_by_type_.end(); ++type_it) { const t_translation::t_terrain t = type_it->first; if (terrain_matches(t, match)) { const size_t match_size = type_it->second.size(); constraint_size += match_size; if (constraint_size >= min_size) { break; // not a minimum, bail out } matching_types.push_back(t); } } if (constraint_size < min_size) { min_size = constraint_size; min_types = matching_types; min_constraint = constraint; if (min_size == 0) { // a constraint is never matched on this map // we break with a empty type list break; } } } //NOTE: if min_types is not empty, we have found a valid min_constraint; for(t_translation::t_list::const_iterator t = min_types.begin(); t != min_types.end(); ++t) { const std::vector<map_location>* locations = &terrain_by_type_[*t]; for(std::vector<map_location>::const_iterator itor = locations->begin(); itor != locations->end(); ++itor) { const map_location loc = itor->legacy_difference(min_constraint->second.loc); if(rule_matches(rule, loc, rule_index, min_constraint)) { apply_rule(rule, loc); } } } ++rule_index; } }