コード例 #1
0
/* the function inserts a string by hashing the string and then going into the 
   correct index in the array to insert the new string at the beginning. It 
   accepts two arguments: 1) a pointer to a hash table. 2) a 
   pointer to a char type string. */
int add_string(hash_table_t *hashtable, char *str)
{
    list_t *new_list;
    list_t *current_list;
    unsigned int hashval = hash(hashtable, str);

    // allocate memory for list
    if ((new_list = malloc(sizeof(list_t))) == NULL)
    {
        return 1;
    }

    // does the item already exist?
    current_list = lookup_string(hashtable, str);

    // if item does exist do not insert again
    if (current_list != NULL)
    {
        return 2;
    }
    
    // insert item into list
    strcpy(new_list->string, str);
    //new_list->string = strdup(str);
    new_list->next = hashtable->table[hashval];
    hashtable->table[hashval] = new_list;

    return 0;
}
コード例 #2
0
ファイル: greg.c プロジェクト: MosheBerman/hebcal
void setDate ( date_t *d )
{
/*
   asctime() converts a time value contained in a tm  structure
   to a 26-character string of the form:
   Sun Sep 16 01:03:52 1973\n\0
   Each field  has  a  constant  width.   asctime()  returns  a
   pointer to the string.
 */

/*    
FIX: removing these decls, but need to start doing compilation platofrm checks to ensure that these aren't necessary.
time_t time ();
    char *ctime( const time_t * );
*/  
    time_t secs = time (NULL);
    char *timestr = ctime (&secs);

/* portability has driven me to truly shameful code.  
   please forgive this.
 */
    sscanf (timestr + 20, "%d", &d->yy);
    d->mm = lookup_string( timestr + 4, eMonths, 13, 3 );
    sscanf (timestr + 8, "%d", &d->dd);
}
コード例 #3
0
ファイル: BKDRhash.c プロジェクト: TempoJiJi/BKDR-hash
int main()
{
    FILE *fp = fopen("name.txt","r");	
    char *str;
    hash_table_t *my_hash_table;
    int size_of_table = 12;	
    list_t *item;
    int count=0;    //check how many item does not match
    
    /* Create a new hash table */
    my_hash_table = create_hash_table(size_of_table);
    
    /* Add the string to hash table */
    while( (fscanf(fp,"%s",str )) !=EOF)
	add_string(my_hash_table,str);

    fclose(fp);

    fp = fopen("input.txt","r");    
    while( (fscanf(fp,"%s",str)) != EOF ){
	    item = lookup_string (my_hash_table , str);
	    if(item == NULL){
		    printf("Can't found %s \n",str);
		    count++;
	    }
	    else
		    printf("Found %s - %s\n",str , item->str);
    }

    printf("%d\n",count);
    fclose(fp);
    
    /* Delete the table */
    free_table(my_hash_table);
}
コード例 #4
0
ファイル: Resources.cpp プロジェクト: iam-peter/guacamole
  std::string lookup_shader(std::string const& file) {
    std::string source(lookup_string(file));

    resolve_includes(source);

    return source;
  }
コード例 #5
0
ファイル: Resources.cpp プロジェクト: iam-peter/guacamole
  std::string lookup_shader(std::vector<unsigned char> const& resource) {
    std::string source(lookup_string(resource));

    resolve_includes(source);

    return source;
  }
コード例 #6
0
ファイル: Resources.cpp プロジェクト: iam-peter/guacamole
  std::string lookup_string(std::string const& file) {
    auto it(data_.find(file));

    if (it == data_.end())
      Logger::LOG_ERROR << "Failed to get string resource: Entry \"" << file << "\" does not exist!" << std::endl;

    return lookup_string(*it->second);
  }
