49 if (extFile ==
"xml") {
51 }
else if(extFile ==
"sqlite") {
59 mapFile(inFile, bufferMap.begin, bufferMap.size, fd);
65 blockList.emplace_back(blockName);
68 unmapFile(bufferMap.begin, bufferMap.size, fd);
88 mapFile(inFile, bufferMap.begin, bufferMap.size, fd);
95 if (inBlock == blockName) {
101 unmapFile(bufferMap.begin, bufferMap.size, fd);
130 if (mode.npos != mode.find(
"overwrite"))
132 if (mode.npos != mode.find(
"append"))
138 std::vector<MDLabel>::const_iterator location;
139 location =
std::find(labelsVector.begin(), labelsVector.end(), label);
140 return (location != labelsVector.end());
145 for (
const auto &l : active) {
157 _isColumnFormat =
true;
168 _isColumnFormat = md._isColumnFormat;
178 if (buffer.
size == 0)
224 const std::vector<MDLabel>* desiredLabels) {
229 if (token.find(
'(') == String::npos)
241 columnValues.emplace_back(
new MDObject(label));
254 std::vector<MDObject*> & columnValues,
255 const std::vector<MDLabel>* desiredLabels,
259 char * newline = NULL;
263 if (!_isColumnFormat)
271 found_column =
false;
272 while (iter[0] ==
'#')
276 while ( isspace(iter[0]))
279 if (iter < end && iter[0] ==
'_')
283 std::stringstream ss;
288 String s(iter, newline - iter);
305 columnValues.emplace_back(_mdObject);
317 if (iter <= block.
end +1)
326 object.fromStream(is);
329 object.failed =
true;
330 std::cerr <<
"WARNING: " << errorMsg << std::endl;
341 const std::vector<MDLabel> *desiredLabels) {
343 std::stringstream ss;
345 bool firstTime =
true;
351 memcpy(buffer, block.
loop, n);
359 line.assign(iter, newline - iter);
362 if (!line.empty() && line[0] !=
'#') {
367 std::stringstream ss(line);
368 this->
_parseObjects(ss, columnValues, desiredLabels, firstTime);
381 while (!is.eof() && !is.fail()) {
383 while (is.peek() !=
';' && isspace(is.peek()) && !is.eof())
386 if (is.peek() ==
';') {
390 }
else if (!isspace(is.peek())) {
393 if (!useCommentAsImage)
398 int nCol = columnValues.size();
399 for (
int i = 0;
i < nCol; ++
i)
407 const String &blockRegExp,
bool decomposeStack) {
416 if (!decomposeStack || image().ndim == 1) {
422 for (
size_t i = 1;
i <= image().ndim; ++
i) {
433 std::stringstream ss;
435 std::vector<MDObject*> columnValues;
442 bool useCommentAsImage =
false;
444 bool oldFormat =
true;
446 is.seekg(0, std::ios::beg);
454 if (line.find(
"data_") != 0) {
457 is.ignore(256,
'\n');
458 bool addspace =
false;
467 _comment +=
" " + line;
480 mapFile(inFile, bufferMap.begin, bufferMap.size, fd);
484 int rc = regcomp(&re, (blockRegExp+
"$").c_str(), REG_EXTENDED|REG_NOSUB);
485 if (blockRegExp.size() && rc != 0)
487 blockRegExp.c_str(), inFile.c_str()));
489 bool firstBlock =
true;
490 bool singleBlock = blockRegExp.find_first_of(
".[*+")==String::npos;
497 if (blockRegExp.size() == 0 || regexec(&re, blockName.c_str(), (size_t) 0, NULL, 0) == 0) {
500 if ((_isColumnFormat = (block.loop != NULL))) {
503 if (block.loop == (block.end + 1))
518 unmapFile(bufferMap.begin, bufferMap.size, fd);
522 blockRegExp.c_str(), inFile.c_str()));
524 }
else if (line.find(
"Headerinfo columns:") != String::npos) {
526 std::cerr <<
"WARNING: ** You are using an old file format (DOCFILE) which is going " 527 <<
"to be deprecated in next Xmipp release **" << std::endl;
536 useCommentAsImage =
true;
539 std::cerr <<
"WARNING: ** You are using an old file format (SELFILE) which is going " 540 <<
"to be deprecated in next Xmipp release **" << std::endl;
547 this->
_readRows(is, columnValues, useCommentAsImage);
550 int nCols = columnValues.size();
551 for (
int i = 0;
i < nCols; ++
i)
552 delete columnValues[
i];
561 REPORT_ERROR(
ERR_IO,
"MetaData:writeStar Trying to write metadata with image extension");
563 struct stat file_status;
566 char *tailMetadataFile = NULL;
568 char *target, * target2 = NULL;
573 if (blockName.empty() || !outFile.
exists())
579 if(stat(outFile.c_str(), &file_status) != 0)
581 size = file_status.st_size;
587 fd = open(outFile.c_str(), O_RDWR, S_IREAD | S_IWRITE);
591 map = (
char *) mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
592 if (map == MAP_FAILED)
601 size_t blockNameSize = _szBlockName.size();
604 target = (
char *)
_memmem(map, size, _szBlockName.c_str(), blockNameSize);
608 target2 = (
char *)
_memmem(target+1, size - (target - map),
"\ndata_", 6);
614 tailMetadataFile = (
char *) malloc( ((map + size) - target2));
615 memmove(tailMetadataFile,target2, (map + size) - target2);
617 if (ftruncate(fd, target - map+1)==-1)
623 if (munmap(map, size) == -1)
628 std::ios_base::openmode openMode = (mode ==
MD_OVERWRITE) ? std::ios_base::out : std::ios_base::app;
629 std::ofstream ofs(outFile.c_str(), openMode);
631 write(ofs, blockName, mode);
633 if (tailMetadataFile != NULL) {
636 ofs.write(tailMetadataFile,(map + size) - target2);
637 free(tailMetadataFile);
651 std::ofstream ofs(outFile.c_str(), std::ios_base::app);
Just to locate unclassified errors.
Case or algorithm not implemented yet.
static MDLabel str2Label(const String &labelName)
String getBlockName() const
#define REPORT_ERROR(nerr, ErrormMsg)
std::vector< SelLine >::iterator find(std::vector< SelLine > &text, const std::string &img_name)
void trim(std::string &s)
void compose(const String &str, const size_t no, const String &ext="")
Input/Output general error.
bool hasImageExtension() const
Memory has not been deallocated.
Bad amount of memory requested.
std::vector< String > StringVector
String getExtension() const
void * _memmem(const void *haystack, size_t haystack_len, const void *needle, size_t needle_len)
void unmapFile(char *&map, size_t &size, int &fileDescriptor)
Incorrect argument received.
static MDLabel getNewAlias(const String &alias, MDLabelType type=LABEL_NOTYPE)
bool fromString(const String &str)
File or directory does not exist.
This block does not exist.
basic_istream< char, std::char_traits< char > > istream
String toString(bool withFormat=false, bool isSql=false) const
void mapFile(const FileName &filename, char *&map, size_t &size, int &fileDescriptor, bool readOnly)
bool isMetaData(bool failIfNotExists=true) const
FileName removeBlockName() const
String formatString(const char *format,...)
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
Environment PATH cannot be read.
static String label2Str(const MDLabel &label)