示例#1
0
void
ppmd_read_font(FILE *                        const ifP,
               const struct ppmd_font **     const fontPP) {

    unsigned int relativeCodePoint;
    struct ppmd_glyph * glyphTable;
    struct ppmd_font * fontP;

    MALLOCVAR(fontP);
    if (fontP == NULL)
        pm_error("Insufficient memory for font header");

    readFontHeader(ifP, &fontP->header);

    MALLOCARRAY(glyphTable, fontP->header.characterCount);
    if (glyphTable == NULL)
        pm_error("Insufficient memory to store %u characters",
                 fontP->header.characterCount);

    for (relativeCodePoint = 0;
         relativeCodePoint < fontP->header.characterCount;
         ++relativeCodePoint) {

        readCharacter(ifP, &glyphTable[relativeCodePoint]);
    }
    fontP->glyphTable = glyphTable;
    *fontPP = fontP;
}
示例#2
0
int main(int argc, char* argv[])
{
    char letter = 0; // 存储用户输入的字母
    char secretWord[100] = {0}; // 要猜测的单词
    int *letterFound = NULL; // 布尔值的数组. 数组的每一个元素对应猜测单词的一个字母。 0 = 还没猜到此字母, 1 = 已猜到字母
    int leftTimes = 7; // 剩余猜测次数 (0 = 失败)
    int i = 0; // 下标
    long wordSize = 0;  // 单词的长度(字母数目)

    printf("欢迎来到悬挂小人游戏!\n");

    // 从词库(文件dictionary)中随机选取一个单词
    if (!chooseWord(secretWord))
      exit(0); // 退出游戏

    // 获取单词的长度
    wordSize = strlen(secretWord);

    letterFound = malloc(wordSize * sizeof(int)); // 动态分配数组的大小,因为我们一开始不知道单词长度
    if (letterFound == NULL)
      exit(0);

    // 初始化布尔值数组,都置为0,表示还没有字母被猜到
    for (i = 0 ; i < wordSize ; i++)
      letterFound[i] = 0;

    // 主while循环,如果还有猜测机会并且还没胜利,继续
    while(leftTimes > 0 && !win(letterFound, wordSize)){
      printf("\n\n您还剩 %d 次机会", leftTimes);
      printf("\n神秘单词是什么呢 ? ");

      /* 我们显示猜测的单词,将还没猜到的字母用*表示
        例如 : *O**LE */
      for (i = 0 ; i < wordSize ; i++)
      {
	if (letterFound[i]) // 如果第i+1个字母已经猜到
	  printf("%c", secretWord[i]); // 打印出来
	else
	  printf("*"); // 还没猜到,打印一个*
      }

      printf("\n输入一个字母 : ");
      letter = readCharacter();

      // 如果用户输入的字母不存在于单词中
      if (!researchLetter(letter, secretWord, letterFound))
      {
	leftTimes--; // 将剩余猜测机会数减1
      }
    }

    if (win(letterFound, wordSize))
      printf("\n\n胜利了! 神秘单词是 : %s\n", secretWord);
    else
      printf("\n\n失败了! 神秘单词是 : %s\n", secretWord);

    return 0;
}
示例#3
0
TStdStringCharacter CParcerFile::goOneCharacterBack()
{
  if(fseek(m_file, ftell(m_file)-(sizeof(TStdStringCharacter)*2), SEEK_SET) == -1)
  {
    m_error = true;
    MON_PRINT_FILEOP_ERRNO(m_filename, "Seek file error");
    return 0;
  }
  return readCharacter();
}
示例#4
0
BerthaBuffer BerthaIO::nextRequest() {
  while (true) {
    if (characterAvailable()) {
      char ch = readCharacter();
      if (endOfLine(ch)) {
        return request;
      } else {
        request.append(ch);
      }
    }
  }
}
示例#5
0
int readData(char*& buff, Number& num, bool& isInputIncorrect)
{
   char c;
   if( readCharacter(num, buff, c, isInputIncorrect) )
   {
      if(c == END_PROGRAM || isInputIncorrect)
      {
         return 0;
      }
      readData(buff, num, isInputIncorrect);
   }
}
示例#6
0
int main(int argc, char const *argv[])
{
	if (argc != 4)
	{
		printf("incorrect number of parameters\n");
		return 0;
	}

	if (strlen(argv[3]) != 1)
	{
		printf("invalid parameter <%s>, must be a char\n",argv[3]);
		return 0;
	}

	//get parameters
	const char* _path = argv[2];
	char _input = argv[3][0];
	int _nChar = 0;

	//open file
	int _fd = open(_path,O_RDONLY);
	if (_fd == -1)
	{
		printf("File doesn't exists or is unaccesible\n");
		return 0;
	}

	//parse mode
	if (strcmp(argv[1],"R") == 0) // call to readchar in a loop
	{
		int i;
		int _fileSize = lseek(_fd,0,SEEK_END);
		for (i=0; i < _fileSize; ++i)
		{
			if (readCharacter(_fd,i) == _input)
				_nChar++;
		}
	}else if (strcmp(argv[1],"M") == 0)//call to countM
	{
		_nChar = countCharacters(_fd,_input);
	}else
	{
		printf("Parameter <%s> unknown\n",argv[1]);
		return 0;
	}

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

	if (close(_fd)!=0)
		perror("Problem closing file");

	return 0;
}
示例#7
0
文件: HW5.c 项目: banga/COMP530
/* 
 * Entry point for process that replaces '**' with '^'
 * It first forks to start the printLines process
 */