コード例 #7
0
void main()
{
        int i;
        int findCnt;
        list_t*  listShow= NULL;
        
        char *find[]={"hello","home"};
        
        int wordsToHash = 20;//Remember to define your size!!!(how many phrase  is s[] )!

        static char *s[]={"steve","bOB","apple","ban","Johnson", 
        "banana","ice","happy","home","hello","love","wen","danny"
        ,"dog","hot" ,"cold","fato","fatrabbit","jerry","tux"};
        
        //timer
        hash_table_t *my_hash_table;
        clock_t         start, stop;
        struct timespec go, end;
        double cpu_time1;


        int size_of_table = 12;
        clock_gettime(CLOCK_REALTIME, &go);
        start = clock();
        my_hash_table = create_hash_table(size_of_table);
        
        
       
        //hashing   Remember
        for( i=0; i<wordsToHash ;i++){
        add_string(my_hash_table,s[i]);
        }

        listShow = lookup_string(my_hash_table,find[1]);


        stop = clock();
        clock_gettime(CLOCK_REALTIME, &end);
        cpu_time1 = diff_in_second(go, end);
        float   elapsedTime = (float)(stop - start) /(float)CLOCKS_PER_SEC * 1000.0f;
        printf( "Time to hash:  %3.1f ms\n", elapsedTime );
        printf("execution time of cpu : %lf sec\n", cpu_time1);
        printf("Found the word list \n");
        printList(listShow);
        printf("Found the word is %s \n",listShow->str);
        printf("-------------------------- \n");  



        printf("Print hash table content \n");
        for(i=0; i<size_of_table ;i++)
        {
         printList(my_hash_table->table[i]);
        printf("\n");
        }

        free_table(my_hash_table);
}
コード例 #8
0
ファイル: ast.c プロジェクト: unixfreak0037/yara-dev
int new_string_identifier(int type, STRING* defined_strings, char* identifier, TERM_STRING** term)
{
    TERM_STRING* new_term = NULL;
    STRING* string;
    int result = ERROR_SUCCESS;
        
    if (strcmp(identifier, "$") != 0) /* non-anonymous strings */
    {
        string = lookup_string(defined_strings, identifier);
      
        if (string != NULL)
        {
    		/* the string has been used in an expression, mark it as referenced */
    		string->flags |= STRING_FLAGS_REFERENCED;  

			/* in these cases we can't not use the fast-matching mode */
			if (type == TERM_TYPE_STRING_COUNT ||
			    type == TERM_TYPE_STRING_AT ||
			    type == TERM_TYPE_STRING_IN_RANGE ||
			    type == TERM_TYPE_STRING_OFFSET)
			{
				string->flags &= ~STRING_FLAGS_FAST_MATCH;
			}
	
            new_term = (TERM_STRING*) yr_malloc(sizeof(TERM_STRING));

            if (new_term != NULL)
            {
                new_term->type = type;
                new_term->string = string;
                new_term->next = NULL;
            }
        }
        else
        {
            result = ERROR_UNDEFINED_STRING;
        }
    }
    else  /* anonymous strings */
    {
        new_term = (TERM_STRING*) yr_malloc(sizeof(TERM_STRING));

        if (new_term != NULL)
        {
            new_term->type = type;
            new_term->string = NULL;
            new_term->next = NULL;
        }      
    }
    
    *term = new_term;   
    return result;
}
コード例 #9
0
ファイル: flatdevtree.c プロジェクト: PennPanda/linux-repo
/* lookup string and insert if not found */
static int map_string(struct ft_cxt *cxt, const char *name)
{
	int off;
	char *p;

	off = lookup_string(cxt, name);
	if (off != NO_STRING)
		return off;
	p = cxt->rgn[FT_STRINGS].start;
	if (!ft_make_space(cxt, &p, FT_STRINGS, strlen(name) + 1))
		return NO_STRING;
	strcpy(p, name);
	return p - cxt->str_anchor;
}
コード例 #10
0
ファイル: dtdparser.c プロジェクト: nodayoshikazu/XML-Parser
char* lookupDTDPath (FILE* file)
{
  static char dtdpath[MAXPATHLEN];
  int ch, match = 0;
  int line = 0;

  lines = 0;			/* fixed bug 10/18/01! */
  if (lookup_string (file, DOCTYPE, DOCTYPE_LENGTH)) {
    if (5 < lines)
      return NULL;		/* not found */
    if (lookup_string (file, SYSTEM, SYSTEM_LENGTH)) {
      char* p = dtdpath;
      while ((ch = fgetc (file)) != '"')
	;
      while ((ch = fgetc (file)) != '"')
	*p++ = ch;
      *p = '\0';
    }
    convert_file_uri_to_system_path (dtdpath);
    return dtdpath;
  }
  return NULL;
}
コード例 #11
0
int ft_prop(struct ft_cxt *cxt, const char *name, const void *data,
		unsigned int sz)
{
	int off, len;

	off = lookup_string(cxt, name);
	if (off == NO_STRING)
		return -1;

	len = 12 + _ALIGN(sz, 4);
	if (!ft_make_space(cxt, &cxt->p, FT_STRUCT, len))
		return -1;

	ft_put_word(cxt, OF_DT_PROP);
	ft_put_word(cxt, sz);
	ft_put_word(cxt, off);
	ft_put_bin(cxt, data, sz);
	return 0;
}
コード例 #12
0
/*add string*/
int add_string(hash_table_t *hashtable, char *str)
{
    list_t *new_list;
    list_t *current_list;
    unsigned int hashval = hash(hashtable, str);

    /* Attempt to allocate memory for list */
    if ((new_list = malloc(sizeof(list_t))) == NULL) return 1;

    /* Does item already exist? */
    current_list = lookup_string(hashtable, str);
        /* item already exists, don't insert it again. */
    if (current_list != NULL) return 2;
    /* Insert into list */
    new_list->str = strdup(str);
    new_list->next = hashtable->table[hashval];
    hashtable->table[hashval] = new_list;

    return 0;
}
コード例 #13
0
 explicit symbol (const char * str):
     data(lookup_string(str))
 {}
