Ensure(Words, converts_spaces_to_zeroes) { char *sentence = strdup("Birds of a feather"); split_words(sentence); int comparison = memcmp("Birds\0of\0a\0feather", sentence, strlen(sentence)); assert_that(comparison, is_equal_to(0)); free(sentence); }
int main(void) { char *str = NULL; char **words = NULL; int words_count = 0, i; char *delim = " ,.?!"; str = gets_str(10, 5); if (NULL == str) return 1; words = split_words(str, &words_count, delim); if (NULL == words) { free(str); return 1; } show_words(words, words_count); free(str); for(i = 0; i < words_count; i++) free(words[i]); free(words); return 0; }
void test_func() { // analy_config_music(); char *tmp_splited_words[BUFSIZ]; int num = split_words(recv_buf, tmp_splited_words, BUFSIZ); int i = 0; for(i = 0; i < num; i++) { free(tmp_splited_words[i]); } printf("____\n"); }
void words(const char *sentence, void (*walker)(const char *, void *), void *memo) { char *words = strdup(sentence); int word_count = split_words(words); char *word = words; while (word_count-- > 0) { (*walker)(word, memo); word = word + strlen(word) + 1; } free(words); }
list *str_to_wordlists(const char *s) { size_t len = cgc_strlen(s); char *x = calloc(1, len + 1); strncpy(x, s, len); x[len] = '\0'; list *words = split_words(x, 1); if (!words) return NULL; free(x); #define CHAIN_LENGTH 2 list *chunks = chunk_words(words, CHAIN_LENGTH); if (!chunks) return NULL; return chunks; }
/** * Handle any one of the supported commands * say ill-formed command otherwise. */ void handle_main_command(char *command) { switch (command[0]) { case 'e': fprintf(stdout, "creating new interactive client\n"); create_client(); break; case 'E': { // Allocate a space on the stack to // throw a pointer to the words char **words = NULL; words = split_words(command); int num_words; // Count up the number of words returned for (num_words = 0; words[num_words] != NULL; ++num_words); // If it isn't 3 or 2 say it is an ill formed command and do nothing. // We allow 2 because the built in function allows the second argument // to be null if we just want to redirec client output to stdout if ((num_words != 3) && (num_words != 2)) { fprintf(stdout, "ill-formed command\n"); return; } // If it worked announce what we're doing fprintf(stdout, "creating new non interactive client from %s to %s\n", words[1], words[2]); create_non_interactive_client(words[1], words[2]); // Make sure we don't leak memory free_words(words); } break; case 's': fprintf(stdout, "pausing clients\n"); pause_clients(); break; case 'g': fprintf(stdout, "unpausing clients\n"); unpause_clients(); break; case 'w': fprintf(stdout, "waiting for clients to exit\n"); // Make sure the clients are unpaused unpause_clients(); wait_for_clients_to_exit(); fprintf(stdout, "now ready to take additional commands\n"); break; case 't': // Start the timer fprintf(stdout, "starting timer\n"); start_timer(); break; case 'T': { // End the timer float time_elapsed = end_timer(); fprintf(stdout, "%f seconds have elapsed since the timer started\n", time_elapsed); break; } default: fprintf(stdout, "ill-formed command\n"); break; } }
/* read the file with all the pic14 definitions and pick out the definition * for a processor if specified. if pic_name is NULL reads everything */ static PIC_device * find_device (char *pic_name) { FILE *pic_file; char pic_buf[PIC14_STRING_LEN]; int found_processor = FALSE; int done = FALSE; char **processor_name; int num_processor_names = 0; int pic_maxram = 0; int pic_bankmsk = 0; int pic_confsiz = 0; int pic_program = 0; int pic_data = 0; int pic_eeprom = 0; int pic_io = 0; char *simple_pic_name; char *dir; char filename[512]; int len = 512; char **pic_word; int num_pic_words; int wcount; pic_word = Safe_calloc (sizeof (char *), SPLIT_WORDS_MAX); processor_name = Safe_calloc (sizeof (char *), SPLIT_WORDS_MAX); /* allow abbreviations of the form "f877" - convert to "16f877" */ simple_pic_name = sanitise_processor_name (pic_name); num_of_supported_PICS = 0; /* open the piclist file */ /* first scan all include directories */ pic_file = NULL; //fprintf (stderr, "%s: searching %s\n", __FUNCTION__, DEVICE_FILE_NAME); for (dir = setFirstItem (userIncDirsSet); !pic_file && dir; dir = setNextItem (userIncDirsSet)) { //fprintf (stderr, "searching1 %s\n", dir); SNPRINTF (&filename[0], len, "%s%s", dir, DIR_SEPARATOR_STRING DEVICE_FILE_NAME); pic_file = fopen (filename, "rt"); if (pic_file) break; } // for for (dir = setFirstItem (includeDirsSet); !pic_file && dir; dir = setNextItem (includeDirsSet)) { //fprintf (stderr, "searching2 %s\n", dir); SNPRINTF (&filename[0], len, "%s%s", dir, DIR_SEPARATOR_STRING DEVICE_FILE_NAME); pic_file = fopen (filename, "rt"); if (pic_file) break; } // for for (dir = setFirstItem (libDirsSet); !pic_file && dir; dir = setNextItem (libDirsSet)) { //fprintf (stderr, "searching3 %s\n", dir); SNPRINTF (&filename[0], len, "%s%s", dir, DIR_SEPARATOR_STRING DEVICE_FILE_NAME); pic_file = fopen (filename, "rt"); if (pic_file) break; } // for for (dir = setFirstItem (libPathsSet); !pic_file && dir; dir = setNextItem (libPathsSet)) { //fprintf (stderr, "searching4 %s\n", dir); SNPRINTF (&filename[0], len, "%s%s", dir, DIR_SEPARATOR_STRING DEVICE_FILE_NAME); pic_file = fopen (filename, "rt"); if (pic_file) break; } // for if (!pic_file) { SNPRINTF (&filename[0], len, "%s", DATADIR LIB_DIR_SUFFIX DIR_SEPARATOR_STRING "pic" DIR_SEPARATOR_STRING DEVICE_FILE_NAME); pic_file = fopen (filename, "rt"); } // if if (pic_file == NULL) { fprintf (stderr, "can't find %s\n", DEVICE_FILE_NAME); return NULL; } // if if (options.verbose) printf ("Using devices from %s.\n", filename); /* read line by line */ pic_buf[sizeof (pic_buf)-1] = '\0'; while (fgets (pic_buf, sizeof (pic_buf)-1, pic_file) != NULL && !done) { /* strip comments */ { char *comment = strchr (pic_buf, '#'); if (comment) *comment = 0; } /* split into fields */ num_pic_words = split_words (pic_word, pic_buf); /* ignore comment / empty lines */ if (num_pic_words > 0) { if (STRCASECMP (pic_word[0], "processor") == 0) { if (pic_name == NULL) { int dcount; /* this is the mode where we read all the processors in - store the names for now */ if (num_processor_names > 0) { /* store away all the previous processor definitions */ for (dcount = 1; dcount < num_processor_names; dcount++) { create_pic (processor_name[dcount], pic_maxram, pic_bankmsk, pic_confsiz, pic_program, pic_data, pic_eeprom, pic_io); } // for } // if /* copy processor names */ num_processor_names = num_pic_words; for (dcount = 1; dcount < num_processor_names; dcount++) { processor_name[dcount] = pic_word[dcount]; pic_word[dcount] = NULL; } // for } // if else { /* if we've just completed reading a processor definition stop now */ if (found_processor) done = TRUE; else { /* check if this processor name is a match */ for (wcount = 1; wcount < num_pic_words; wcount++) { /* skip uninteresting prefixes */ char *found_name = sanitise_processor_name (pic_word[wcount]); if (STRCASECMP (found_name, simple_pic_name) == 0) found_processor = TRUE; } // for } // if } // if } // if else { if (found_processor || pic_name == NULL) { /* only parse a processor section if we've found the one we want */ if (STRCASECMP (pic_word[0], "maxram") == 0 && num_pic_words > 1) { pic_maxram = parse_config_value (pic_word[1]); setMaxRAM (pic_maxram); } // if else if (STRCASECMP (pic_word[0], "bankmsk") == 0 && num_pic_words > 1) pic_bankmsk = parse_config_value (pic_word[1]); else if (STRCASECMP (pic_word[0], "confsiz") == 0 && num_pic_words > 1) pic_confsiz = parse_config_value (pic_word[1]); else if (STRCASECMP (pic_word[0], "program") == 0 && num_pic_words > 1) pic_program = parse_config_value (pic_word[1]); else if (STRCASECMP (pic_word[0], "data") == 0 && num_pic_words > 1) pic_data = parse_config_value (pic_word[1]); else if (STRCASECMP (pic_word[0], "eeprom") == 0 && num_pic_words > 1) pic_eeprom = parse_config_value (pic_word[1]); else if (STRCASECMP (pic_word[0], "io") == 0 && num_pic_words > 1) pic_io = parse_config_value (pic_word[1]); else if (STRCASECMP (pic_word[0], "regmap") == 0 && num_pic_words > 2) { if (found_processor) register_map (num_pic_words, pic_word); } // if else if (STRCASECMP (pic_word[0], "memmap") == 0 && num_pic_words > 2) { if (found_processor) ram_map (num_pic_words, pic_word); } // if else { fprintf (stderr, "WARNING: %s: bad syntax `%s'\n", DEVICE_FILE_NAME, pic_word[0]); } // if } // if } // if } // if } // while fclose (pic_file); split_words (pic_word, NULL); free (pic_word); /* if we're in read-the-lot mode then create the final processor definition */ if (pic_name == NULL) { if (num_processor_names > 0) { /* store away all the previous processor definitions */ int dcount; for (dcount = 1; dcount < num_processor_names; dcount++) { create_pic (processor_name[dcount], pic_maxram, pic_bankmsk, pic_confsiz, pic_program, pic_data, pic_eeprom, pic_io); } // for } // if } // if else { /* in search mode */ if (found_processor) { split_words (processor_name, NULL); free (processor_name); /* create a new pic entry */ return create_pic (pic_name, pic_maxram, pic_bankmsk, pic_confsiz, pic_program, pic_data, pic_eeprom, pic_io); } // if } // if split_words (processor_name, NULL); free (processor_name); return NULL; }
Ensure(Words, returns_word_count) { char *sentence = strdup("Birds of a feather"); int word_count = split_words(sentence); assert_that(word_count, is_equal_to(4)); free(sentence); }
/* Fill the sentence with new words: in "page" mode, fills the page with text; in "scroll" mode, just makes one long horizontal sentence. The sentence might have *no* words in it, if no text is currently available. */ static void populate_sentence (state *s, sentence *se) { int i = 0; int left, right, top, x, y; int space = 0; int line_start = 0; Bool done = False; int array_size = 100; se->move_chars_p = (s->mode == SCROLL ? False : (random() % 3) ? False : True); se->alignment = (random() % 3); recolor (s, se); if (se->words) { for (i = 0; i < se->nwords; i++) free_word (s, se->words[i]); free (se->words); } se->words = (word **) calloc (array_size, sizeof(*se->words)); se->nwords = 0; switch (s->mode) { case PAGE: left = random() % (s->xgwa.width / 3); right = s->xgwa.width - (random() % (s->xgwa.width / 3)); top = random() % (s->xgwa.height * 2 / 3); break; case SCROLL: left = 0; right = s->xgwa.width; top = random() % s->xgwa.height; break; default: abort(); break; } x = left; y = top; while (!done) { char *txt = get_word_text (s); word *w; if (!txt) { if (se->nwords == 0) return; /* If the stream is empty, bail. */ else break; /* If EOF after some words, end of sentence. */ } if (! se->font) /* Got a word: need a font now */ { pick_font (s, se); if (y < se->font->ascent) y += se->font->ascent; space = XTextWidth (se->font, " ", 1); } w = new_word (s, se, txt, !se->move_chars_p); /* If we have a few words, let punctuation terminate the sentence: stop gathering more words if the last word ends in a period, etc. */ if (se->nwords >= 4) { char c = w->text[strlen(w->text)-1]; if (c == '.' || c == '?' || c == '!') done = True; } /* If the sentence is kind of long already, terminate at commas, etc. */ if (se->nwords >= 12) { char c = w->text[strlen(w->text)-1]; if (c == ',' || c == ';' || c == ':' || c == '-' || c == ')' || c == ']' || c == '}') done = True; } if (se->nwords >= 25) /* ok that's just about enough out of you */ done = True; if (s->mode == PAGE && x + w->rbearing > right) /* wrap line */ { align_line (s, se, line_start, x, right); line_start = se->nwords; x = left; y += se->font->ascent; /* If we're close to the bottom of the screen, stop, and unread the current word. (But not if this is the first word, otherwise we might just get stuck on it.) */ if (se->nwords > 0 && y + se->font->ascent > s->xgwa.height) { unread_word (s, w); /* done = True; */ break; } } w->target_x = x + w->lbearing; w->target_y = y - w->ascent; x += w->rbearing + space; se->width = x; if (se->nwords >= (array_size - 1)) { array_size += 100; se->words = (word **) realloc (se->words, array_size * sizeof(*se->words)); if (!se->words) { fprintf (stderr, "%s: out of memory (%d words)\n", progname, array_size); exit (1); } } se->words[se->nwords++] = w; } se->width -= space; switch (s->mode) { case PAGE: align_line (s, se, line_start, x, right); if (se->move_chars_p) split_words (s, se); scatter_sentence (s, se); shuffle_words (s, se); break; case SCROLL: aim_sentence (s, se); break; default: abort(); break; } # ifdef DEBUG if (s->debug_p) { fprintf (stderr, "%s: sentence %d:", progname, se->id); for (i = 0; i < se->nwords; i++) fprintf (stderr, " %s", se->words[i]->text); fprintf (stderr, "\n"); } # endif }