void processAsterisks(int fd_read, int) {
	int fd_write, dummy;
	char c, c2;

	pid_t child_pid = fork_and_pipe(printLines, &dummy, &fd_write);

	while(readCharacter(fd_read, &c)) {
		if(c == '*') {
			if(readCharacter(fd_read, &c2)) {
				if(c2 == '*') {
					c = '^';
				} else {
					writeCharacter(fd_write, c);
					c = c2;
				}
			}
		}
		writeCharacter(fd_write, c);
	}
	close(fd_write);

	waitpid(child_pid, NULL, 0);
}
示例#8
0
文件: HW5.c 项目: banga/COMP530
/* 
 * Entry point for process that generates 80 character lines 
 */
void printLines(int fd_read, int) {
	char c, buf[LINE_SIZE+1];
	int count = 0;

	buf[LINE_SIZE] = NULL;

	while(readCharacter(fd_read, &c)) {
		buf[count++] = c;
		if(count == LINE_SIZE){
			printf("%s\n", buf);
			count = 0;
		}
	}
}
示例#9
0
文件: HW5.c 项目: banga/COMP530
/* 
 * Entry point for process that replaces newlines with spaces
 * It first forks to start the processAsterisks process
 */
void processNewlines(int fd_read, int) {
	int fd_write, dummy;
	char c;

	pid_t child_pid = fork_and_pipe(processAsterisks, &dummy, &fd_write);

	while(readCharacter(fd_read, &c)) {
		if(c == '\n')
			c = ' ';

		writeCharacter(fd_write, c);
	}
	close(fd_write);

	waitpid(child_pid, NULL, 0);
}
 inline bool
 AsciiXmlParser::recordTo( std::string& recordingString,
                           char const endChar )
 /* this records characters from textStream by appending them to
  * recordingStream, up to the 1st instance of endChar. it returns false if
  * no further instances of endChar were found in textStream.
  */
 {
   while( readCharacter()
          &&
          endChar != currentChar )
   {
     recordingString.append( 1,
                             currentChar );
   }
   return streamIsGood;
 }
 inline bool
 AsciiXmlParser::discardToNextMarkup()
 /* this skips characters up to the next XML markup opening character, then
  * the characters up to the next markup closing character are stored in
  * markupString. this also records the number of newline characters up to
  * this markup.
  */
 {
   while( readCharacter()
          &&
          ( markupOpener != currentChar ) )
   {
     // the conditional does the work of reading up to the next tag.
   }
   newlinesBeforeMarkup = readNewlines;
   return ( streamIsGood
            &&
            closeMarkup() );
 }
