示例#1
0
//does not free HandlerInfo struct itself, or .req
void HL_Free(HandlerInfo *info)
{
  int i, len;

  s_free(info->docroot);
  s_free(info->servroot);
  
  //free url query
  len = array_len(info->query);
  for (i=0; i<len; i++) {
    s_free(info->query[i]);
  }
  if (info->query)
    array_free(info->query);

  //free body query, if it exists (e.g. POST-submitted forms)
  len = array_len(info->query2);
  for (i=0; i<len; i++) {
    s_free(info->query2[i]);
  }
  if (info->query2)
    array_free(info->query2);

  if (info->out_buf)
    s_free(info->out_buf);

  info->query = NULL;
  info->out_buf = NULL;
}
示例#2
0
TEST_F(ArrTest, testEnsure) {
  Foo *f = array_new(Foo, 1);
  array_hdr_t *hdr = array_hdr(f);
  Foo *tail = array_ensure_tail(&f, Foo);
  // Make sure Valgrind does not complain!
  tail->x = 0;
  tail->y = 0;

  Foo *middle = array_ensure_at(&f, 5, Foo);
  ASSERT_EQ(0, middle->x);
  ASSERT_EQ(0, middle->y);

  for (size_t ii = 0; ii < array_len(f); ++ii) {
    ASSERT_EQ(0, f[ii].x);
    ASSERT_EQ(0, f[ii].y);
  }

  // Try again with ensure_tail
  tail = array_ensure_tail(&f, Foo);
  f->x = 99;
  f->y = 990;
  tail->x = 100;
  tail->y = 200;

  // ensure_append
  Foo threeFoos[] = {{10, 11}, {20, 21}, {30, 31}};
  size_t prevlen = array_len(f);
  f = array_ensure_append(f, &threeFoos, 3, Foo);
  ASSERT_EQ(10, f[prevlen].x);
  ASSERT_EQ(20, f[prevlen + 1].x);
  ASSERT_EQ(30, f[prevlen + 2].x);
  array_free(f);
}
示例#3
0
文件: testa.c 项目: stevedonovan/llib
int main(int argc,  const char **argv)
{
    ArgState *state = arg_command_line(args,argv);
    if (! interactive) {
        if (array_len(incdirs) > 0) {
            printf("the include paths\n");
            FOR_ARR (str_t,P,incdirs)
                printf("'%s'\n",*P);
        }
        if (array_len(string_args) > 0) {
            printf("the string args\n");
            FOR_ARR (str_t,P,string_args)
                printf("'%s'\n",*P);
        }
        printf("flag a is %d\n",a);
    } else {
        char *line;
            printf("> ");
        while ((line = file_getline(stdin)) != NULL) {
            char **parts = str_split(line," ");
            // args_process assumes args start at second element, hence -1 here
            PValue v = arg_process(state,(const char**)parts-1);
            if (v != NULL) {
                printf("%s\n",value_tostring(v));
                unref(v);
            }
            dispose(parts,line);
            printf("> ");
            arg_reset_used(state);
        }        
    }
    return 0;
}
示例#4
0
void SpellCheck_SendReplyOnTerm(RedisModuleCtx *ctx, char *term, size_t len, RS_Suggestions *s,
                                uint64_t totalDocNumber) {
#define TERM "TERM"
  RedisModule_ReplyWithArray(ctx, 3);
  RedisModule_ReplyWithStringBuffer(ctx, TERM, strlen(TERM));
  RedisModule_ReplyWithStringBuffer(ctx, term, len);

  RS_Suggestion **suggestions = spellCheck_GetSuggestions(s);

  for (int i = 0; i < array_len(suggestions); ++i) {
    if (suggestions[i]->score == -1) {
      suggestions[i]->score = 0;
    } else {
      if (totalDocNumber > 0) {
        suggestions[i]->score = (suggestions[i]->score) / totalDocNumber;
      }
    }
  }

  qsort(suggestions, array_len(suggestions), sizeof(RS_Suggestion *), RS_SuggestionCompare);

  if (array_len(suggestions) == 0) {
    // no results found, we return an empty array
    RedisModule_ReplyWithArray(ctx, 0);
  } else {
    RedisModule_ReplyWithArray(ctx, array_len(suggestions));
    for (int i = 0; i < array_len(suggestions); ++i) {
      RedisModule_ReplyWithArray(ctx, 2);
      RedisModule_ReplyWithDouble(ctx, suggestions[i]->score);
      RedisModule_ReplyWithStringBuffer(ctx, suggestions[i]->suggestion, suggestions[i]->len);
    }
  }

  array_free_ex(suggestions, RS_SuggestionFree(*(RS_Suggestion **)ptr));
}
示例#5
0
文件: tw.c 项目: Alberne/tinyprograme
//设置优惠活动 
struct sales_promotion *get_promotion() 
{
	static struct sales_promotion *promotions;
        struct promotion_one_info *p; //临时变量  
    	struct set_t *pset; //临时变量
    	int i;  
    	static struct sales_products discount_promotion[] = { //折扣商品信息
		{"ITEM00002", 1},
		{"ITEM00001", 1}
		//{"ITEM00003", 0},
		//{"ITEM00004", 0}
	};
	static struct sales_products one_free_promotion[] = { //买二送一商品信息
		{"ITEM00001", 1},
		{"ITEM00003", 1}
		//{"ITEM00003", 0}
	};
	 
