]> git.datanom.net - lensdb.git/blob - lib/exiftool/ExifToolPipe.cpp
Fix collecting lens data
[lensdb.git] / lib / exiftool / ExifToolPipe.cpp
1 //------------------------------------------------------------------------------
2 // File: ExifToolPipe.cpp
3 //
4 // Description: Piped output from exiftool application
5 //
6 // License: Copyright 2013-2019, Phil Harvey (philharvey66 at gmail.com)
7 //
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.
12 //
13 // Created: 2013-11-23 - Phil Harvey
14 //------------------------------------------------------------------------------
15
16 #include <stdio.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <errno.h>
21 #include <sys/wait.h>
22 #include "ExifToolPipe.h"
23
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)
29 {
30 }
31
32 //------------------------------------------------------------------------------
33 // Destructor -- close the input file and delete the buffer
34 ExifToolPipe::~ExifToolPipe()
35 {
36 if (mFile >= 0) close(mFile);
37 Free();
38 }
39
40 //------------------------------------------------------------------------------
41 // Initialize the ExifTool pipe object
42 // - sets input file to non-blocking
43 void ExifToolPipe::Init(int fd, int pid, int initialSize)
44 {
45 mFile = fd;
46 mPid = pid;
47 mBlockSize = initialSize;
48 // set read pipe to non-blocking
49 int flags = fcntl(fd, F_GETFL, 0) | O_NONBLOCK;
50 #ifdef O_BINARY
51 // set to binary mode (Windows only)
52 flags |= O_BINARY;
53 #endif
54 fcntl(fd, F_SETFL, flags);
55 }
56
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()
62 {
63 const int kMinRemaining = 1024; // enlarge buffer if less than this free
64
65 Flush(); // remove previous response from buffer
66
67 // keep reading until we get a complete response or there is no more to read
68 for (;;) {
69
70 // read to fill remaining response buffer, but leave room for null terminator
71 int remaining = mSize - mLen - 1;
72
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);
80 delete [] mBuff;
81 mBuff = pt;
82 mSize = newSize;
83 remaining = newSize - mLen - 1;
84 }
85
86 // read output from exiftool process
87 int bytesRead = (int)read(mFile, mBuff + mLen, remaining);
88 if (bytesRead < 0) {
89 if (errno != EAGAIN) return -2; // read error!
90 bytesRead = 0;
91 }
92 mLen += bytesRead;
93 if (mLen < 13) {
94 if (!bytesRead) {
95 // no response, so check to be sure our exiftool process is still running
96 int status;
97 if (mPid == -1) return -1;
98 if (waitpid(mPid, &status, WNOHANG)) {
99 mPid = -1;
100 return -1;
101 }
102 }
103 return 0;
104 }
105 // must null terminate response string
106 mBuff[mLen] = '\0';
107 // 0123456789012
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;
112
113 for (;;) {
114
115 pt = (char *)memmem(pt, end-pt, "{ready", 6);
116 if (!pt) {
117 mSearchPos = mLen - 5; // continue next search where we left off
118 break;
119 }
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')))
123 {
124 // validate and extract command number
125 int cmdNum = 0;
126 for (int i=0; i<5; ++i) {
127 unsigned d = pt[i+6] -'0';
128 if (d > 9) {
129 cmdNum = 0;
130 break;
131 }
132 cmdNum = cmdNum * 10 + d;
133 }
134 if (cmdNum) {
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!
144 }
145 }
146 pt += 6; // step to next possible search position
147 }
148 if (bytesRead != remaining) break; // stop if we read everything
149 }
150 return 0; // no complete response available
151 }
152
153 //------------------------------------------------------------------------------
154 // Free buffer memory
155 void ExifToolPipe::Free()
156 {
157 delete [] mBuff;
158 mBuff = mString = NULL;
159 mLen = mSize = mPos = mSearchPos = mStringLen = 0;
160 }
161
162 //------------------------------------------------------------------------------
163 // Remove previous response from buffer
164 void ExifToolPipe::Flush()
165 {
166 if (mPos) {
167 if (mLen > mPos) {
168 memmove(mBuff, mBuff+mPos, mLen-mPos);
169 mLen -= mPos;
170 } else {
171 mLen = 0;
172 }
173 mPos = mSearchPos = 0;
174 }
175 mString = NULL;
176 mStringLen = 0;
177 }
178
179 // end
This page took 0.640225 seconds and 6 git commands to generate.