示例#12
0
Token DefaultLexer::readToken() {
  char c = lookChar();

  while (true) {
    // skip whitespace
    while (isWhiteSpace(c)) {
      skipChar();
      c = lookChar();
    }

    // newlines
    if (isNewline(c)) {
      readNewline(c);
      if (interactive_ && getCurrentBraceNesting() == 0)
        return Token(TK_Newline);
      c = lookChar();
      continue;
    }

    // treat line comments as newlines
    if (c == '/' && lookChar(1) == '/') {
      readLineComment();
      c = lookChar();
      continue;
    }
    break;
  }

  SourceLocation sloc = getCurrentLocation();

  // punctuation
  if (c == '(') {
    skipChar();
    signalOpenBrace(TK_LParen);
    return Token(TK_LParen, "(", sloc);
  }
  if (c == ')') {
    skipChar();
    signalCloseBrace(TK_LParen);
    return Token(TK_RParen, ")", sloc);
  }
  if (c == '{') {
    skipChar();
    signalOpenBrace(TK_LCurlyBrace);
    return Token(TK_LCurlyBrace, "{", sloc);
  }
  if (c == '}') {
    skipChar();
    signalCloseBrace(TK_LCurlyBrace);
    return Token(TK_RCurlyBrace, "}", sloc);
  }
  if (c == '[') {
    skipChar();
    signalOpenBrace(TK_LSquareBrace);
    return Token(TK_LSquareBrace, "[", sloc);
  }
  if (c == ']') {
    skipChar();
    signalCloseBrace(TK_LSquareBrace);
    return Token(TK_RSquareBrace, "]", sloc);
  }
  if (c == ',') {
    skipChar();
    return Token(TK_Comma, ",", sloc);
  }
  if (c == ';') {
    skipChar();
    return Token(TK_Semicolon, ";", sloc);
  }
  if (c == ':' && !isOperatorChar(lookChar(1))) {
    skipChar();
    return Token(TK_Colon, ":", sloc);
  }
  if (c == '.') {
    skipChar();
    return Token(TK_Period, ".", sloc);
  }

  // identifiers
  if (isLetter(c)) {
    readIdentifier(c);
    StringRef str = copyStr(finishToken());

    unsigned keyid = lookupKeyword(str.c_str());
    if (keyid) {
      return Token(keyid, str, sloc);
    }
    return Token(TK_Identifier, str, sloc);
  }

  // generic operators
  if (isOperatorChar(c)) {
    readOperator(c);
    StringRef str = copyStr(finishToken());

    unsigned keyid = lookupKeyword(str.c_str());
    if (keyid) {
      return Token(keyid, str, sloc);
    }
    return Token(TK_Operator, str, sloc);
  }

  // numbers
  if (isDigit(c)) {
    readInteger(c);
    StringRef str = copyStr(finishToken());
    return Token(TK_LitInteger, str, sloc);
  }

  // characters
  if (c == '\'') {
    if (!readCharacter())
      return Token(TK_Error);

    StringRef str = copyStr(finishToken());
    return Token(TK_LitCharacter, str, sloc);
  }

  // strings
  if (c == '\"') {
    if (!readString())
      return Token(TK_Error);

    StringRef str = copyStr(finishToken());
    return Token(TK_LitString, str, sloc);
  }

  // if we're out of buffer, put in an EOF token.
  if (c == 0 || stream_eof()) {
    return Token(TK_EOF, "", sloc);
  }

  // Can't get the next token -- signal an error and bail.
  signalLexicalError();
  return Token(TK_Error, "", sloc);
}
示例#13
0
//-------------------------------------------------------------------------------------------------
// FuntionName: readCharacter
// Purpose: read each character input from stdin, then add it to number object
//          case charater is ' ' => build number.
//          case charater is '\n' => Calculate a sum of squares for Line, then loop for other Line
//          case charater is 'q' => end of test.
//          Other case will be reject.
//--------------------------------------------------------------------------------------------------
bool readCharacter( Number& num, char*& buff, char& sign, bool& isInputIncorrect )
{
   char c = getchar();
   if( c == ENTER )
   {
      putc('\n',stdout);
   }
   else if( c != END_PROGRAM )
   {
      putc(c,stdout);
   }

   if( (c >= '0' && c <= '9') || c == ENTER || c == END_PROGRAM || c == '-' || c == ' ')
   {
      if(c == END_PROGRAM)
      {
         sign = END_PROGRAM;
         return true;
      }
      else
      {
         if( c != ' ' && c != ENTER )
         {
            num.setListCharacter(c);
         }
         else
         {
            bool ret = num.setNumber();
            if(ret)
            {
               countLine += num.getNumber() * num.getNumber();
               num.reset();
            }
            else
            {
               num.reset();
               countLine = 0;
               isInputIncorrect = true;
               return true;
            }

            if( c == ENTER )
            {
               char buf[10];
               sprintf(buf,"%d\n",countLine);
               appenData(buff,buf);
               countLine = 0;
            }
         }

         readCharacter(num, buff, sign, isInputIncorrect);
      }
   }
   else
   {
      num.reset();
      countLine = 0;
      isInputIncorrect = true;
      return true;
   }
}
示例#14
0
int main(void){

	FILE *inputFile = NULL, *outputFileStats = NULL, *outputFileAscii = NULL;
	char c1 = '\0', c2 = '\0', c3 = '\0', c4 = '\0', c5 = '\0', c6 = '\0', c7 = '\0', c8 = '\0', c9 = '\0', c10 = '\0';
	int num1 = 0, num2 = 0, num3 = 0, num4 = 0, num5 = 0, num6 = 0, num7 = 0, num8 = 0, num9 = 0, num10 = 0, numLines = 0, 
		numVowels = 0, numDigits = 0, numAlpha = 0, numLower = 0, numUpper = 0, numSpace = 0, numAlnum = 0, numPunct = 0;

	inputFile = openInputFile();
	outputFileStats = fopen("output_stats.dat", "w");
	outputFileAscii = fopen("output_ascii.dat", "w");

	if (inputFile != NULL && outputFileStats != NULL && outputFileAscii != NULL){
		c1 = readCharacter(inputFile);
		c2 = readCharacter(inputFile);
		c3 = readCharacter(inputFile);
		c4 = readCharacter(inputFile);
		c5 = readCharacter(inputFile);
		c6 = readCharacter(inputFile);
		c7 = readCharacter(inputFile);
		c8 = readCharacter(inputFile);
		c9 = readCharacter(inputFile);
		c10 = readCharacter(inputFile);

		num1 = determineAsciiValue(c1);
		num2 = determineAsciiValue(c2);
		num3 = determineAsciiValue(c3);
		num4 = determineAsciiValue(c4);
		num5 = determineAsciiValue(c5);
		num6 = determineAsciiValue(c6);
		num7 = determineAsciiValue(c7);
		num8 = determineAsciiValue(c8);
		num9 = determineAsciiValue(c9);
		num10 = determineAsciiValue(c10);

		printInt(outputFileAscii, num1);
		printInt(outputFileAscii, num2);
		printInt(outputFileAscii, num3);
		printInt(outputFileAscii, num4);
		printInt(outputFileAscii, num5);
		printInt(outputFileAscii, num6);
		printInt(outputFileAscii, num7);
		printInt(outputFileAscii, num8);
		printInt(outputFileAscii, num9);
		printInt(outputFileAscii, num10);

		numLines = numberLines(c1, numLines);
		numLines = numberLines(c2, numLines);
		numLines = numberLines(c3, numLines);
		numLines = numberLines(c4, numLines);
		numLines = numberLines(c5, numLines);
		numLines = numberLines(c6, numLines);
		numLines = numberLines(c7, numLines);
		numLines = numberLines(c8, numLines);
		numLines = numberLines(c9, numLines);
		numLines = numberLines(c10, numLines);
		printStats(outputFileStats, "Number Lines: ", numLines);

		numVowels = numberVowels(c1, numVowels);
		numVowels = numberVowels(c2, numVowels);
		numVowels = numberVowels(c3, numVowels);
		numVowels = numberVowels(c4, numVowels);
		numVowels = numberVowels(c5, numVowels);
		numVowels = numberVowels(c6, numVowels);
		numVowels = numberVowels(c7, numVowels);
		numVowels = numberVowels(c8, numVowels);
		numVowels = numberVowels(c9, numVowels);
		numVowels = numberVowels(c10, numVowels);
		printStats(outputFileStats, "Number Vowels: ", numVowels);

		numDigits = numberDigits(c1, numDigits);
		numDigits = numberDigits(c2, numDigits);
		numDigits = numberDigits(c3, numDigits);
		numDigits = numberDigits(c4, numDigits);
		numDigits = numberDigits(c5, numDigits);
		numDigits = numberDigits(c6, numDigits);
		numDigits = numberDigits(c7, numDigits);
		numDigits = numberDigits(c8, numDigits);
		numDigits = numberDigits(c9, numDigits);
		numDigits = numberDigits(c10, numDigits);
		printStats(outputFileStats, "Number Digits: ", numDigits);
			
		numAlpha = numberAlphas(c1, numAlpha);
		numAlpha = numberAlphas(c2, numAlpha);
		numAlpha = numberAlphas(c3, numAlpha);
		numAlpha = numberAlphas(c4, numAlpha);
		numAlpha = numberAlphas(c5, numAlpha);
		numAlpha = numberAlphas(c6, numAlpha);
		numAlpha = numberAlphas(c7, numAlpha);
		numAlpha = numberAlphas(c8, numAlpha);
		numAlpha = numberAlphas(c9, numAlpha);
		numAlpha = numberAlphas(c10, numAlpha);
		printStats(outputFileStats, "Number Alpha Characters: ", numAlpha);

		numLower = numberLowers(c1, numLower);
		numLower = numberLowers(c2, numLower);
		numLower = numberLowers(c3, numLower);
		numLower = numberLowers(c4, numLower);
		numLower = numberLowers(c5, numLower);
		numLower = numberLowers(c6, numLower);
		numLower = numberLowers(c7, numLower);
		numLower = numberLowers(c8, numLower);
		numLower = numberLowers(c9, numLower);
		numLower = numberLowers(c10, numLower);
		printStats(outputFileStats, "Number Lowercase Characters: ", numLower);

		numUpper = numberUppers(c1, numUpper);
		numUpper = numberUppers(c2, numUpper);
		numUpper = numberUppers(c3, numUpper);
		numUpper = numberUppers(c4, numUpper);
		numUpper = numberUppers(c5, numUpper);
		numUpper = numberUppers(c6, numUpper);
		numUpper = numberUppers(c7, numUpper);
		numUpper = numberUppers(c8, numUpper);
		numUpper = numberUppers(c9, numUpper);
		numUpper = numberUppers(c10, numUpper);
		printStats(outputFileStats, "Number Uppercase Characters: ", numUpper);

		numSpace = numberSpaces(c1, numSpace);
		numSpace = numberSpaces(c2, numSpace);
		numSpace = numberSpaces(c3, numSpace);
		numSpace = numberSpaces(c4, numSpace);
		numSpace = numberSpaces(c5, numSpace);
		numSpace = numberSpaces(c6, numSpace);
		numSpace = numberSpaces(c7, numSpace);
		numSpace = numberSpaces(c8, numSpace);
		numSpace = numberSpaces(c9, numSpace);
		numSpace = numberSpaces(c10, numSpace);
		printStats(outputFileStats, "Number Whitespaces: ", numSpace);
			
		numAlnum = numberAlnums(c1, numAlnum);
		numAlnum = numberAlnums(c2, numAlnum);
		numAlnum = numberAlnums(c3, numAlnum);
		numAlnum = numberAlnums(c4, numAlnum);
		numAlnum = numberAlnums(c5, numAlnum);
		numAlnum = numberAlnums(c6, numAlnum);
		numAlnum = numberAlnums(c7, numAlnum);
		numAlnum = numberAlnums(c8, numAlnum);
		numAlnum = numberAlnums(c9, numAlnum);
		numAlnum = numberAlnums(c10, numAlnum);
		printStats(outputFileStats, "Number Alphanumeric Characters: ", numAlnum);

		numPunct = numberPuncts(c1, numPunct);
		numPunct = numberPuncts(c2, numPunct);
		numPunct = numberPuncts(c3, numPunct);
		numPunct = numberPuncts(c4, numPunct);
		numPunct = numberPuncts(c5, numPunct);
		numPunct = numberPuncts(c6, numPunct);
		numPunct = numberPuncts(c7, numPunct);
		numPunct = numberPuncts(c8, numPunct);
		numPunct = numberPuncts(c9, numPunct);
		numPunct = numberPuncts(c10, numPunct);
		printStats(outputFileStats, "Number Punctuation Characters: ", numPunct);

		fclose(inputFile);
		fclose(outputFileAscii);
		fclose(outputFileStats);
	}

	return 0;
}