コード例 #1
0
ファイル: geo.c プロジェクト: wikimedia/analytics-udp-filters
char *geo_lookup(GeoIP *gi, char *ipaddr, int bird) {
	/*
	 * Lookup the country_code by ip address, we can
	 * extend this in the future with more granular data
	 * such as region,city or even zipcode.
	 */
	static char area[MAX_BUF_LENGTH];

	// set the charset to UTF8
	GeoIP_set_charset(gi, GEOIP_CHARSET_UTF8);

	switch(bird){
		case COUNTRY: {
			const char *country= GeoIP_country_code_by_addr(gi, ipaddr);
			if (country==NULL){
				strncpy(area, unknown_geography, MAX_BUF_LENGTH);
			} else {
				strncpy(area, country, MAX_BUF_LENGTH);
			}

		}
		break;

		case REGION:{
			GeoIPRegion *gir;
			gir=GeoIP_region_by_addr(gi,ipaddr);
			if(gir == NULL || strlen(gir->region)==0){
				strncpy(area, unknown_geography, MAX_BUF_LENGTH);
			} else {
				strncpy(area, gir->region, MAX_BUF_LENGTH);
			}

			if(gir != NULL) {
				GeoIPRegion_delete(gir);
			}
			break;
		}

		case CITY:{
			GeoIPRecord *grecord;
			char *city;
			int mustFreeCity = 0;
			grecord = GeoIP_record_by_addr(gi, ipaddr);
			if (grecord !=NULL){
				if (grecord->city == NULL){
					strncpy(area, unknown_geography, MAX_BUF_LENGTH);
				} else {
					int len = strlen(grecord->city);
					city = strdup(grecord->city);
					mustFreeCity = 1;
					strncpy(area,city, MAX_BUF_LENGTH);
					replace_space_with_underscore(area, len);
				}
				if (mustFreeCity) {
					free(city);
				}
				GeoIPRecord_delete(grecord);
			} else {
				strncpy(area, unknown_geography, MAX_BUF_LENGTH);
			}
			break;
		}

		case LAT_LON: {
			GeoIPRecord *grecord;
			grecord = GeoIP_record_by_addr(gi, ipaddr);
			if (grecord!=NULL){
				snprintf(area, MAX_BUF_LENGTH, "%f,%f", grecord->latitude, grecord->longitude);
				GeoIPRecord_delete(grecord);
			} else {
				strncpy(area, unknown_geography, MAX_BUF_LENGTH);
			}
			break;
		}

		case EVERYTHING: {
			GeoIPRecord *grecord;
			char *country = unknown_geography, *region = unknown_geography, *city = unknown_geography;
			int mustFreeCity = 0;
			float lat = 0.0, lon = 0.0;
			grecord = GeoIP_record_by_addr(gi, ipaddr);
			if (grecord != NULL) {
				if (grecord->city != NULL) {
					city = strdup(grecord->city);
					mustFreeCity = 1;
				}
				replace_space_with_underscore(city, strlen(city));

				if (grecord->region != NULL) {
					region = grecord->region;
				}
				if (grecord->country_code != NULL) {
					country = grecord->country_code;
				}
				lat = grecord->latitude;
				lon = grecord->longitude;
			}
			snprintf(area, MAX_BUF_LENGTH, "%s|%s|%s|%f,%f", country, region, city, lat, lon);

			if (grecord != NULL) {
				GeoIPRecord_delete(grecord);
			}

			if (mustFreeCity) {
				free(city);
			}
			break;
		}

		default:
			break;
	}
	return area;
}
コード例 #2
0
int main (int argc, char ** argv)
{
	//*** 2. Define AC variables
	AC_AUTOMATA_t * acap;

	AC_TEXT_t input_text = {0, 0};

	#define PATTERN_NUMBER (sizeof(allpattern)/sizeof(AC_PATTERN_t))

	unsigned int i;

	/* Sending parameter to call-back function */
	struct sample_param my_param;
	my_param.anum = 1;
	my_param.achar = 'a'; /* 'a': find all, 'f': find first */

	//*** 3. Get a new automata
	acap = ac_automata_init (match_handler);

	//*** 4. add patterns to automata
	for (i=0; i<PATTERN_NUMBER; i++)
	{
		allpattern[i].length = strlen(allpattern[i].astring);
		ac_automata_add (acap, &allpattern[i]);
	}

	//*** 5. Finalize automata.
	ac_automata_finalize (acap);

	//*** 5.1 Display automata
	//ac_automata_display (acap, 's');

	/* This illustrates how to search big text chunk by chunk
	 * in this example input buffer size is 2500 and input file is pretty
	 * bigger than that. in fact it imitate reading from input file.
	 * in such situations searching must be done inside a loop. the loop
	 * continues until it consumes all input file.
	**/

	FILE *fp;
	char *line;
        size_t len=0;
	int k=0;
	char tolower_input_line[10000];

	fp = fopen(argv[1], "r");
        if(fp == NULL) printf("File Couldn't Open\n");

	input_line[0]='_'; tolower_input_line[0]='_';
	while(getline(&line, &len, fp)!=-1)
        {  
	   strcpy(input_line+1, line);
	 //  printf("%s", input_line);
	  
	   //calling function replace_space_with_underscore()
           replace_space_with_underscore(input_line);
	   *(input_line+len)='_';  

	   k=0;
	   strcpy(tolower_input_line+1, line); 

	   //tolower 
	   while(tolower_input_line[k])
           { 
	      *(tolower_input_line+k)= tolower(tolower_input_line[k]);
              k++;
           }

	   /*****appending original line and tolower of the same line
	         *(input_line+strcspn(input_line, "\n"))= '/';
                 strcat(input_line, tolower_input_line);            **********/
 
	   //calling function replace_space_with_underscore()
           replace_space_with_underscore(tolower_input_line);
	   *(tolower_input_line+len)='_';  

	   char * chunk_start = input_line;
           //	char * end_of_file = input_line+sizeof(input_line);
	   char * end_of_file = input_line+strlen(input_line);
	   input_text.astring = buffer;

	/* Search loop */
	 while (chunk_start<end_of_file) { 
		//*** 6. Set input text
		input_text.length = (chunk_start<end_of_file)?
				sizeof(buffer):(sizeof(input_line)%sizeof(buffer));
		strncpy(input_text.astring, chunk_start, input_text.length);
        
		//*** 7. Do search
		if(ac_automata_search (acap, &input_text, (void *)(&my_param)))
			break;
		/* according to the return value of ac_automata_search() we decide to
		 * continue or break loop. */

		chunk_start += sizeof(buffer); 
	 }
	    //*** 8. Reset
	 ac_automata_reset(acap);

	   //Checking multi words after tolowering the i/p. 
	   strcpy(input_line,tolower_input_line);
	   chunk_start = input_line;
           end_of_file = input_line+strlen(input_line);
           input_text.astring = buffer;

        /* Search loop */
           while (chunk_start<end_of_file) { 
                //*** 6. Set input text
                input_text.length = (chunk_start<end_of_file)?
                                sizeof(buffer):(sizeof(input_line)%sizeof(buffer));
                strncpy(input_text.astring, chunk_start, input_text.length);

                //*** 7. Do search
                if(ac_automata_search (acap, &input_text, (void *)(&my_param)))
                        break;
                /* according to the return value of ac_automata_search() we decide to
                 * continue or break loop. */

                chunk_start += sizeof(buffer);
            }  

	   ac_automata_reset(acap); 
	   printf(";~~~~~~~~~~\n");
	}
	//*** 9. Release automata
	ac_automata_release (acap);
	return 0;
}
コード例 #3
0
int main (int argc, char ** argv)
{
    unsigned int i;
    struct parameter my_param;
    // we use this struct to send/receive input/output parameters to/from automata
    my_param.position = 250;    // input: end position; change it to 1000 and see what happens
    my_param.match_count = 0;   // output:

    AC_TEXT_t input_text;
    AC_AUTOMATA_t * atm = ac_automata_init ();

    for (i=0; i<PATTERN_COUNT; i++)
    {
        AC_STATUS_t status;
        sample_patterns[i].length = strlen (sample_patterns[i].astring);
        status = ac_automata_add (atm, &sample_patterns[i]);
        switch (status)
        {
            case ACERR_DUPLICATE_PATTERN:
//                printf ("Add pattern failed: ACERR_DUPLICATE_PATTERN: %s\n", sample_patterns[i].astring);
                break;
            case ACERR_LONG_PATTERN:
                printf ("Add pattern failed: ACERR_LONG_PATTERN: %s\n", sample_patterns[i].astring);
                break;
            case ACERR_ZERO_PATTERN:
                printf ("Add pattern failed: ACERR_ZERO_PATTERN: %s\n", sample_patterns[i].astring);
                break;
            case ACERR_AUTOMATA_CLOSED:
                printf ("Add pattern failed: ACERR_AUTOMATA_CLOSED: %s\n", sample_patterns[i].astring);
                break;
            case ACERR_SUCCESS:
//                printf ("Pattern Added: %s\n", sample_patterns[i].astring);
                break;
        }
    }

    ac_automata_finalize (atm);
    
    // here we illustrates how to search a big text chunk by chunk.
    // in this example input buffer size is 64 and input file is pretty
    // bigger than that. we want to imitate reading from input file.
    // in such situations searching must be done inside a loop. the loop
    // continues until it consumed all input file.

//    printf ("Automata finalized.\n\nSearching...\n");
	//Added below code by Roja  
	FILE *fp;
	char *line;
	size_t len=0;

        fp = fopen(argv[1], "r");
	fp1 = fopen(argv[2], "w");
        if(fp == NULL  || fp1 == NULL) printf("File Couldn't Open\n");

        input_line[0]='_';
        while(getline(&line, &len, fp)!=-1)
        {
           strcpy(input_line+1, line);
         //  printf("%s", input_line);

           //calling function replace_space_with_underscore()
           replace_space_with_underscore(input_line);
           *(input_line+len)='_';
	//Code added by  Roja ended

	char * chunk_start = input_line;
	char * end_of_file = input_line + sizeof(input_line);
	input_text.astring = buffer;

	while (chunk_start<end_of_file)
	{
	        input_text.length = (chunk_start<end_of_file)?sizeof(buffer):(sizeof(input_line)%sizeof(buffer));
        	strncpy (buffer, chunk_start, input_text.length);

	        if (ac_automata_search (atm, &input_text, 0, match_handler, (void *)(&my_param)))
        	    // if the search stopped in the middle (returned 1) we should break the loop
            		break;

	        chunk_start += sizeof(buffer);
	}


    printf(";~~~~~~~~~~\n"); //Added by Roja
    fprintf(fp1, ";~~~~~~~~~~\n"); //Added by Roja
    }
    
//    printf ("found %d occurrence in the beginning %d bytes\n", my_param.match_count, my_param.position);

    // TODO: do the same search with settext/findnext interface
    
    ac_automata_release (atm);

    return 0;
}
コード例 #4
0
ファイル: tests.cpp プロジェクト: sdukshis/swng
TEST(replace_space_with_underscore, test) {
    ASSERT_STREQ("a_b", replace_space_with_underscore("a b").c_str());
    ASSERT_STREQ("a_b_c", replace_space_with_underscore("a b c").c_str());
}