예제 #1
0
Test(Player, look_empty)
{
	player_t *pl = player_create_at((vector2d_t){9, 9});
	game_t *gm = game_create(20, 20, 7, 5);

	cr_assert(pl);
	cr_assert(gm);
	game_add_team(gm, "pandas");
	pl->p_teamname = strdup("pandas");
	cr_assert_neq(game_register_player(gm, pl), -1);
	dynbuf_t *buf = player_look(pl, gm);
	cr_assert(buf);
	cr_log_info(buf->b_data);
	cr_assert(strstr(buf->b_data, "player"));
	cr_assert_eq(count_char(buf->b_data, ','), 3);
	pl->p_lvl = 2;
	dynbuf_delete(buf);
	buf = player_look(pl, gm);
	cr_log_info(buf->b_data);
	cr_assert(strstr(buf->b_data, "player"));
	cr_assert_eq(count_char(buf->b_data, ','), 8);
	dynbuf_delete(buf);
	pl->p_lvl = 3;
	buf = player_look(pl, gm);
	cr_log_info(buf->b_data);
	cr_assert(strstr(buf->b_data, "player"));
	cr_assert_eq(count_char(buf->b_data, ','), 15);
	dynbuf_delete(buf);
	game_delete(gm);
}
예제 #2
0
파일: main.c 프로젝트: msiemens/mlisp
char* read_input() {
    char* input = readline(">>> ");

    if (input == NULL) {
        return input;
    }

    int braces_balanced = 0;

    do {
        int parens_l = count_char(input, '(');
        int parens_r = count_char(input, ')');
        int braces_l = count_char(input, '{');
        int braces_r = count_char(input, '}');

        braces_balanced = (parens_r == parens_l && braces_r == braces_l);
        if (!braces_balanced) {
            // Read next line and append it
            char* continuation = readline("... ");
            if (strlen(continuation) == 0) { return input; }
            input = strappend(input, continuation, strlen(input) + strlen(continuation) + 1);
            free(continuation);
        }
    } while (!braces_balanced);

    return input;
}
예제 #3
0
int count_char(char ch, const char *string){
    int ans;
    printf("ch:%c | string[0]:%c | string[1]:%c | string: %s\n", ch, string[0], string[1], string);
    if(string[0] == '\0'){
        ans = 0;
    } else {
        if( ch == string[0]){
            ans = 1 + count_char(ch, &string[1]);
        } else {
            ans = count_char(ch, &string[1]);
        }
    }
    printf("ans: %d\n", ans);
    return ans;
}
예제 #4
0
파일: updates.c 프로젝트: cache-s/CPP-Zappy
int			update_client(t_client *client, UNUSED t_serv *serv)
{
  char			*new_cmd;
  int			len;
  int			len_s;
  char			*save_cmd;

  if (client->cmd == NULL)
    return (EXIT_SUCCESS);
  save_cmd = strdup(client->cmd);
  len = strlen(client->cmd);
  strtok(client->cmd, ";");
  len_s = strlen(client->cmd) + 1;
  if ((new_cmd = malloc(len - len_s + 1)) == NULL)
    return (my_error(ERR_MALLOC));
  new_cmd = str_cpy_from(new_cmd, save_cmd, len_s);
  client->cmd = strdup(new_cmd);
  if (count_char(client->cmd, ';') < 1)
    client->cmd = NULL;
  if (client->shortest_cmd != NULL)
    free(client->shortest_cmd);
  client->shortest_cmd = NULL;
  client->need_write = 0;
  return (EXIT_SUCCESS);
}
예제 #5
0
int getusbcount()
{
    char* result="";
    result =cmd_system("lsusb");
    int number=count_char(result,'\n');
    return number;
}
예제 #6
0
파일: scrap.c 프로젝트: LarBob/executor
PUBLIC int
get_scrap_helper (void *vh, void *lp, int len, boolean_t convert_text)
{
  int retval;
  int new_len;
  Handle h;

  if (convert_text)
    new_len = len - count_char (lp, len, '\n'); /* won't copy linefeeds */
  else
    new_len = len;
  h = (Handle) vh;
  ReallocHandle (h, new_len);
  if (MemErr != noErr)
    retval = -1;
  else
    {
      if (convert_text)
	memcpy_but_delete_char (STARH (h), lp, len, '\n');
      else
	memcpy (STARH (h), lp, len);
      retval = new_len;
    }
  return retval;
}
예제 #7
0
static void parse_colors(char *s)
{
    int n = ncolors + 1 + count_char(s, ',');

    g_colors = (Uint32*)realloc(g_colors, n * sizeof(Uint32));

    // we'll need an X display for this... yuck!
    Display *dpy = XOpenDisplay(NULL);
    if (!dpy) {
        fprintf(stderr, "can't open display\n");
        exit(EXIT_FAILURE);
    }
    Colormap cmap = DefaultColormap(dpy, 0);

    // tokenize the string and parse each color
    char *p = strsep(&s, ",");
    while (p) {
        Uint32 c;
        if (strlen(p) || !ncolors) {
            XColor color;
            if (!XParseColor(dpy, cmap, p, &color)) {
                fprintf(stderr, "can't parse color: %s\n", p);
                exit(EXIT_FAILURE);
            }

            c = (color.red / 256) << 16 | (color.green / 256) << 8 | (color.blue / 256);
        } else {
            c = g_colors[ncolors - 1];
        }
        g_colors[ncolors++] = c;

        p = strsep(&s, ",");
    }
}
예제 #8
0
Test(Player, look_up)
{
	player_t *pl = player_create_at((vector2d_t){9, 9});
	game_t *gm = game_create(20, 20, 7, 5);

	cr_assert(pl);
	cr_assert(gm);
	game_add_team(gm, "pandas");
	pl->p_teamname = strdup("pandas");
	cr_assert_neq(game_register_player(gm, pl), -1);
	pl->p_dir = (vector2d_t){0, -1};

	board_put_resource(gm->ga_board, (vector2d_t){9, 9}, SIBUR);
	board_put_resource(gm->ga_board, (vector2d_t){8, 8}, THYSTAME);
	board_put_resource(gm->ga_board, (vector2d_t){9, 8}, LINEMATE);
	board_put_resource(gm->ga_board, (vector2d_t){10, 8}, DERAUMERE);
	board_inc_food(gm->ga_board, (vector2d_t){10, 8});
	board_inc_food(gm->ga_board, (vector2d_t){10, 8});

	dynbuf_t *buf = player_look(pl, gm);
	cr_assert(buf);
	cr_log_info(buf->b_data);
	cr_assert(strstr(buf->b_data, "player"));
	cr_assert_eq(count_char(buf->b_data, ','), 3);
	cr_assert_str_eq(buf->b_data,
		"[sibur player,thystame,linemate,food food deraumere]");
	dynbuf_delete(buf);
	game_delete(gm);
}
예제 #9
0
파일: dns.c 프로젝트: HarryR/ffff-dnsp2p
bool
f4dns_is_valid(const char *fqdn) {
    char *tmp;
    const char *b = fqdn;
    const char *a = NULL;
    int dashes = 0;
    assert( fqdn != NULL );
    assert( strlen(fqdn) > 3 ); // XXX: should be more like 36 chars minimum?

    if( fqdn[0] == '.' ) return false;
    while( (tmp = strchr(b,'.')) ) {
        a = b;
        b = ++tmp;
    }

    if( a == NULL ) return false;
    if( strcmp(b, "key") != 0 ) return false;

    tmp = strchr(a, '.');
    assert( tmp != NULL );

    dashes = count_char(a, '-');
    if( (tmp - a) - dashes != 32 ) return false;

    return true;
}
예제 #10
0
char *ft_unsplit(char **table)
{
	char	*s;
	int		i;
	int		j;
	int		k;

	k = 0;
	s = ft_strnew(count_char(table) + 1);
	i = 0;
	while (table[i])
	{
		j = 0;
		while (table[i][j])
		{
			s[k] = table[i][j];
			k++;
			j++;
		}
		s[k] = ' ';
		k++;
		i++;
	}
	s[k] = '\0';
	return (s);
}
예제 #11
0
파일: test.c 프로젝트: MrRobot245/Cis-1500
int main(void)
{
	char data[20][7] = {"moider","cardon","labrum","sneest","lunoid",
	                    "axil","munj","furca","stingy","beode"};
	int i, numV, n, choice;
	char opt;
	
	n = 10;
	
	for (i=0; i<n; i=i+1)
	    printf("%d. %s\n", i+1, data[i]);
	    
	numV = count_vowels(data,n);
	
	printf("The words have %d vowels\n", numV);
	printf("There are %d total characters\n", count_char(data,n));

    printf("Vowels - quash them? (y/n): ");
	scanf("%c%*c", &opt);
	if (opt == 'y' || opt == 'Y')
	{
	    printf("Which word? (1-%d) :", n);
	    scanf("%d", &choice);
	    vowel_quash(data,choice-1);
	    printf("The quashed word is %s\n", data[choice-1]);
	}    
	

    return 0;
}
예제 #12
0
// Returns number of types.
int load_hla_csv(const char *path, char ***bools_ptr, int num_rows)
{
  assert(num_rows > 0);

  StrBuf line;
  strbuf_alloc(&line, 1024);

  FILE *fh = fopen(path, "r");
  if(fh == NULL) die("Cannot open file: %s.", path);

  if(strbuf_readline(&line, fh) == 0) die("Empty CSV file: %s.", path);
  int num_types = count_char(line.b, ',');

  char **bools = my_malloc(sizeof(char*) * num_rows, __FILE__, __LINE__);
  char *data = my_malloc(sizeof(char) * num_rows * (num_types+1), __FILE__, __LINE__);
  printf("Number of rows: %i.\n",num_rows);
  int i;
  for(i = 0; i < num_rows && strbuf_reset_readline(&line, fh); i++)
  {
    strbuf_chomp(&line);
    bools[i] = data + i * (num_types+1);
    load_comma_bool_line(line.b, bools[i], num_types);
    bools[i][num_types] = '\0';
  }

  if(i < num_rows) die("Not enough rows in CSV file: %s.", path);

  fclose(fh);
  strbuf_dealloc(&line);

  *bools_ptr = bools;
  return num_types;
}
예제 #13
0
파일: hello.c 프로젝트: Kimuuulf/INF1060h15
int main(int argc, char* argv[]) {
    char* text = TEKST;

#ifdef UTSKRIFT
    printf("%d\n", count_char('a', text));
#endif

    return 1;
}
예제 #14
0
파일: itsh.c 프로젝트: Ponty-/Past_Projects
/** interpret_command(char* line)
**		Take the input from stdin or a script file and react appropriately.
**		This means executing a command, changing the directory or exiting.
*/
void interpret_command(char* line) {
	if (line[0] == '#') { 
		/*Comment - do nothing*/
		return;
	}
	/*Check for pipes*/
	else if (count_char(line, '|') > 0) {
		/*Split the two commands*/
		char** pipeCmds = split_string(line, 1, "|");
		//Make two commands based on [0] and [1] of pipeCmds
		Command cmd1, cmd2;
		//Change last space of pipeCmds to null
		pipeCmds[0][strlen(pipeCmds[0]) - 1] = '\0';
		strcpy(cmd1.cmdString, pipeCmds[0]);
		//Copy from 2nd char, first char is space
		strcpy(cmd2.cmdString, pipeCmds[1] + 1);
		setup_command(&cmd1);
		setup_command(&cmd2);
		//Run the commands
		run_pipe_commands(&cmd1, &cmd2);
	}
	/*Check for in-built command 'exit'*/
	else if (!strcmp(line, "exit") || !strncmp(line, "exit ", 5)) {
		printf("Exiting...\n");
		exit(0);
	}
	/*Check for in-built command 'cd' - no arguments*/
	else if (!strcmp("cd", line) || !strcmp("cd ", line)) {
		chdir(getenv("HOME")); //Change to HOME directory
	}
	//Check for in-built command 'cd' with arguments
	else if (!strncmp("cd ", line, 3)) {
		char* cdDest = split_string(line, count_char(line, ' '), " ")[1];
		chdir(cdDest);
	}
	//Run command normally
	else {
		Command cmd;
		strcpy(cmd.cmdString, line);
		//Set up the command
		setup_command(&cmd);
		run_command(&cmd);
	}
}
예제 #15
0
파일: char.c 프로젝트: gaowenjing/C
//split one word to two function
sw split_word(char *word, int n)
{
	int wc = count_char(word);
	sw r;
	if (n >= 0) {
		r.left_word = shrink_char(word, n - wc);
		r.right_word = shrink_char(word, n);
	} else {
		r.right_word = shrink_char(word, n + wc);
		r.left_word = shrink_char(word, n);
	}
	return r;
}
예제 #16
0
파일: init_char.c 프로젝트: Alban95/epikong
int		init_monster(t_struct *st)
{
  int		i;
  int		size;

  size = count_char(st->file, 'm');
  if ((st->monster = malloc(sizeof(int) * (size + 1))) == NULL)
    return (-1);
  i = -1;
  while (++i < size)
    st->monster[i] = 1;
  return (0);
}
예제 #17
0
파일: zsh.c 프로젝트: rotarui/42sh
static void		check_pos(t_shell *shell)
{
  int			y;
  int			cc;
  struct winsize	ws;

  ioctl(1, TIOCGWINSZ, &ws);
  y = -1;
  cc = (count_char(&shell->com) / ws.ws_col);
  while (++y < cc)
    my_dprintf(1, "\n");
  gotoyx(shell, get_actual_pos(shell) - (1 + y), 0);
}
예제 #18
0
uint8_t			check_errors(const char *fmt)
{
	int	approx_args_size;

	approx_args_size = (count_char(fmt, '%') * INTERNAL_DATA_SIZE_MAX);
	if (fmt == NULL)
		ft_putendl_fd("fmt NULL", 2);
	if (ft_strlen(fmt) + approx_args_size > PRINTF_BUFFER_SIZE_MAX)
	{
		ft_putendl_fd("error: printf_basic: \
You might need to update DATA_SIZE_MAX or BUFFER_SIZE_MAX", 2);
		return (0);
	}
예제 #19
0
t_date      *ft_date_convert(char *str)
{
    char    **ret;
    t_date  *date;

    ret = ft_strsplit(str, ' ');
    if (count_char(str, ' ') != 1)
        return (NULL);
    if (count_char(str, '/') != 2)
        return (NULL);
    if (count_char(str, ':') != 2)
        return (NULL);
    if (ret != NULL && ret[1] != NULL && !ret[2])
    {
        if (!(date = date_create()))
            return (NULL);
        date_convert_time(ret[0], date);
        date_convert_date(ret[1], date);
        ft_tabstrdel(ret);
        return (date);
    }
    return (NULL);
}
예제 #20
0
파일: itsh.c 프로젝트: Ponty-/Past_Projects
int main(int argc, char* argv[]){
	char buf[MAXLINE + 3];//Extra 3 chars used for testing long lines
	FILE* input;
	
	if (argc > 2) {
		fprintf(stderr, "Usage: itsh [script]\n");
		exit(1);
	}
	//Set up read - either stdin or script file
	if (argc == 2) {
		//Open file
		input = fopen(argv[1], "r");
		if (!input) {
			fprintf(stderr, "File %s not found\n", argv[1]);
			exit(2);
		}
	} else {
		input = stdin;
	}
	
	//Set up signal handling
	//Ignore ctrl-c signals for parent and background children
	signal(SIGINT, SIG_IGN);
	//Use handler for child signal
	signal(SIGCHLD, background_handler);
	
	prompt();
	while (fgets(buf, MAXLINE + 3, input) != NULL) {
		if (strlen(buf) > 129) {
			//Line too long
			fprintf(stderr, "Line too long - input limited to %d characters.\n", MAXLINE);
			//Read until newline - finish off too long line
			do {
				fgets(buf, MAXLINE, input);
			} while (!count_char(buf, '\n'));
			prompt();
			continue;
		}
		//Cut off the newline character (for a properly sized command)
		buf[strlen(buf) - 1] = '\0';
		if (strlen(buf) > 0) {
			interpret_command(buf);
		}
		prompt();
	}
	
	return(0);
}
예제 #21
0
파일: key_arrows.c 프로젝트: 42rdavid/42sh
int				k_up(t_data *data, char key[KEY_LENGTH])
{
	if (ft_strcmp(key, "\x1b\x5b\x41\0\0\0") == 0)
	{
		if (data->history->prev != NULL)
		{
			clear_line(data);
			data->history = data->history->prev;
			data->nb_c = count_char(data->history->line);
			data->cursor = data->nb_c;
			free_line(data->last->line);
			data->last->line = copy_line(data->history->line);
		}
		return (1);
	}
	return (0);
}
예제 #22
0
파일: gravity.c 프로젝트: Alban95/epikong
int		gravity_monster(t_struct *st)
{
  t_pos         pos;
  int           i;
  int           size;

  size = count_char(st->file, 'm');
  i = 0;
  while (i < size)
    {
      pos = find_pos(st->tmap, i);
      if (pos.y != -42)
	check_m(st, pos);
      i++;
    }
  return (0);
}
예제 #23
0
파일: itsh.c 프로젝트: Ponty-/Past_Projects
/** setup_command(Command* cmd)
**		Take a command struct with the command line already set.
**		Set the arguments, redirects and whether to run in the background.
*/
void setup_command(Command* cmd) {
	cmd->redirectIn = NULL;
	cmd->redirectOut = NULL;
	cmd->background = 0;
	//Split command by spaces
	int numSpaces = count_char(cmd->cmdString, ' ');
	cmd->cmdArgs = split_string(cmd->cmdString, numSpaces, " ");
	//Get the input and output redirects, storing the index of the earliest arrow word
	int lastArg = numSpaces + 1;
	for (int i = 1; i < numSpaces + 1; i++) {
		if (!strcmp("<", cmd->cmdArgs[i])) {
			if (lastArg > i) {
				lastArg = i;
			}
			i++; //Increment 'i' to the file
			//Attempt to open the file for reading (input)
			cmd->redirectIn = fopen(cmd->cmdArgs[i], "r");
			//Check for errors
			if (!cmd->redirectIn) {
				perror("Redirect input error:");
			}
		} else if (!strcmp(">", cmd->cmdArgs[i])) {
			if (lastArg > i) {
				lastArg = i;
			}
			i++; //Increment 'i' to the file
			//Attempt to open the file for writing (output)
			cmd->redirectOut = fopen(cmd->cmdArgs[i], "w");
			//Check for errors
			if (!cmd->redirectOut) {
				perror("Redirect output error:");
			}
		}
	}
	//Get whether this is to be a background process
	if (!strcmp(cmd->cmdArgs[numSpaces], "&")){
		cmd->background = 1;
		
		if (lastArg > numSpaces) {
				lastArg = numSpaces;
		}
	}
	//Set up the argument list for execing (last element null)
	cmd->numArgs = lastArg;
}
예제 #24
0
파일: win_clip.c 프로젝트: LarBob/executor
PRIVATE int
calc_length_and_format (UINT *formatp, LONGINT type, LONGINT length,
			const char *p)
{
  int retval;

  switch (type)
    {
    case T ('T', 'E', 'X', 'T'):
      retval = length + count_char (p, length, '\r') + 1;
      *formatp = CF_TEXT;
      break;
    default:
      retval = length+4;
      *formatp = ROMlib_executor_format (type);
      break;
    }
  return retval;
}
예제 #25
0
static void parse_scales(char *s)
{
    int n = nscales + 1 + count_char(s, ',');

    g_scales = (float*)realloc(g_scales, n * sizeof(float));

    char *p = strsep(&s, ",");
    while (p) {
        float f;
        if (strlen(p) || !nscales) {
            f = atof(p);
        } else {
            f = g_scales[nscales - 1];
        }
        g_scales[nscales++] = f;

        p = strsep(&s, ",");
    }
}
예제 #26
0
static void parse_heights(char *s)
{
    int n = nheights + 1 + count_char(s, ',');

    g_heights = (int*)realloc(g_heights, n * sizeof(int));

    char *p = strsep(&s, ",");
    while(p) {
        int h;
        if (strlen(p) || !nheights) {
            h = atoi(p);
        } else {
            h = g_heights[nheights - 1];
        }
        g_heights[nheights++] = h;

        p = strsep(&s, ",");
    }
}
예제 #27
0
파일: char.c 프로젝트: gaowenjing/C
//delete specific character base on number order (n) 
//1 means the first character ,-1 means backward first character
char *shrink_char(char *word, int n)
{
	int wc = count_char(word);
	if (wc < abs(n))
		error(1, 0, "out of range");
	if (n >= 0)
		return word + n;
	else {
		char *new_word = malloc(sizeof(word));
		int i;
		for (i = 0; i < (wc + n); i++)
			*(new_word + i) = *(word + i);
//note: pointer out of range will cause pointer disappear
//              printf ( "%p\n",new_word );
//              printf ( "%c\n",*(new_word+3) );
//              printf ( "%p\n",new_word );
		return new_word;
	}
}
예제 #28
0
static char *
local_quote_string (const char *file)
{
  const char *file_sans_qmark;
  int qm;

  if (!opt.html_extension)
    return html_quote_string (file);

  qm = count_char (file, '?');

  if (qm)
    {
      const char *from = file;
      char *to, *newname;

      /* qm * 2 because we replace each question mark with "%3F",
	 i.e. replace one char with three, hence two more.  */
      int fsqlen = strlen (file) + qm * 2;

      to = newname = (char *)alloca (fsqlen + 1);
      for (; *from; from++)
	{
	  if (*from != '?')
	    *to++ = *from;
	  else
	    {
	      *to++ = '%';
	      *to++ = '3';
	      *to++ = 'F';
	    }
	}
      assert (to - newname == fsqlen);
      *to = '\0';

      file_sans_qmark = newname;
    }
  else
    file_sans_qmark = file;

  return html_quote_string (file_sans_qmark);
}
예제 #29
0
int main()
{
	char a[1024];
	
	int b[256], i = 0;

	while(gets(a) && a[0] != '\0')
	{
		bzero(b, 256 * sizeof(int));
		count_char(a, b);
		i = 0;
		while(i < 256)
		{
			if(b[i])
				printf("%c %d times\n", i, b[i]);
			i++;
		}
	}


	

/*	char a[] = "abc";
	part_reverse(a);
	puts(a);
	char a[N] = "123";
	int m = char_to_int(a);
	printf("%d\n", m);


	int n = 123;
	char b[N];
	bzero(b, N);
	int_to_char(n, b);
	puts(b);

*/

	return 0;
}
예제 #30
0
void					put_in_array(std::string const & str)
{
	int								**tab;
	std::istringstream				is(str);
	int								pos = str.find_first_of('\n');
	unsigned int					scale = count_char(str.substr(0, pos));

	tab = new int*[scale];
	for (unsigned int i = 0; i < scale; ++i)
	{
		tab[i] = new int[scale];
		for(unsigned int j = 0; j < scale; ++j)
			is >> tab[i][j];
	}

	for (unsigned int i = 0; i < scale; ++i)
	{
		for (unsigned int j = 0; j < scale; ++j)
			std::cout << tab[i][j] << "\t";
		std::cout << std::endl;
	}

}