mardi 26 juin 2018

Process files in folders and keep the structure with the output

I've got the following problem. I have the following code:

static void performtask_dir(const char *path) {
    struct dirent *entry;
    DIR *dir = opendir(path);
    if (dir == NULL) {
        return;
    }
    while ((entry = readdir(dir)) != NULL) {
      if (strcmp(entry-> d_name, ".") && strcmp(entry->d_name,".."))
        performTask(std::string(entry->d_name));
        //printf("%s\n",entry->d_name);
    }
    closedir(dir);
}


int main() {
  performtask_dir("./data");
}

and also in performTask I have something like this:

void performTask(...) {
...
file->Export("./output");
...
}

That works fine, but it takes all the documents on the input folder and puts them in the output folder. Now I have a folder with sub-folders, and I would like to do the same but keeping the structure of sub-folders. For instance, if in the input folder I have dir1, dir2 and dir3, I would like to have the output folder with dir1, dir2 and dir3 (each one with the output documents). Is there an easy way to achieve this? Thanks!

lundi 25 juin 2018

How can I solve the following pattern in c++?

The following pattern has to be made:

1 2 3 4 5

1 2 3 4 * 

1 2 3 * * *

1 2 * * * * *

1 * * * * * * *

Here is the code that I've written. It works fine for n=5 but displays incorrect results for n>5:

#include <iostream>
using namespace std;
int main(){
int n,star,k=0;; cin>>n;
for(int row=n;row>0;row--)
{

for(int i=1;i<=row;i++)
{
    cout<<i<<" ";
}
if(row>3){star=n-row;}
else if(row<=3){star=n-row+k-1;}k++;
for(int i=0;i<star;i++)
{
cout<<"*"<<" ";
}
cout<<endl;
}}

Please explain what can be done.

How can I split "Segment" to two "Segments"? c++

given the following classes:

#include <vector>

using std::vector;

enum TypeSeg {
    OPEN, CLOSE, CLOSE_LEFT, CLOSE_RIGHT
};

template<class T>
class Segment {
    T left;
    T right;
    TypeSeg typeS;
public:
    Segment(T left, T right, TypeSeg typeS) :
            left(left), right(right), typeS(typeS) {
    }
    //.....
};

template<class T>
class SegCollection {
    vector<Segment<T>> segments;
public:
//.....
};

So that Segment describes segment ?left, right? while:
if typeS==OPEN so : (left,right).
if typeS==CLOSE so : [left,right].
if typeS==CLOSE_LEFT so : [left,right).
if typeS==CLOSE_RIGHTso : (left,right].

and SegCollection describes collection of segments so that:
The SegCollection no contains two same segments , and no contains two segments with intersect (it will contain the union of them instead) and even no contains two segments like that: [1,4) and [4,5) (for example) , but, it will contain [1,5).

How can I implement operator-() for SegCollection that delete segment from SegCollection so that: this segment not must be in SegCollection, but, all the points (from all the segments in SegCollection) that exists in SegCollection and in the deleted segment will be removed from the segments that exists in SegCollection.

For example: given: [1,7] , [9,12] , if we will remove (2,5) , so we will get: [1,2] , [5,7] , [9,12].

I don't know (I thought about it some hours..) how can I treat in the case that I need to split segment following remove of segment (like [1,7] in the example , that changed to [1,2] , [5,7])?

Note: Segment is a template-class because that it's can be from (int, int) , (float,float) e.g.

How to display in a QListWidget for each item of a QStringList a QLabel and another QString? Qt c++

I retrieve a file list from a folder. For each file I would like to put its date (QString) and a QLabel. For each file these three elements would be put in a QListWidget. I recover all the files and the date correctly. The problem is that these two elements are in two different items. Moreover with this method, I can not put the QLabel in the QListWidget.

Here is my code :

viewList.h :

class viewList : public QWidget{
Q_OBJECT

public:
viewList();
QString getDate();

private:
QGridLayout *gridlayout;
QHBoxLayout *hboxList;
QVBoxLayout *vboxlist;
QPushButton *button;
QLabel *myLabel;
QListWidget *listwidget;

};

viewList.cpp:

viewList::viewList(){

gridlayout=new QGridLayout;
vboxlist=new QVBoxLayout;
hboxList=new QHBoxLayout;
//Button is outside the list 
button=new QPushButton("test",this);
myLabel=new QLabel("ok",this);

QString path="/home/myFolder";
listwidget=new QListWidget;

foreach(QString file, files){
    listwidget->addItem(file);
    listwidget->addItem(getDate());
}
vboxlist->addWidget(listwidget);
gridlayout->addWidget(button,0,0,1,1);
gridlayout->addLayout(vboxlist,1,0,1,1);
this->setLayout(gridlayout);

}

QString viewList::getDate(){
  return QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
}

My three problems is that if I do listwidget-> addItem (myLabel) it does not work.In addition I would like to put the file and the label in a QHBoxLayout and the QHBoxLayout in a QVBoxLayout. But with my method I can not do it. Finally I would like QLabel, QString (date) and QString (file) to be in the same item.

Would anyone have an idea of ​​the method for doing this? For now I just show the file and the date in two different items.

std::ostream manipulators overloads

Below is how ostream deals with manipulators such as std::endl

ostream& ostream::operator << ( ostream& (*op)(ostream&)) {
// call the function passed as parameter with this stream as the argument
   return (*op)(*this);
}

My question is why the parameter is defined as a pointer to a function and not as std::function ?

std::function can store any callable method (function, lambda, function object, etc). Wouldn't that be more powerful? Is there an overload for that ?

The above is an example from the book The C++ standard library a tutorial and reference - Josuttis).

conditional removal of macros

I have codes like this:

    #define SUPPORTS_LOGGING

    class Logger {
        // ...
        template<typename... Args>
        void info(const char* fmt, const Args&... args) { /*...*/ }
        // ...
    };

    Logger logger_ {};

    void someMethod() {
#ifdef SUPPORTS_LOGGING
        logger_.info("....");
#endif
        //....
#ifdef SUPPORTS_LOGGING
        logger_.info("....");
#endif
    }

So, my problem is, logging code clutters main code, due to lot of #ifdefs. Can we remove #ifdefs in one line? Like:

LOG_INFO(logger_, info, "%d%d%d", a, b, c);

And above code conditionally expands to: logger_.info("%d%d%d", a, b, c); if SUPPORTS_LOGGING is defined.

Can we do this?

Increasing the size of an array during runtime

I want to dynamically allocate an array in a for loop using pointers. As the for loop proceeds, the size of the array should increase by one and a new element should be added then. The usual method involves using the new operator, but this always allocated a fixed memory at the time of declaration. Is there any way to do so?

I tried to do so using the following code (simplified for explaing the problem):

sameCombsCount = 0;
int **matchedIndicesArray;    
for(int i = 0; i<1000; i++) //loop condition a variable
{
  sameCombsCount++;

  matchedIndicesArray = new int*[sameCombsCount]; // ??
  // Now add an element in the new block created...
}

The thing is, I do not know the size of the for loop during execution time. It can vary depending upon execution conditions and inputs given. I don't think this is the correct way to do so. Can someone suggest a way to do so?