Bläddra i källkod

cmFile.h/c: Added ability to use stdin,stdout,stderr.

master
kevin 11 år sedan
förälder
incheckning
c9a0a1b2dc
2 ändrade filer med 38 tillägg och 7 borttagningar
  1. 31
    6
      cmFile.c
  2. 7
    1
      cmFile.h

+ 31
- 6
cmFile.c Visa fil

@@ -60,6 +60,27 @@ cmFileRC_t cmFileOpen(    cmFileH_t* hp, const cmChar_t* fn, enum cmFileOpenFlag
60 60
   if( cmIsFlag(flags,kUpdateFileFl) )
61 61
     mode[1]='+';
62 62
 
63
+  // handle requests to use stdin,stdout,stderr
64
+  FILE*           sfp = NULL;
65
+  if( cmIsFlag(flags,kStdoutFileFl) )
66
+  {
67
+    sfp = stdout;
68
+    fn  = "stdout";
69
+  }
70
+  else
71
+    if( cmIsFlag(flags,kStderrFileFl) )
72
+    {
73
+      sfp = stderr;
74
+      fn  = "stderr";
75
+    }
76
+    else
77
+      if( cmIsFlag(flags,kStdinFileFl) )
78
+      {
79
+        sfp = stdin;
80
+        fn  = "stdin";
81
+      }
82
+
83
+
63 84
   if( fn == NULL )
64 85
     return cmErrMsg(&err,kObjAllocFailFileRC,"File object allocation failed due to empty file name.");
65 86
 
@@ -73,13 +94,17 @@ cmFileRC_t cmFileOpen(    cmFileH_t* hp, const cmChar_t* fn, enum cmFileOpenFlag
73 94
   p->fnStr = (cmChar_t*)(p+1);
74 95
   strcpy(p->fnStr,fn);
75 96
   
76
-
77
-  errno = 0;
78
-  if((p->fp = fopen(fn,mode)) == NULL )
97
+  if( sfp != NULL )
98
+    p->fp = sfp;
99
+  else
79 100
   {
80
-    cmFileRC_t rc = _cmFileError(p,kOpenFailFileRC,errno,"File open failed");
81
-    cmMemFree(p);
82
-    return rc;
101
+    errno = 0;
102
+    if((p->fp = fopen(fn,mode)) == NULL )
103
+    {
104
+      cmFileRC_t rc = _cmFileError(p,kOpenFailFileRC,errno,"File open failed");
105
+      cmMemFree(p);
106
+      return rc;
107
+    }
83 108
   }
84 109
  
85 110
   hp->h    = p;

+ 7
- 1
cmFile.h Visa fil

@@ -42,7 +42,10 @@ extern "C" {
42 42
     kWriteFileFl  = 0x02, //< Create an empty file for writing
43 43
     kAppendFileFl = 0x04, //< Open a file for writing at the end of the file.
44 44
     kUpdateFileFl = 0x08, //< Open a file for reading and writing.
45
-    kBinaryFileFl = 0x10  //< Open a file for binary (not text) input/output.
45
+    kBinaryFileFl = 0x10, //< Open a file for binary (not text) input/output.
46
+    kStdoutFileFl = 0x20, //< Ignore fn use 'stdout'
47
+    kStderrFileFl = 0x40, //< Ignore fn use 'stderr'
48
+    kStdinFileFl  = 0x80, //< Ignore fn use 'stdin'
46 49
   };
47 50
 
48 51
   // Open or create a file.    
@@ -51,6 +54,9 @@ extern "C" {
51 54
   // be set to cmFileNullHandle prior to calling this function. If *hp is a valid handle
52 55
   // then it is automatically finalized by an internal call to cmFileClose() prior to
53 56
   // being re-iniitalized.
57
+  //
58
+  // If kStdoutFileFl, kStderrFileFl or kStdinFileFl are set then 
59
+  // file name argument 'fn' is ignored.
54 60
   cmFileRC_t cmFileOpen(    
55 61
     cmFileH_t*             hp,    // Pointer to a client supplied cmFileHandle_t to recieve the handle for the new object.
56 62
     const cmChar_t*        fn,    // The name of the file to open or create. 

Laddar…
Avbryt
Spara