Skip to content

Commit

Permalink
add: debug xml-parsing errors
Browse files Browse the repository at this point in the history
When a file cannot be loaded because of a mis-formatted XML-tag,
a message is sent to stderr, where to find the error in the file.
Additionally that part of the XML-file will be written to stderr.
  • Loading branch information
plc-user committed Jan 14, 2025
1 parent 467f58a commit c3c9e86
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 7 deletions.
89 changes: 89 additions & 0 deletions inc/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <random> // for random values of uuid
#include <cmath> // for sqrt, atan2, isnan(), ...
#include <iomanip> // for IO-Operations
#include <fstream> // for file-reading
#include <sstream> // for String-Streams
#include <regex> // for "double"-Check

Expand Down Expand Up @@ -247,3 +248,91 @@ void CheckForDoubleString(std::string& sArg){
// ### END: does the string contain a double- or int-value ###
// ###############################################################
//




//
// ###############################################################
// ### determine the terminal-geometry ###
// ###############################################################
//
void get_terminal_size(size_t& width, size_t& height) {
// Quelle:
// https://stackoverflow.com/questions/23369503/get-size-of-terminal-window-rows-columns
#if defined(_WIN32)
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
width = (size_t)(csbi.srWindow.Right-csbi.srWindow.Left+1);
height = (size_t)(csbi.srWindow.Bottom-csbi.srWindow.Top+1);
#elif defined(__linux__) || defined(__APPLE__)
struct winsize w;
ioctl(fileno(stdout), TIOCGWINSZ, &w);
width = (size_t)(w.ws_col);
height = (size_t)(w.ws_row);
#endif // Windows/Linux
}
// ---
size_t get_terminal_height(void) {
// abgeleitet von: get_terminal_size
#if defined(_WIN32)
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
return (size_t)(csbi.srWindow.Bottom-csbi.srWindow.Top+1);
#elif defined(__linux__) || defined(__APPLE__)
struct winsize w;
ioctl(fileno(stdout), TIOCGWINSZ, &w);
return (size_t)(w.ws_row);
#endif // Windows/Linux
}
// ---
size_t get_terminal_width(void) {
// abgeleitet von: get_terminal_size
#if defined(_WIN32)
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
return (size_t)(csbi.srWindow.Right-csbi.srWindow.Left+1);
#elif defined(__linux__) || defined(__APPLE__)
struct winsize w;
ioctl(fileno(stdout), TIOCGWINSZ, &w);
return (size_t)(w.ws_col);
#endif // Windows/Linux
}
//
// ###############################################################
// ### END: determine the terminal-geometry ###
// ###############################################################
//



//
// ###############################################################
// ### read a piece from a text-file ###
// ###############################################################
//
// returns a string of "length" characters around "position"
// the existence of "file" MUST be checked before!!!
std::string ReadPieceOfFile(const std::string file, size_t pos, const size_t length) {
std::ifstream strm;
strm.open ( file );
strm.seekg (0, strm.end);
if (pos < (length/2)) {
pos = 0;
} else if (pos > ((size_t)strm.tellg() - (length/2) )) {
pos = (size_t)strm.tellg() - length;
} else {
pos -= (length/2);
}
strm.seekg (pos);
std::string buffer(length, '\0');
strm.read(&buffer[0], length);
if (buffer.length() > length) { buffer.resize(length); }
strm.close();
return buffer;
}
//
// ###############################################################
// ### END: read a piece from a text-file ###
// ###############################################################
//
25 changes: 24 additions & 1 deletion inc/helpers.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2024 plc-user
* Copyright (c) 2022-2025 plc-user
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
Expand Down Expand Up @@ -29,6 +29,15 @@
#include <iostream> // for IO-Operations
#include <cstdint> // int8_t, ...

// für die Terminal-Größe:
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include <windows.h>
#elif defined(__linux__) || defined(__APPLE__)
#include <sys/ioctl.h>
#endif // Windows/Linux

#define _DEBUG_ 0


Expand Down Expand Up @@ -62,6 +71,20 @@ std::string TextToEntity(const std::string& s);
void CheckForDoubleString(std::string& s);


//
// --- determine the geometry of the terminal ----------------------------------
//
void get_terminal_size(size_t&, size_t&);
size_t get_terminal_height(void);
size_t get_terminal_width(void);


//
// --- returns a string of "length" characters around "position" ---------------
//
std::string ReadPieceOfFile(const std::string file, size_t pos, const size_t length);


//
// Funktionen, die vorher auch schon da waren
//
Expand Down
10 changes: 5 additions & 5 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@
// +----------------------------------------------------------+
//

// own headers:
#include "inc/pugixml/pugixml.hpp"
#include "inc/helpers.h"
#include "inc/elements.h"
// own header(s):
#include "main.h"


Expand Down Expand Up @@ -85,7 +82,10 @@ int main(int argc, char **argv)
}
// check the result of "doc.load"-Function
if (!result){
std::cerr << "file \"" << ElementFile << "\" could not be loaded: " << result.description() << std::endl;
std::cerr << "File \"" << ElementFile << "\" could not be loaded: " << result.description() << std::endl;
std::cerr << "Check file up to byte-offset: " << result.offset << " -- content partly shown here:\n";
// try to read the corrupt part and output to stderr
std::cerr << "(...)" << ReadPieceOfFile(ElementFile, result.offset, (get_terminal_width()-10)) << "(...)\n\n";
return -1;
} else {
if (_DEBUG_) std::cerr << "Element-File loaded successfully.\n";
Expand Down
6 changes: 5 additions & 1 deletion main.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@
#include <getopt.h> // for Commandline-Parameters
#include <filesystem> // for exe-filename
#include <list> // for list of UUIDs
// project-includes
#include "inc/pugixml/pugixml.hpp"
#include "inc/helpers.h"
#include "inc/elements.h"

// =============================================================================
// global variables
// =============================================================================

const std::string sVersion = "v0.5.1";
const std::string sVersion = "v0.5.2";

// the element-file to process:
static std::string ElementFile = "";
Expand Down

0 comments on commit c3c9e86

Please sign in to comment.