    	PNEW(promotions, 1, struct sales_promotion *);
	promotions->promotion_all_info = NULL;
	
        /*假设当前有以下两种活动*
         *  1. 买二送一活动
         *  2. 9.5折优惠 */
    	promotions->priority = FREE_ONE;  //当某种商品满足两种优惠时,优先选则活动一
     
        /*首先设置优惠活动1信息*/
     	PNEW(p, 1, struct promotion_one_info *);
     	p->next_promotion = promotions->promotion_all_info;
     	promotions->promotion_all_info = p;
     	p->type = FREE_ONE;
     	strcpy(p->statement, "买二送一活动");
     	pset = set_new(100, compare_product, hashcode);
     	p->sales_set = pset; 

     	for(i = 0; i < array_len(one_free_promotion); i++) {
     		set_put(pset, &one_free_promotion[i]);		 		 
     	}
 
     	/*其次设置优惠活动2信息*/ 
	 PNEW(p, 1, struct promotion_one_info *); //分配活动2商品所需的内存
	 //链表头插法
	 p->next_promotion = promotions->promotion_all_info;
	 promotions->promotion_all_info = p;
	 p->type = DISCOUNT;
	 strcpy(p->statement, "9.5折优惠");
	 pset = set_new(100, compare_product, hashcode);
	 p->sales_set = pset;

	 for(i = 0; i < array_len(discount_promotion); i++) {
		 set_put(pset, &discount_promotion[i]);
	 }
	 