コード例 #14
0
 symbol_data lookup_string(const char * str)
 {
     return lookup_string(str, strlen(str));
 }
コード例 #15
0
 explicit symbol (std::string const & str):
     data(lookup_string(str.c_str(), str.size()))
 {}
コード例 #16
0
ファイル: crawler.c プロジェクト: subasreev/cs537
void *parse (void * context)
{
  void (*_edge_fn) (char *from, char *to) = context;
  char *url, *page;
  node_t *entry;

  while (!areQueuesEmpty ())
    {
      //critical section: dequeue page from shared unbounded queue acting as consumer
      //printf("in parser\n");

      pthread_mutex_lock(&lock2);
      while (List_Empty(queue_pages))
	pthread_cond_wait (&fill_unbounded, &lock2);
      entry = List_Get (queue_pages);
      pthread_mutex_unlock(&lock2);

      url = entry->key;
      page = entry->val; 
      //printf("parsing %s\n",url);


      //core logic for parsing a page
      char *save_ptr1, *token;
      char *delim = " \n\t";
      regex_t regex;
      char *expr = "^link:.*";
      int reti = regcomp (&regex, expr, 0);
      if (reti)
	{
	  //printf (stderr, "Could not compile regex\n");
	  exit (1);
	}

      char *str1 = page;
      int has_atleast_a_link = 0;
      for (;; str1 = NULL)
	{
	  token = strtok_r (str1, delim, &save_ptr1);
	  if (token != NULL)
	    {
	      if (!(regexec (&regex, token, 0, NULL, 0)) && strlen(token)>5)
		{
		  //printf ("%s matched: %s len=%d\n", url,token,	  (int) strlen (token));
		  char *addr = strndup (token + 5, (int) strlen (token) - 5);

		  //critical section: insert into shared bounded queue acting as producer
		  pthread_mutex_lock (&lock1);
		  while (count == QUEUE_SIZE)
		    pthread_cond_wait (&empty, &lock1);

                  //check if url is not already visited
		  pthread_mutex_unlock (&lock1); 

		  _edge_fn(url,addr);

		  pthread_mutex_lock (&lock1);
                  if(lookup_string(urls, addr)==NULL){
			has_atleast_a_link = 1;
			add_string(urls, addr);
			put(addr);
			pthread_mutex_lock(&glock);
                        sum_queue_lengths++;
			pthread_mutex_unlock(&glock);
			//_edge_fn(url,addr);
		  	pthread_cond_signal (&fill);
                  }
		  pthread_mutex_unlock (&lock1);

		}

	    }
	  else
	    break;
	}
        pthread_mutex_lock(&glock);
	sum_queue_lengths--; 
        pthread_mutex_unlock(&glock);
      /*
      if (has_atleast_a_link == 0)
	{
	  pthread_mutex_lock (&glock);
	  sum_queue_lengths--;
	  pthread_mutex_unlock (&glock);
	}
      */
    }
  thr_exit ();
  //printf("parser thread exiting\n");
  return NULL;
}
コード例 #17
0
ファイル: sw.c プロジェクト: NanXiao/illumos-joyent
/*ARGSUSED*/
static int
sw_fmri_nvl2str(topo_mod_t *mod, tnode_t *node, topo_version_t version,
    nvlist_t *nvl, nvlist_t **out)
{
	nvlist_t *object, *site = NULL, *anvl = NULL;
	char *file, *func, *token;
	uint8_t scheme_version;
	char *path, *root;
	nvlist_t *fmristr;
	size_t buflen = 0;
	int linevalid = 0;
	char *buf = NULL;
	ssize_t size = 0;
	char linebuf[32];
	int64_t line;
	int pass;
	int err;

	if (version > TOPO_METH_NVL2STR_VERSION)
		return (topo_mod_seterrno(mod, EMOD_VER_NEW));

	if (nvlist_lookup_uint8(nvl, FM_VERSION, &scheme_version) != 0 ||
	    scheme_version > FM_SW_SCHEME_VERSION)
		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));

	/* Get authority, if present */
	err = nvlist_lookup_nvlist(nvl, FM_FMRI_AUTHORITY, &anvl);
	if (err != 0 && err != ENOENT)
		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));

	/*
	 * The 'object' nvlist is required. It must include the path,
	 * but the root is optional.
	 */
	if (nvlist_lookup_nvlist(nvl, FM_FMRI_SW_OBJ, &object) != 0 ||
	    !lookup_string(object, FM_FMRI_SW_OBJ_PATH, &path, B_TRUE) ||
	    !lookup_string(object, FM_FMRI_SW_OBJ_ROOT, &root, B_FALSE))
		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));

	/* The 'site' nvlist is optional */
	file = func = token = NULL;
	linevalid = 0;
	if ((err = nvlist_lookup_nvlist(nvl, FM_FMRI_SW_SITE, &site)) == 0) {
		/*
		 * Prefer 'token' to file/func/line
		 */
		if (lookup_string(site, FM_FMRI_SW_SITE_TOKEN, &token,
		    B_FALSE) <= 0) {
			/*
			 * If no token then try file, func, line - but
			 * func and line are meaningless without file.
			 */
			if (lookup_string(site, FM_FMRI_SW_SITE_FILE,
			    &file, B_FALSE) == 1) {
				(void) lookup_string(site, FM_FMRI_SW_SITE_FUNC,
				    &func, B_FALSE);
				if (nvlist_lookup_int64(site,
				    FM_FMRI_SW_SITE_LINE, &line) == 0)
					linevalid = 1;
			}
		}
	} else if (err != ENOENT) {
		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
	}

	/* On the first pass buf is NULL and size and buflen are 0 */
	pass = 1;
