]>
git.datanom.net - lensdb.git/blob - lib/exiftool/ExifToolPipe.cpp
1 //------------------------------------------------------------------------------
2 // File: ExifToolPipe.cpp
4 // Description: Piped output from exiftool application
6 // License: Copyright 2013-2019, Phil Harvey (philharvey66 at gmail.com)
8 // This is software, in whole or part, is free for use in
9 // non-commercial applications, provided that this copyright notice
10 // is retained. A licensing fee may be required for use in a
11 // commercial application.
13 // Created: 2013-11-23 - Phil Harvey
14 //------------------------------------------------------------------------------
22 #include "ExifToolPipe.h"
24 //------------------------------------------------------------------------------
25 // ExifToolPipe constructor
26 ExifToolPipe::ExifToolPipe()
27 : mFile(-1), mBuff(NULL
), mSize(0), mLen(0), mPos(0), mSearchPos(0),
28 mBlockSize(0), mString(NULL
), mStringLen(0), mPid(-1)
32 //------------------------------------------------------------------------------
33 // Destructor -- close the input file and delete the buffer
34 ExifToolPipe::~ExifToolPipe()
36 if (mFile
>= 0) close(mFile
);
40 //------------------------------------------------------------------------------
41 // Initialize the ExifTool pipe object
42 // - sets input file to non-blocking
43 void ExifToolPipe::Init(int fd
, int pid
, int initialSize
)
47 mBlockSize
= initialSize
;
48 // set read pipe to non-blocking
49 int flags
= fcntl(fd
, F_GETFL
, 0) | O_NONBLOCK
;
51 // set to binary mode (Windows only)
54 fcntl(fd
, F_SETFL
, flags
);
57 //------------------------------------------------------------------------------
58 // Read exiftool response for specified command number
59 // returns: command number on success, 0 if no response available, <0 on error
60 // - this routine returns immediately
61 int ExifToolPipe::Read()
63 const int kMinRemaining
= 1024; // enlarge buffer if less than this free
65 Flush(); // remove previous response from buffer
67 // keep reading until we get a complete response or there is no more to read
70 // read to fill remaining response buffer, but leave room for null terminator
71 int remaining
= mSize
- mLen
- 1;
73 // enlarge buffer if necessary
74 // (could test for "remaining < 1", but what is the point in reading just 1 byte?)
75 if (remaining
< kMinRemaining
) {
76 int newSize
= mSize
+ mLen
+ mBlockSize
;
77 char *pt
= new char[newSize
];
78 if (!pt
) return -3; // out of memory!
79 if (mSize
) memcpy(pt
, mBuff
, mSize
);
83 remaining
= newSize
- mLen
- 1;
86 // read output from exiftool process
87 int bytesRead
= (int)read(mFile
, mBuff
+ mLen
, remaining
);
89 if (errno
!= EAGAIN
) return -2; // read error!
95 // no response, so check to be sure our exiftool process is still running
97 if (mPid
== -1) return -1;
98 if (waitpid(mPid
, &status
, WNOHANG
)) {
105 // must null terminate response string
108 // response should end with "{ready#####}\n" or "{ready#####}\r\n"
109 // - continue searching from where we left off
110 char *pt
= mBuff
+ mSearchPos
;
111 char *end
= mBuff
+ mLen
;
115 pt
= (char *)memmem(pt
, end
-pt
, "{ready", 6);
117 mSearchPos
= mLen
- 5; // continue next search where we left off
120 if (end
-pt
>=13 && pt
[11]=='}' &&
121 // must end with newline, or CR+LF in Windows
122 (pt
[12]=='\n' || (pt
[12]=='\r' && end
-pt
>=14 && pt
[13]=='\n')))
124 // validate and extract command number
126 for (int i
=0; i
<5; ++i
) {
127 unsigned d
= pt
[i
+6] -'0';
132 cmdNum
= cmdNum
* 10 + d
;
135 *pt
= '\0'; // NULL terminate original response
136 mStringLen
= (int)(pt
- mBuff
);
137 // skip LF if this was a Windows CR+LF combo
138 if (pt
[12] == '\r') ++pt
;
139 pt
+= 13; // step to start of next response
140 // update current position in response buffer
141 mPos
= (int)(pt
- mBuff
);
142 mString
= mBuff
; // set return string
143 return cmdNum
; // success!
146 pt
+= 6; // step to next possible search position
148 if (bytesRead
!= remaining
) break; // stop if we read everything
150 return 0; // no complete response available
153 //------------------------------------------------------------------------------
154 // Free buffer memory
155 void ExifToolPipe::Free()
158 mBuff
= mString
= NULL
;
159 mLen
= mSize
= mPos
= mSearchPos
= mStringLen
= 0;
162 //------------------------------------------------------------------------------
163 // Remove previous response from buffer
164 void ExifToolPipe::Flush()
168 memmove(mBuff
, mBuff
+mPos
, mLen
-mPos
);
173 mPos
= mSearchPos
= 0;
This page took 0.640225 seconds and 6 git commands to generate.