	 return promotions;
}
示例#6
0
文件: set.c 项目: salvy/Neverball-svn
int set_init()
{
    fs_file fin;
    char *name;

    Array items;
    int i;

    if (sets)
        set_quit();

    sets = array_new(sizeof (struct set));
    curr = 0;

    /*
     * First, load the sets listed in the set file, preserving order.
     */

    if ((fin = fs_open(SET_FILE, "r")))
    {
        while (read_line(&name, fin))
        {
            struct set *s = array_add(sets);

            if (!set_load(s, name))
                array_del(sets);

            free(name);
        }
        fs_close(fin);
    }

    /*
     * Then, scan for any remaining set description files, and add
     * them after the first group in alphabetic order.
     */

    if ((items = fs_dir_scan("", is_unseen_set)))
    {
        array_sort(items, cmp_dir_items);

        for (i = 0; i < array_len(items); i++)
        {
            struct set *s = array_add(sets);

            if (!set_load(s, DIR_ITEM_GET(items, i)->path))
                array_del(sets);
        }

        fs_dir_free(items);
    }

    return array_len(sets);
}
示例#7
0
int main (int argc, char ** argv) {
	if (argc != 2) {
		fprintf (stderr, "usage: %s <N>\n", argv[0]);
		return 1;
	}

	int N = atoi (argv[1]);

	if (N <= 0)
		return 1;

	/*
	 * C_n,r = n!/r!(n - r)!
	 * We need to check when C_n,r > LIMIT ~ n!/r! > LIMIT * (n - r)!
	 * Precompute the right side of the inequality in factorials[] and the left side in cancelled_factorials[]
	 * Given that C_n,r == C_n,n-r we only need to consider r <= n /2
	 */

	bignum_t * factorials[N + 1];

	factorials[0] = bignum_get (LIMIT);

	for (size_t i = 1; i < array_len (factorials); i++)
		factorials[i] = bignum_mult (factorials[i - 1], (int) i);

	int count = 0;

	for (int n = 1; n <= N; n++) {
		bignum_t * cancelled_factorials[n + 1];

		cancelled_factorials[n] = bignum_get (1);

		for (int i = n - 1; i >= 0; i--)
			cancelled_factorials[i] = bignum_mult (cancelled_factorials[i + 1], i + 1);

		for (int r = 1; r <= n / 2 ; r++)
			if (bignum_cmp (cancelled_factorials[r], factorials[n - r]) > 0) {
				if (r + r == n)
					count++;
				else
					count += 2;
			}

		bignum_free_array (cancelled_factorials, array_len (cancelled_factorials));
	}

	bignum_free_array (factorials, array_len (factorials));

	printf ("%d\n", count);

	return 0;
}
示例#8
0
static int lang_gui(void)
{
    const int step = (first == 0 ? LANG_STEP - 1 : LANG_STEP);

    int id, jd;
    int i;

    if ((id = gui_vstack(0)))
    {
        if ((jd = gui_hstack(id)))
        {
            gui_label(jd, _("Language"), GUI_SML, 0, 0);
            gui_space(jd);
            gui_space(jd);
            gui_navig(jd, array_len(langs), first, LANG_STEP);
        }

        gui_space(id);

        if (step < LANG_STEP)
        {
            int default_id;
            default_id = gui_state(id, _("Default"), GUI_SML, LANG_DEFAULT, 0);
            gui_set_hilite(default_id, !*config_get_s(CONFIG_LANGUAGE));
        }

        for (i = first; i < first + step; i++)
        {
            if (i < array_len(langs))
            {
                struct lang_desc *desc = LANG_GET(langs, i);

                int lang_id;

                lang_id = gui_state(id, lang_name(desc),
                                    GUI_SML, LANG_SELECT, i);

                gui_set_hilite(lang_id, (strcmp(config_get_s(CONFIG_LANGUAGE),
                                                desc->code) == 0));
            }
            else
            {
                gui_label(id, " ", GUI_SML, 0, 0);
            }
        }

        gui_layout(id, 0, 0);
    }

    return id;
}
示例#9
0
Array * normalize(Array * vector)
{
        register int i;
        register double sum = 0;

        for (i = 0; i < array_len(vector); i++)
                sum += array_get(vector, i);

        for (i = 0; i < array_len(vector); i++)
                array_diveq(vector, i, sum);

        return vector;

}
示例#10
0
文件: ipp.c 项目: ebichu/dd-wrt
int ipp_add_tag( IPP *ipp, char tag_type, char *name,  void *value, short val_len, int set )
{
  struct _ipp_attr_seq *s;
  struct _ipp_attr_value *attr_value;
  IPP_ATTR *attr;
  ARRAY *vals;
  int i;

  if( tag_type&0xF0 ) { // Value
    s = array_get( ipp->seqs, array_len( ipp->seqs ) - 1 );

    for( i = 0; i < array_len( s->attr ); i++ ) {
      attr = array_get( s->attr, i );
      if( strcmp( attr->name, name ) == 0 ) {
        if( set ) {
          vals = attr->values;
        } else {
          array_free( attr->values );
          vals = attr->values = array_new();
        }
        break;
      }
    }

    if( i == array_len( s->attr ) ) {
      attr = calloc( 1, sizeof( IPP_ATTR ) );
      attr->name = strdup( name );
      vals = attr->values = array_new();
      array_add( s->attr, attr );
    }

    attr_value = malloc( sizeof( struct _ipp_attr_value ) );

    attr_value->type = tag_type;

    if( tag_type & (IPP_TAG_INTEGERS|IPP_TAG_OCTET_STRING) ) {  // Binary
      attr_value->value = malloc( val_len );
      memcpy( attr_value->value, value, val_len );
    } else if ( tag_type & IPP_TAG_CHAR_STRING ) { // CHARACTER-STRING
      attr_value->value = malloc( val_len + 1 ); // Terminating NUL
      memcpy( attr_value->value, value, val_len );
      ((char *)attr_value->value)[val_len] = '\0';
    }
    attr_value->len = val_len;
    array_add( vals, attr_value );
  } else {
    ipp_add_seq( ipp, tag_type );
  }
  return 1;
}
示例#11
0
int main (int argc, char ** argv) {
	if (argc != 2) {
		fprintf (stderr, "usage: %s <N>\n", argv[0]);
		return 1;
	}

	int N = atoi (argv[1]);

	if (N < 6)
		return 1;

	int counts[N + 1];

	for (size_t i = 0; i < array_len (counts); i++)
		counts[i] = 0;

	for (int n = 1;; n++) {
		if (euclid_pythagorean_triple_perim (n + 1, n) > N)
			break;

		for (int m = n + 1;; m++) {
			int primitive_length = euclid_pythagorean_triple_perim (m, n);

			if (primitive_length == 0)
				continue;
			else if (primitive_length > N)
				break;

			for (int len = primitive_length; len <= N; len += primitive_length)
				counts[len] += 1;
		}
	}


	int max_count = 0;
	int perimeter_when_max = 0;

	for (size_t i = 0; i < array_len (counts); i++)
		if (counts[i] > max_count) {
			max_count = counts[i];
			perimeter_when_max = i;
		}

	if (max_count > 0) {
		printf ("%d\n", perimeter_when_max);
		return 0;
	} else
		return 1;
}
示例#12
0
TEST_F(ArrTest, testScalar) {
  int *ia = array_new(int, 8);
  for (size_t i = 0; i < 100; i++) {
    ia = array_append(ia, i);
    ASSERT_EQ(i + 1, array_len(ia));
    ASSERT_EQ(i, array_tail(ia));
  }

  for (size_t i = 0; i < array_len(ia); i++) {
    ASSERT_EQ(i, ia[i]);

    // printf("%d %zd\n", ia[i], array_len(ia));
  }
  array_free(ia);
}
示例#13
0
static void
handle_KeyField (char *name, char **args)
{
  register int i;
  int offset = array_len (key_fields);
  int extra = array_len (args);

  key_fields = (char **)
    xrealloc (key_fields, (2 + extra + offset) * sizeof (char *));

  for (i = 0; args[i] != (char *)NULL; i++)
    key_fields[i + offset] = strdup (args[i]);

  key_fields[i + offset] = (char *)NULL;
}
示例#14
0
TEST_F(ArrTest, testTrimm) {
  const char *strs[] = {"foo", "bar", "baz", NULL};
  const char **a = array_new(const char *, 16);
  size_t i = 0;
  for (i = 0; strs[i] != NULL; i++) {
    a = array_append(a, strs[i]);
    ASSERT_EQ(i + 1, array_len(a));
    ASSERT_STREQ(strs[i], array_tail(a));
  }
  a = array_trimm_cap(a, 2);
  ASSERT_EQ(array_len(a), 2);
  array_trimm_len(a, 1);
  ASSERT_EQ(array_len(a), 1);
  array_free(a);
}
示例#15
0
static bool SpellCheck_CheckTermDictsExistance(SpellCheckCtx *scCtx) {
  for (int i = 0; i < array_len(scCtx->includeDict); ++i) {
    if (!SpellCheck_CheckDictExistence(scCtx, scCtx->includeDict[i])) {
      return false;
    }
  }

  for (int i = 0; i < array_len(scCtx->excludeDict); ++i) {
    if (!SpellCheck_CheckDictExistence(scCtx, scCtx->excludeDict[i])) {
      return false;
    }
  }

  return true;
}
示例#16
0
static void
handle_description_command (char *command, char **args)
{
  register int i;

  for (i = 0; commands[i].name; i++)
    if (strcasecmp (command, commands[i].name) == 0)
      {
	int args_len = array_len (args);

	if (commands[i].required_args > args_len)
	  {
	    fprintf (stderr, "Error: `%s' Requires at least %d args, got %d\n",
		     command, commands[i].required_args, args_len);
	    exit (1);
	  }

	(*commands[i].handler) (command, args);
	return;
      }

  fprintf (stderr, "Error: Unrecognized command (%s) in description file\n",
	   command);
  exit (1);
}
示例#17
0
static char	*_create_msg(char **argv, char *user)
{
  char		*msg;
  int		i;

  if (argv[0][0] == ':')
    msg = my_malloc(strlen("PRIVMSG ") + strlen(user) + strlen(" ") +
		    array_len(argv) + 1);
  else
    msg = my_malloc(strlen("PRIVMSG ") + strlen(user) + strlen(" :") +
		    strlen(argv[0]) + 1);
  msg[0] = '\0';
  msg = strcat(msg, "PRIVMSG ");
  msg = strcat(msg, user);
  msg = strcat(msg, " ");
  if (argv[0][0] == ':')
    {
      i = -1;
      while (argv[++i] != NULL)
	msg = strcat(msg, argv[i]);
    }
  else
    {
      msg = strcat(msg, ":");
      msg = strcat(msg, argv[0]);
    }
  return (msg);
}
示例#18
0
static int demo_enter(struct state *st, struct state *prev)
{
    if (!items || (prev == &st_demo_del))
    {
        if (items)
        {
            demo_dir_free(items);
            items = NULL;
        }

        items = demo_dir_scan();
        total = array_len(items);
    }

    first       = first < total ? first : 0;
    last        = MIN(first + DEMO_STEP - 1, total - 1);
    last_viewed = MIN(MAX(first, last_viewed), last);

    if (total)
        demo_dir_load(items, first, last);

    audio_music_fade_to(0.5f, "bgm/inter.ogg");

    return demo_gui();
}
示例#19
0
文件: base.c 项目: vcosta/derclou
static char AutoDetectLanguage(void)
{
    size_t i;
    const char langs[] = {
      'd', 'e', 'f', 's'
    };
    char lang = ' ';

    DebugMsg(ERR_DEBUG, ERROR_MODULE_BASE, "Detecting Language...");

    for (i = 0; i < array_len(langs); i++) {
        char File[DSK_PATH_MAX], Path[DSK_PATH_MAX];
    
        sprintf(File, "tcmaine%c.txt", langs[i]);
        if (dskBuildPathName(DISK_CHECK_FILE, TEXT_DIRECTORY, File, Path)) {
            lang = langs[i];
	    break;
	}
    }

    if (lang == ' ') {
	DebugMsg(ERR_ERROR, ERROR_MODULE_BASE, "Could not detect language!");
    } else {
	DebugMsg(ERR_DEBUG, ERROR_MODULE_BASE, "Detected Language: '%c'", lang);
    }
    return lang;
}
示例#20
0
static void
handle_Template (char *name, char **args)
{
  register int i;
  int offset = array_len (record_template);
  int extra = array_len (args);

  record_template = (char **)
    xrealloc (record_template, (2 + extra + offset) * sizeof (char *));

  for (i = 0; args[i] != (char *)NULL; i++)
    {
      record_template[i + offset] = strdup (args[i]);
    }

  record_template[i + offset] = (char *)NULL;
}
示例#21
0
文件: main.c 项目: lubomyr/Neverball
static void make_dirs_and_migrate(void)
{
    Array items;
    int i;

    const char *src;
    char *dst;

    if (fs_mkdir("Replays"))
    {
        if ((items = fs_dir_scan("", is_replay)))
        {
            for (i = 0; i < array_len(items); i++)
            {
                src = DIR_ITEM_GET(items, i)->path;
                dst = concat_string("Replays/", src, NULL);
                fs_rename(src, dst);
                free(dst);
            }

            fs_dir_free(items);
        }
    }

    if (fs_mkdir("Scores"))
    {
        if ((items = fs_dir_scan("", is_score)))
        {
            for (i = 0; i < array_len(items); i++)
            {
                src = DIR_ITEM_GET(items, i)->path;
                dst = concat_string("Scores/",
                                    src + sizeof ("neverballhs-") - 1,
                                    ".txt",
                                    NULL);
                fs_rename(src, dst);
                free(dst);
            }

            fs_dir_free(items);
        }
    }

    fs_mkdir("Screenshots");
}
示例#22
0
char *RQ_UnEscapeDup(char *input, int mask) {
  char *buf = s_dup(input);
  int blen = array_len(buf)*2;

  array_resize(buf, blen);
  RQ_UnEscapePath(input, buf, blen, mask);

  return buf;
}
示例#23
0
char *policy_name(int policy)
{
    static char buf[32];
    if (policy >= array_len(policy_names)) {
        sprintf(buf, "[%d]", policy);
        return buf;
    }
    return policy_names[policy];
}
示例#24
0
文件: ipp.c 项目: ebichu/dd-wrt
IPP_ATTR *ipp_get_tag( IPP *ipp, char seq, char *name, int nr ) {
  int i, e;
  struct _ipp_attr_seq *s;
  IPP_ATTR *a;

  for( i = 0; i < array_len( ipp->seqs); i++ ) {
    s = array_get( ipp->seqs, i );
    if( s->seq == seq && !--nr) {
      for( e = 0; e < array_len( s->attr ); e++ ) {
        a = array_get( s->attr, e );
        if( strcmp( a->name, name ) == 0 ) {
          return a;
        }
      }
    }
  }
  return NULL;
}
示例#25
0
void MultiBranchData::post_initialize(BytecodeStream* stream,
                                      methodDataOop mdo) {
  assert(stream->bci() == bci(), "wrong pos");
  int target;
  int my_di;
  int target_di;
  int offset;
  if (stream->code() == Bytecodes::_tableswitch) {
    Bytecode_tableswitch* sw = Bytecode_tableswitch_at(stream->bcp());
    int len = sw->length();
    assert(array_len() == per_case_cell_count * (len + 1), "wrong len");
    for (int count = 0; count < len; count++) {
      target = sw->dest_offset_at(count) + bci();
      my_di = mdo->dp_to_di(dp());
      target_di = mdo->bci_to_di(target);
      offset = target_di - my_di;
      set_displacement_at(count, offset);
    }
    target = sw->default_offset() + bci();
    my_di = mdo->dp_to_di(dp());
    target_di = mdo->bci_to_di(target);
    offset = target_di - my_di;
    set_default_displacement(offset);

  } else {
    Bytecode_lookupswitch* sw = Bytecode_lookupswitch_at(stream->bcp());
    int npairs = sw->number_of_pairs();
    assert(array_len() == per_case_cell_count * (npairs + 1), "wrong len");
    for (int count = 0; count < npairs; count++) {
      LookupswitchPair *pair = sw->pair_at(count);
      target = pair->offset() + bci();
      my_di = mdo->dp_to_di(dp());
      target_di = mdo->bci_to_di(target);
      offset = target_di - my_di;
      set_displacement_at(count, offset);
    }
    target = sw->default_offset() + bci();
    my_di = mdo->dp_to_di(dp());
    target_di = mdo->bci_to_di(target);
    offset = target_di - my_di;
    set_default_displacement(offset);
  }
}
示例#26
0
void RQ_PrintHeaders(ReqInfo *req) {
  int i, ilen;

  ilen = array_len(req->headers)/2;
  printf("==Headers==");
  for (i=0; i<ilen; i++) {
    printf("%s: %s\n", req->headers[i*2], req->headers[i*2+1]);
  }
  printf("\n");
}
示例#27
0
文件: set.c 项目: senquack/neverball
static int set_is_loaded(const char *path)
{
    int i;

    for (i = 0; i < array_len(sets); i++)
        if (strcmp(SET_GET(sets, i)->file, path) == 0)
            return 1;

    return 0;
}
示例#28
0
文件: set.c 项目: senquack/neverball
void set_quit(void)
{
    int i;

    for (i = 0; i < array_len(sets); i++)
        set_free(array_get(sets, i));

    array_free(sets);
    sets = NULL;
}
示例#29
0
char *RQ_GetHeader(ReqInfo *req, char *header)
{
  int len = array_len(req->headers), i;

  for (i=0; i<len/2; i++) {
    if (req->headers[i*2] && !strcncmp(req->headers[i*2], header, s_len(req->headers[i*2]), strlen(header)))
      return i*2+1 < len ? req->headers[i*2+1] : NULL;
  }

  return NULL;
}
示例#30
0
char *RQ_GetQueryKey(HandlerInfo *info, char *key)
{
  int len = array_len(info->query), i;

  for (i=0; i<len/2; i++) {
    if (info->query[i*2] && !strcmp(info->query[i*2], key))
      return i*2+1 < len ? info->query[i*2+1] : NULL;
  }

  return NULL;
}