again:
	/*
	 * sw://[<authority>]/
	 *	[:root=<object.root]
	 *	:path=<object.path>
	 *	[#<fragment-identifier>]
	 *
	 *	<fragment-identifier> is one of
	 *
	 *		:token=<site.token>
	 *	or
	 *		:file=<site.file>[:func=<site.func>][:line=<site.line>]
	 */

	/* sw:// */
	topo_fmristr_build(&size, buf, buflen, FM_FMRI_SCHEME_SW,
	    NULL, "://");

	/* authority, if any */
	if (anvl != NULL) {
		nvpair_t *apair;
		char *aname, *aval;

		for (apair = nvlist_next_nvpair(anvl, NULL);
		    apair != NULL; apair = nvlist_next_nvpair(anvl, apair)) {
			if (nvpair_type(apair) != DATA_TYPE_STRING ||
			    nvpair_value_string(apair, &aval) != 0)
				continue;
			aname = nvpair_name(apair);
			topo_fmristr_build(&size, buf, buflen, ":", NULL, NULL);
			topo_fmristr_build(&size, buf, buflen, "=",
			    aname, aval);
		}
	}

	/* separating slash */
	topo_fmristr_build(&size, buf, buflen, "/", NULL, NULL);

	/* :root=... */
	if (root) {
		topo_fmristr_build(&size, buf, buflen, root,
		    ":" FM_FMRI_SW_OBJ_ROOT "=", NULL);
	}

	/* :path=... */
	topo_fmristr_build(&size, buf, buflen, path,
	    ":" FM_FMRI_SW_OBJ_PATH "=", NULL);

	if (token) {
		/* #:token=... */
		topo_fmristr_build(&size, buf, buflen, token,
		    "#:" FM_FMRI_SW_SITE_TOKEN "=", NULL);
	} else if (file) {
		/* #:file=... */
		topo_fmristr_build(&size, buf, buflen, file,
		    "#:" FM_FMRI_SW_SITE_FILE "=", NULL);

		/* :func=... */
		if (func) {
			topo_fmristr_build(&size, buf, buflen, func,
			    ":" FM_FMRI_SW_SITE_FUNC "=", NULL);
		}

		/* :line=... */
		if (linevalid) {
			if (pass == 1)
				(void) snprintf(linebuf, sizeof (linebuf),
				    "%lld", line);

			topo_fmristr_build(&size, buf, buflen, linebuf,
			    ":" FM_FMRI_SW_SITE_LINE "=", NULL);
		}
	}

	if (buf == NULL) {
		if ((buf = topo_mod_alloc(mod, size + 1)) == NULL)
			return (topo_mod_seterrno(mod, EMOD_NOMEM));

		buflen = size + 1;
		size = 0;
		pass = 2;
		goto again;
	}

	/*
	 * Construct the nvlist to return as the result.
	 */
	if (topo_mod_nvalloc(mod, &fmristr, NV_UNIQUE_NAME) != 0) {
		topo_mod_strfree(mod, buf);
		return (topo_mod_seterrno(mod, EMOD_NOMEM));
	}

	if (nvlist_add_string(fmristr, "fmri-string", buf) != 0) {
		topo_mod_strfree(mod, buf);
		nvlist_free(fmristr);
		return (topo_mod_seterrno(mod, EMOD_NOMEM));
	}
	topo_mod_strfree(mod, buf);
	*out = fmristr;

	return (0);
}
コード例 #18
0
 symbol (const char * str, std::size_t length):
     data(lookup_string(str, length))
 {}
