示例#1
0
/** Reads each character of the file into a buffer if they are all valid
    @param fp the input file
    @return the buffer of the entire file
 */
char *readFile( FILE *fp )
{ 
  int capacity = 5;
  char *buffer = malloc( capacity );

  int len = 0;
  
  char a;
  while( !feof( fp ) ) {
    fscanf( fp, "%c", &a );
    if ( validChar( a ) ) {
      if ( capacity == ( len + 1 ) ) {
        capacity += capacity;
        buffer = ( char* )realloc( buffer, capacity );
        buffer[ capacity ] = '\0';
      }
      buffer[ len ] = a;
      len++;
    } else {
      fprintf( stderr, "usage: pack <input.txt> <compressed.raw> [wordfile.txt]\n" );
      exit( 1 );
    }
  }
  return buffer;
}
示例#2
0
文件: console.c 项目: KoWu/atxos
//reads a max 32 char string from uart, supporting delete chars.
static char* readString(void)
{
	static char rx[33];
	char tmp;
	BYTE i = 0;

	while((tmp = uart_getc()) != '\n' && tmp != '\r') {
		if (tmp == 0x7f) {
			if (i > 0) {
				rx[--i] = 0;
				uart_putc(tmp);
			}
			continue;
		}
		if (i < sizeof(rx) - 1) {
			if (validChar(tmp)) {
				rx[i++] = tmp;
				uart_putc(tmp);
			}
		}
	}
	rx[i] = 0;
	
	return rx;
}
示例#3
0
文件: ttt.c 项目: johnjones4/DinoDOS
/*
 * Start the game play. This function is terminated when the board is filled, or
 * when a player has won.
 */
