Ver código fonte

added stdin_to_temp for using pipe/stdin as file (for functions that need to fseek, rewind etc)

Former-commit-id: c0281174cdd7055a6671d11c547ab1624a0d6afc
jalec 13 anos atrás
pai
commit
4bd7008b86

+ 22 - 0
examples/stdin_to_temp/Makefile

@@ -0,0 +1,22 @@
+
+.PHONY: all
+
+all: example
+
+.PHONY: example
+
+igl_lib=../../
+
+CFLAGS=-g
+inc=-I$(igl_lib)
+lib=
+
+example: example.o
+	g++ $(CFLAGS) -o example example.o $(lib)
+	rm example.o
+
+example.o: example.cpp
+	g++ $(CFLAGS) -c example.cpp -o example.o $(inc)
+clean:
+	rm -f example.o
+	rm -f example

+ 12 - 0
examples/stdin_to_temp/README

@@ -0,0 +1,12 @@
+This is a simple example program that shows how to use the stdin_to_temp
+function in stdin_to_temp.h in the igl library.
+
+To Build:
+  make
+
+To Run:
+  cat file1 | ./stdin_to_temp_demo
+  cat file1 | ./stdin_to_temp_demo | cat >file2
+  cat file1 | ./stdin_to_temp_demo dummy1 dummy2 | cat >file2
+  ./stdin_to_temp_demo <file1 | cat >file2
+  ./stdin_to_temp_demo <file1 >file2

+ 50 - 0
examples/stdin_to_temp/example.cpp

@@ -0,0 +1,50 @@
+/**
+ * Simple example showing how to use stdin_to_temp
+ *
+ * Compile with:
+ *   g++ -O3 -o stdin_to_temp_demo stdin_to_temp_demo.cpp
+ *
+ * Run examples:
+ *   cat file1 | ./stdin_to_temp_demo
+ *   cat file1 | ./stdin_to_temp_demo | cat >file2
+ *   cat file1 | ./stdin_to_temp_demo dummy1 dummy2 | cat >file2
+ *   ./stdin_to_temp_demo <file1 | cat >file2
+ *   ./stdin_to_temp_demo <file1 >file2
+ *
+ */
+
+#include "stdin_to_temp.h"
+using namespace igl;
+
+#include <cstdio>
+using namespace std;
+
+int main(int argc,char * argv[])
+{
+  // Process arguements and print to stderr
+  for(int i = 1;i<argc;i++)
+  {
+    fprintf(stderr,"argv[%d] = %s\n",i,argv[i]);
+  }
+
+  FILE * temp_file;
+  bool success = stdin_to_temp(&temp_file);
+  if(!success)
+  {
+    fprintf(stderr,"Fatal Error: could not convert stdin to temp file\n");
+    // try to close temp file
+    fclose(temp_file);
+    exit(1);
+  }
+
+  // Do something interesting with the temporary file. 
+  // Read the file and write it to stdout
+  char c;
+  // Read file one character at a time and write to stdout
+  while(fread(&c,sizeof(char),1,temp_file)==1)
+  {
+    fwrite(&c,sizeof(char),1,stdout);
+  }
+  // close file
+  fclose(temp_file);
+}

+ 55 - 0
stdin_to_temp.h

@@ -0,0 +1,55 @@
+#ifndef IGL_STDIN_TO_TEMP
+#define IGL_STDIN_TO_TEMP
+#include <cstdio>
+namespace igl
+{
+  // Write stdin/piped input to a temporary file which can than be preprocessed as it
+  // is (a normal file). This is often useful if you want to process stdin/piped
+  // with library functions that expect to be able to fseek(), rewind() etc..
+  //
+  // If your application is not using fseek(), rewind(), etc. but just reading
+  // from stdin then this will likely cause a bottle neck as it defeats the whole
+  // purpose of piping.
+  //
+  // Outputs:
+  //   temp_file  pointer to temp file pointer, rewound to beginning of file so
+  //     its ready to be read
+  // Return true only if no errors were found
+  //
+  // Note: Caller is responsible for closing the file (tmpfile() automatically
+  // unlinks the file so there is no need to remove/delete/unlink the file)
+  bool stdin_to_temp(FILE ** temp_file);
+}
+
+// IMPLEMENTATION
+#include <iostream>
+
+bool igl::stdin_to_temp(FILE ** temp_file)
+{
+  // get a temporary file
+  *temp_file = tmpfile();
+  if(*temp_file == NULL)
+  {
+    fprintf(stderr,"IOError: temp file could not be created.\n");
+    return false;
+  }
+  char c;
+  // c++'s cin handles the stdind input in a reasonable way
+  while (std::cin.good())
+  {
+    c = std::cin.get();
+    if(std::cin.good())
+    {
+      if(1 != fwrite(&c,sizeof(char),1,*temp_file))
+      {
+        fprintf(stderr,"IOError: error writing to tempfile.\n");
+        return false;
+      }
+    }
+  }
+  // rewind file getting it ready to read from
+  rewind(*temp_file);
+  return true;
+}
+
+#endif