コード例 #19
0
ファイル: read.c プロジェクト: JohannesBuchner/nfft
int
gsl_ieee_read_mode_string (const char * description, 
                           int * precision, 
                           int * rounding, 
                           int * exception_mask)
{
  char * start ;
  char * end;
  char * p;

  int precision_count = 0 ;
  int rounding_count = 0 ;
  int exception_count = 0 ;

  start = (char *) malloc(strlen(description) + 1) ;

  if (start == 0) 
    {
      GSL_ERROR ("no memory to parse mode string", GSL_ENOMEM) ;
    }

  strcpy (start, description) ;

  p = start ;

  *precision = 0 ;
  *rounding = 0 ;
  *exception_mask = 0 ;

  do {
    int status ;
    int new_precision, new_rounding, new_exception ;

    end = strchr (p,',') ;

    if (end) 
      {
        *end = '\0' ;
        do 
          {
            end++ ;  /* skip over trailing whitespace */
          } 
        while (*end == ' ' || *end == ',') ;
      }
        
    new_precision = 0 ; 
    new_rounding = 0 ; 
    new_exception = 0 ;

    status = lookup_string (p, &new_precision, &new_rounding, &new_exception) ;

    if (status)
      GSL_ERROR ("unrecognized GSL_IEEE_MODE string.\nValid settings are:\n\n" 
                 "  single-precision double-precision extended-precision\n"
                 "  round-to-nearest round-down round-up round-to-zero\n"
                 "  mask-invalid mask-denormalized mask-division-by-zero\n"
                 "  mask-overflow mask-underflow mask-all\n"
                 "  trap-common trap-inexact\n"
                 "\n"
                 "separated by commas. "
                 "(e.g. GSL_IEEE_MODE=\"round-down,mask-underflow\")",
                 GSL_EINVAL) ;

    if (new_precision) 
      {
        *precision = new_precision ;
        precision_count ++ ;
        if (precision_count > 1)
          GSL_ERROR ("attempted to set IEEE precision twice", GSL_EINVAL) ;
      }

    if (new_rounding) 
      {
        *rounding = new_rounding ;
        rounding_count ++ ;
        if (rounding_count > 1)
          GSL_ERROR ("attempted to set IEEE rounding mode twice", GSL_EINVAL) ;
      }

    if (new_exception) 
      {
        *exception_mask |= new_exception ;
        exception_count ++ ;
      }

    p = end ; 

  } while (end && *p != '\0') ;

  free(start) ;

  return GSL_SUCCESS ;
}