stdin_to_temp.cpp 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "stdin_to_temp.h"
  9. #include <iostream>
  10. IGL_INLINE bool igl::stdin_to_temp(FILE ** temp_file)
  11. {
  12. // get a temporary file
  13. *temp_file = tmpfile();
  14. if(*temp_file == NULL)
  15. {
  16. fprintf(stderr,"IOError: temp file could not be created.\n");
  17. return false;
  18. }
  19. char c;
  20. // c++'s cin handles the stdind input in a reasonable way
  21. while (std::cin.good())
  22. {
  23. c = std::cin.get();
  24. if(std::cin.good())
  25. {
  26. if(1 != fwrite(&c,sizeof(char),1,*temp_file))
  27. {
  28. fprintf(stderr,"IOError: error writing to tempfile.\n");
  29. return false;
  30. }
  31. }
  32. }
  33. // rewind file getting it ready to read from
  34. rewind(*temp_file);
  35. return true;
  36. }