コード例 #1
0
ファイル: browse.c プロジェクト: dschniepp/Multiplayer-Quiz
/**
 * \brief	Den Inhalt eines Verzeichnisses auf der Standardausgabe ausgeben
 *
 * Gibt den Inhalt eines Verzeichnisses auf der Standardausgabe zeilenweise aus.
 * Es werden keine versteckten Dateien (deren Name mit einem Punkt beginnt) aufgelistet.
 * Eine Leerzeile signalisiert das Ende der Ausgabe.
 * Fehlermeldungen erscheinen auf der Standardfehlerausgabe.
 */
void browse(const char *directory_name		/**< Der Name des aufzulistenden Verzeichnisses */
	   )
{
	DIR *dirp = opendir(directory_name);
	struct dirent *entry;

	if(dirp == NULL)
	{
		debugPrint("Kann Verzeichnis %s nicht auflisten: %s",
			   directory_name, strerror(errno));
		write(STDOUT_FILENO, "\n", 1);
		return;
	}

	errno = 0;
	entry = readdir(dirp);
	while(entry != NULL)
	{
		if(entry->d_name[0] != '.')		/* versteckte Dateien ignorieren... */
			myPuts(entry->d_name);		/* ...und nur die anderen ausgeben */

		errno = 0;
		entry = readdir(dirp);
	}

	if(errno != 0)			/* Falls ein Fehler aufgetreten ist... */
	{
		debugPrint("Fehler beim Auflisten des Verzeichnisses %s: %s",		/* ...Meldung ausgeben */
			   directory_name, strerror(errno));
	}

	write(STDOUT_FILENO, "\n", 1);
}
コード例 #2
0
ファイル: flush.c プロジェクト: turmary/smalls
void demoFlush(HANDLE hConOut)
{
  HANDLE hStdIn;
  INPUT_RECORD InputBuffer;
  DWORD dwInputEvents;
  int i = 0;
  BOOL bSuccess;
  DWORD dwBytesWritten;

  setConTitle(__FILE__);
  hStdIn = GetStdHandle(STD_INPUT_HANDLE);
  PERR(hStdIn != INVALID_HANDLE_VALUE, "GetStdHandle");
  myPuts(hConOut, "Type a number of characters quickly. I will read 5\n"
                  "characters from the input buffer with a Sleep() delay\n"
                  "which will allow it to fill with characters. After 5\n"
                  "characters I will flush the input buffer with\n"
                  "FlushConsoleInputBuffer and restart the sequence. Note\n"
                  "that any characters you've typed that haven't been read\n"
                  "yet are lost due to the flush.\n"
                  "Enter characters (hit ESC to return):");
  for(;;)
    {
    bSuccess = ReadConsoleInput(hStdIn, &InputBuffer, 1, &dwInputEvents);
    PERR(bSuccess, "ReadConsoleInput");
    /* is it a key down event? */
    if (InputBuffer.EventType == KEY_EVENT && 
        InputBuffer.Event.KeyEvent.bKeyDown)
      {
      if (InputBuffer.Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE)
        return;
      /* write the ascii character out to the console */
      bSuccess = WriteFile(hConOut,
          &InputBuffer.Event.KeyEvent.uChar.AsciiChar,
          1, &dwBytesWritten, NULL);
      PERR(bSuccess, "WriteFile");
      Sleep(1000); /* pause for 1s */
      i++;
      if (i > 5)
        {
        /* flush the input buffer */
        bSuccess = FlushConsoleInputBuffer(hStdIn);
        PERR(bSuccess, "FlushConsoleInputBuffer");
        i = 0;
        }
      } /* if */
    } /* while */
  return;
}
コード例 #3
0
ファイル: readchar.c プロジェクト: turmary/smalls
void demoReadConChar(HANDLE hConOut)
{
  BOOL bSuccess;
  INPUT_RECORD inputBuffer;
  DWORD dwStdInMode;
  HANDLE hStdIn;
  DWORD dwInputEvents;
  COORD coordLine; /* coordinates of where to read characters from */
  CHAR *szLine;  /* buffer to hold the line read from the console */
  DWORD dwCharsRead;
  int i;

  setConTitle(__FILE__);
  myPuts(hConOut, "Click on any line containing characters. I will use\n"
                  "ReadConsoleOutputCharacter to read that line of text into\n"
                  "a buffer, then print that buffer to the console at the\n"
                  "current cursor position. Hit ESC to return.\n\n");
  hStdIn = GetStdHandle(STD_INPUT_HANDLE);
  PERR(hStdIn != INVALID_HANDLE_VALUE, "GetStdHandle");
  /* save the console mode */
  bSuccess = GetConsoleMode(hStdIn, &dwStdInMode);
  PERR(bSuccess, "GetConsoleMode");
  /* enable mouse input */
  bSuccess = SetConsoleMode(hStdIn, dwStdInMode | ENABLE_MOUSE_INPUT);
  PERR(bSuccess, "SetConsoleMode");
  /* allocate space for one line */
  szLine = (char *) malloc(getConX(hConOut));
  PERR(szLine, "malloc");
  for(;;)
    {
    /* get a single input event */
    bSuccess = ReadConsoleInput(hStdIn, &inputBuffer, 1, &dwInputEvents);
    PERR(bSuccess, "ReadConsoleInput");
    switch (inputBuffer.EventType)
      {
      case KEY_EVENT:
        /* is it an ESC key? */
        if (inputBuffer.Event.KeyEvent.bKeyDown &&
            inputBuffer.Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE)
          {
          /* set input mode back to what it was originally and return */
          bSuccess = SetConsoleMode(hStdIn, dwStdInMode);
          PERR(bSuccess, "SetConsoleMode");
          free(szLine); /* free allocated space for a text line */
          return;
          }
        break;
      case MOUSE_EVENT:
        /* was this was a click event? Is any button down or not? */
        if (inputBuffer.Event.MouseEvent.dwEventFlags != MOUSE_MOVED &&
            inputBuffer.Event.MouseEvent.dwButtonState)
          {
          /* read the line where the mouse is, starting at column 0 */
          coordLine.X = 0;
          coordLine.Y = inputBuffer.Event.MouseEvent.dwMousePosition.Y;
          bSuccess = ReadConsoleOutputCharacter(hConOut, szLine,
              getConX(hConOut), coordLine, &dwCharsRead);
          PERR(bSuccess, "ReadConsoleOutputCharacter");
          /* strip trailing spaces */
          i = getConX(hConOut) - 1;
          szLine[i--] = 0; /* null terminate */
          while (szLine[i] == ' ')
            szLine[i--] = 0;
          myPuts(hConOut, szLine);
          }
      } /* switch */
    } /* while */
}