void play()
{
	//X goes first
	char turn = 'X';
	//current number of moves
	int moves = 0;
	
	//draw the board
	initializeGame();
	//go until we find a winner or the board is filled
	while(!winner() && moves < 9) {
		//the current user input
		char c;
		int row, col;
		//wait until we get a valid charter
		while(!validChar(c = getchar()));
		
		//map the character to a row and column
		if(c == 'q' || c == 'w' || c == 'e') {
			//first row
			row = 0;
		} else if(c == 'a' || c == 's' || c == 'd') {
			//second row
			row = 1;
		} else {
			//third row
			row = 2;
		}
		//column check
		if(c == 'q' || c == 'a' || c == 'z') {
			//first col
			col = 0;
		} else if(c == 'w' || c == 's' || c == 'x') {
			//second col
			col = 1;
		} else {
			//third col
			col = 2;
		}
		
		//check if that space is free, otherwise loop goes again
		if (grid[row][col] == '\0') {
			//mark that spot
			grid[row][col] = turn;
			//draw it an change turns
			if (turn == 'X') {
				drawX(row,col);
				turn = 'O';
			} else if (turn == 'O') {
				drawO(row,col);
				turn = 'X';
			}
			//add the the move count
			moves++;
		}
	}
	//after the game is over, wait for the user to press any key to end
	getchar();
}
示例#4
0
WordList *readWordList( char const *fname )
{
  FILE* f = fopen(fname, "r");
  WordList* list = malloc(sizeof(WordList));
  list->capacity = (WORD_MAX + 1);
  list->words = malloc(sizeof(Word) * list->capacity);

  // Number of characters we're currently using.
  list->len = 0;

  int wordLen;
  char s[WORD_MAX+1]; // = {wordLen = '\0'};
  for (int i = 0; fscanf(f, "%d", &wordLen) == 1; i++) {
    fgetc(f); //skip space after the number

    if (i >= MAX_WORDS) {
      fprintf(stderr, "Invalid word file\n");
      exit(EXIT_FAILURE);
    }

    if (wordLen > WORD_MAX || wordLen < WORD_MIN) {
      fprintf(stderr, "Invalid word file\n");
      exit(EXIT_FAILURE);
    }

    for (int i = 0; i < wordLen; i++) {
      fscanf(f, "%c", &(s[i]));
      if (!validChar(s[i])){
        fprintf(stderr, "Invalid character code: %x\n", (unsigned char)s[i]);
        exit(EXIT_FAILURE);
      }
    }
    s[wordLen] = '\0';
    strcpy(list->words[list->len], s);
    list->len++;

    if (list->len >= list->capacity / 2){
      list->capacity *= 2;
      list->words = realloc(list->words, sizeof(Word) * list->capacity + 2);
    }

  }

  addHardCodes(list);

  qsort(list->words, list->len, sizeof(Word), myComp);

  fclose(f);

  return list;

}
示例#5
0
int verifyTokens(struct tokenList* tokens)
{
	int i, flag = 0;
	struct tokenElement* node = tokens->first;

	for (i = 0; i < tokens->qnt; i++)
	{
		if (node->token->text[0] == '\'')
		{
			if (validChar(node->token))
				node->token->code = tokenToCode(node->token->text,'c');
			else
				flag = 1;
		}
		else if (node->token->text[0] == '\"')
		{
			if (validString(node->token))
				node->token->code = tokenToCode(node->token->text,'s');
			else
				flag = 1;
		}
		else if (isalpha(node->token->text[0]))
			node->token->code = tokenToCode(node->token->text,'i');
		else if (isdigit(node->token->text[0]))
		{
			if (validNumber(node->token))
				node->token->code = tokenToCode(node->token->text,'n');
			else
				flag = 1;
		}
		else if (validSeparator(node->token))
			node->token->code = tokenToCode(node->token->text,'t');
		else
			flag = 1;

		node = node->next;
	}

	if (!flag)
		return 1;
	else
		return 0;
}
示例#6
0
/**
  This function is responsable for building the word list. It reads words form word file
  given as fname. After reading all the words from the word file, it adds single-character
  words for each of the 98 valid characters. Finally, it sorts the word list lexicographically
  so that the index of each word is its code.

  @fname - name of the file that contains all lists from the word
  @return - Lists of words from fname, resorted.
*/
WordList *readWordList( char const *fname ){
  //initialize WordList
  WordList *wordList = (WordList *)malloc( sizeof ( Word ) );
  wordList->capacity = 1;
  wordList->len = 0;
  wordList->words = ( Word * )malloc( sizeof( wordList->capacity * sizeof( Word ) ) );

  //Opens file
  FILE *fp;
  if ( ( fp = fopen( fname, "r" ) ) == NULL  ){
    fprintf(stderr, "Invalid word file\n" );
    exit( EXIT_FAILURE );
  }

  //stores char from file before moving into words
  char nextWord[ WORD_MAX + 1 ];

  //Length of word in the file
  int length;

  //Counts the number of words that have been added to the file
  int count = 0;

  //Loop that runs through file
  while ( fscanf( fp, "%d", &length ) == 1 ){
    //space between number and word
    char ch = fgetc( fp );

    //Produces error if the word length is longer than 20
    if ( length > WORD_MAX ){
      fprintf(stderr, "Invalid word file\n");
      exit( EXIT_FAILURE );
    }

    //Produces error if there is more than 414 words in the list
    if ( count > LIST_MAX ){
      fprintf(stderr, "Invalid word file\n");
      exit( EXIT_FAILURE );
    }

    //Checks the characters in the word before adding the word to the file
    int i = 0;
    //Loop that runs through each char in the word
    while ( i < length ){
      ch = fgetc( fp );

      //Produces error if char is not in acceptable range
      if ( !validChar( ch ) ){
        fprintf(stderr, "Invalid word file\n");
        exit( EXIT_FAILURE );
      }

      //Adds char to new word, which will be added to the file
      nextWord[ i ] = ch;
      i++;
    }

    //Add null terminator to end of word
    nextWord[ length ] = '\0';

    //Words count
    count ++;

    //Adds word to the total list of words
    addWord( wordList, nextWord );
  }

  //Adds additional chars not included in the file list
  //add chars 0x20 - 0x7e
  for ( char i = '!'; i <= '~'; i++ ){
    strcpy( nextWord, &i );
    nextWord[ 1 ] = '\0';
    addWord( wordList, nextWord );
    count ++;
  }

  //add tab
  strcpy( nextWord, "\t");
  addWord( wordList, nextWord );

  //add new line
  strcpy( nextWord, "\n");
  addWord( wordList, nextWord );

  //add r
  strcpy( nextWord, "\r");
  addWord( wordList, nextWord );

  //add space
  strcpy( nextWord, " ");
  addWord( wordList, nextWord );

  //sort list lexographically
  qsort( (int *) wordList->words[0], wordList->len, sizeof ( Word ),
       (int (*)(const void*, const void*) ) strcmp);

  //Close file
  fclose(fp);

  return wordList;
}
示例#7
0
/**
 * This function is responsible for building the word list. It reads words from a word file 
 * given as fname. Before reading all the words from the word file, it adds single-character 
 * words for each of the 98 valid characters. Finally, it sorts the word list lexicographically 
 * so that the index of each word is its code.
 *
 * @param char const *fname - file name for the word list file
 * @return Wordlist *list - a pointer to the word list
 */
