Skip to content

Commit

Permalink
m
Browse files Browse the repository at this point in the history
  • Loading branch information
neheb committed Nov 21, 2024
1 parent 9fa7214 commit 8316148
Showing 1 changed file with 54 additions and 47 deletions.
101 changes: 54 additions & 47 deletions samples/geotag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <sys/types.h>

#include <algorithm>
#include <fstream>
#include <iostream>

#if __has_include(<filesystem>)
Expand Down Expand Up @@ -475,48 +476,55 @@ bool readDir(const char* path, Options& options) {
return bResult;
}

inline size_t sip(FILE* f, char* buffer, size_t max_len, size_t len) {
while (!feof(f) && len < max_len && buffer[len - 1] != '>')
buffer[len++] = fgetc(f);
inline size_t sip(std::ifstream& f, char* buffer, size_t max_len, size_t len) {
while (f && len < max_len && buffer[len - 1] != '>') {
char c;
f.get(c);
buffer[len++] = c;
}
return len;
}

bool readXML(const char* path, Options& options) {
FILE* f = fopen(path, "r");
std::ifstream file(path, std::ios::binary);
if (!file.is_open()) {
return false;
}

XML_Parser parser = XML_ParserCreate(nullptr);
bool bResult = f && parser;
if (bResult) {
char buffer[8 * 1024];
UserData me(options);

XML_SetUserData(parser, &me);
XML_SetElementHandler(parser, startElement, endElement);
XML_SetCharacterDataHandler(parser, charHandler);

// a little sip at the data
size_t len = fread(buffer, 1, sizeof(buffer) - 100, f);
const char* lead = "<?xml";
bResult = strncmp(lead, buffer, strlen(lead)) == 0;

// swallow it
if (bResult) {
len = sip(f, buffer, sizeof buffer, len);
bResult = XML_Parse(parser, buffer, static_cast<int>(len), len == 0) == XML_STATUS_OK;
}
if (!parser) {
return false;
}

// drink the rest of the file
while (bResult && len != 0) {
len = fread(buffer, 1, sizeof(buffer) - 100, f);
len = sip(f, buffer, sizeof buffer, len);
bResult = XML_Parse(parser, buffer, static_cast<int>(len), len == 0) == XML_STATUS_OK;
};
bool bResult = true;
std::vector<char> buffer(8 * 1024);
UserData me(options);

XML_SetUserData(parser, &me);
XML_SetElementHandler(parser, startElement, endElement);
XML_SetCharacterDataHandler(parser, charHandler);

// A little sip at the data
file.read(buffer.data(), buffer.size() - 100);
std::streamsize len = file.gcount();
const char* lead = "<?xml";
bResult = len > 0 && strncmp(lead, buffer.data(), strlen(lead)) == 0;

// Swallow it
if (bResult) {
len = sip(file, buffer.data(), buffer.size(), len);
bResult = XML_Parse(parser, buffer.data(), static_cast<int>(len), len == 0) == XML_STATUS_OK;
}

if (f)
fclose(f);
if (parser)
XML_ParserFree(parser);
// Drink the rest of the file
while (bResult && len > 0) {
file.read(buffer.data(), buffer.size() - 100);
len = file.gcount();
len = sip(file, buffer.data(), buffer.size(), len);
bResult = XML_Parse(parser, buffer.data(), static_cast<int>(len), len == 0) == XML_STATUS_OK;
}

XML_ParserFree(parser);
return bResult;
}

Expand Down Expand Up @@ -579,22 +587,21 @@ bool sina(const char* s, const char** a) {
}

int readFile(const char* path, const Options& /* options */) {
FILE* f = fopen(path, "r");
int nResult = f ? typeFile : typeUnknown;
if (f) {
const char* ext = strstr(path, ".");
if (ext) {
const char* docs[] = {".doc", ".txt", nullptr};
const char* code[] = {".cpp", ".h", ".pl", ".py", ".pyc", nullptr};
if (sina(ext, docs))
nResult = typeDoc;
if (sina(ext, code))
nResult = typeCode;
}
fclose(f);
if (!fs::exists(path)) {
return typeUnknown;
}

const char* ext = strstr(path, ".");
if (ext) {
const char* docs[] = {".doc", ".txt", nullptr};
const char* code[] = {".cpp", ".h", ".pl", ".py", ".pyc", nullptr};
if (sina(ext, docs))
return typeDoc;
if (sina(ext, code))
return typeCode;
}

return nResult;
return typeFile;
}

Position* searchTimeDict(TimeDict_t& td, const time_t& time, long long delta) {
Expand Down

0 comments on commit 8316148

Please sign in to comment.