WordList *readWordList( char const *fname )
{
  FILE *fp;
  WordList *list = (WordList *) malloc( sizeof( WordList ) );
  if(( fp = fopen( fname, "r" )) == NULL ){
    fprintf(stderr, "Can't open word file: %s\n", fname);
    exit( EXIT_FAILURE );
  }
  
  list->capacity = INITIAL_CAPACITY;
  list->len = 0;
  list->words = (Word *)malloc( (list->capacity + 1) * sizeof(Word));
  
  // Set to negative value for EOF checking with dangling chars
  int wordLength = -1;
  char wordString[WORD_MAX + 1] = {'\0'};
  char temp = ' ';
  
  addValidChars(list);

  while ( !feof(fp) ) {
    // Get word length
    fscanf(fp, "%d", &wordLength);
    
    // If word length is still -1 at this point, the word file has characters left over (possibly just a new line) such that
    // EOF was not detected. If this is the case then break out of the while loop.
    if( wordLength == -1){
      break;
    }
    
    // Check whether length is within the allowed range for strings in the word list.
    if(wordLength <= WORD_MAX && wordLength >= WORD_MIN){    
     
      // Skip a single space
      getc(fp);
     
      // Get the word and check if each character is valid.
      for(int j = 0; j < wordLength; j++){       
        temp = getc(fp);
        if(validChar(temp)){
          wordString[j] = temp;
        } else {
          fprintf(stderr, "Invalid word file\n");
          exit( EXIT_FAILURE );
        }
      }
      
      // Check if enough memory is allocated, if not then resize the word list.
      if ( list->len >= list->capacity ) {
        list->capacity *= 2;
        Word *newList = (Word *)malloc( (list->capacity + 1)* sizeof( Word ) );
        memcpy( newList, list->words, list->len * sizeof( Word ) );
        free( list->words );
        list->words = newList;
      }
      
      // Copy the temporary string into a word in the word list.
      strcpy(list->words[list->len], wordString);
      list->len++;
      
      // If there are too many words in the list then print an error.
      if(list->len > MAX_LISTLEN){
        fprintf(stderr, "Invalid word file\n");
        exit(EXIT_FAILURE);
      }
    } else {
      fprintf(stderr, "Invalid word file\n");
      exit(EXIT_FAILURE);
    }
      
    // Reset temporary string and length variables.
    for(int k = 0; k < WORD_MAX; k++){
      wordString[k] = '\0';
    }
    wordLength = -1;    
  }
  
  //Sort the word list
  qsort(list->words, list->len, sizeof( Word ), compareWords) ;
  
  //Close the word file
  fclose(fp);
  
  return list;
}