Exemplo n.º 1
0
////////////////////////////////////////////////////////////////////////////////
// Table loading and saving methods
////////////////////////////////////////////////////////////////////////////////
int ReportTableBase::loadTable(char separator)
{
  // make sure the table is empty;
  m_cells.empty();

  // get filename with full path
  string fn = composeFileName(m_projectDir, m_tableFilename);
  // open file to read
  ifstream file(fn.c_str(), ios::in | ios::binary);
    // if error, say so
  if(!file.is_open()) {
    //cerr << "ERROR: Report table: could not open file " << filename << endl;
    return ERROR;
  }

  // delimiter used to parse the file
  string delim;
  delim = separator;
  // parse the file
  string aux;
  // mark the first line
  bool firstLine = true;
  // get one line from file
  while(getline(file, aux)) {

    // remove comments
    //int found = aux.find_last_of("#");
    //if(found != string::npos)
    //  aux = str.substr(0, found);
    if(firstLine) {
      // vector to store split cells
      vector<string> headers;
      // split the line
      stringSplit2(aux, headers, ";");
      // add the strings to the table
      for(int i = 0 ; i < headers.size() ; i++)
        m_colHeadings.push_back(headers[i]);
      // set first line to false
      firstLine = false;
      // resume to following lines
      continue;
    }

    // vector to store split cells
    vector<string> parts;
    // split the line
    stringSplit2(aux, parts, delim);
    // add the strings to the table
    m_cells.push_back(parts);
  }

  // close the file
  file.close();

  // return OK
  return OK;
}
Exemplo n.º 2
0
int howManyFilesExist(std::string strippedFileName){
  int counter = 0;
  int fID = 0;
  bool hMFEflag = true;

  while(hMFEflag){
    std::string bstring = composeFileName(strippedFileName, fID);
    std::ifstream infile;
    infile.open(bstring.c_str());
    if(infile.good()){
      counter++;
      fID++;
      infile.close();
    }
    else{
      hMFEflag=false;
    }
  }

  return counter;
}
Exemplo n.º 3
0
////////////////////////////////////////////////////////////////////////////////
// Saves the table in text format
int ReportTableBase::saveTable(char separator, bool noEmptyCells)
{
  // get filename with full path
  string fn = composeFileName(m_projectDir, m_tableFilename);
  // open file to write to
  ofstream file(fn.c_str(), ios::out | ios::binary);
    // if error, say so
  if(!file.is_open()) {
    //cerr << "ERROR: Report table: could not open file for writing: " << filename << endl;
    return ERROR;
  }

  // output file

  //output the table headings, if they exist
  if(m_colHeadings.size()) {
    string line;
    for(int j = 0 ; j < m_colHeadings.size() ; j++) {
      if(j) line += separator;

      string cell = m_colHeadings[j];
      if(noEmptyCells && (cell.length() == 0) )
        cell = " ";

      line += cell; //m_colHeadings[j];
    }
    file << line << endl;
  }

  writeTable(file, noEmptyCells, separator);

  // close the file
  file.close();
  // return OK status
  return OK;
}
Exemplo n.º 4
0
int main(int narg, char **args)
{
  parallelData world;
  initializeMPIWorld(world, &narg, &args);
  parseArgs(narg,args,world);
  if(!flag_with_filters){
    std::cout<<"NO FILTERS!!!"<<std::endl;
    return 1;
  }
  checkFlagsConsistence(world);

  int numberOfFiles = getAndCheckFileNumber(world, inputfileName);
  int fileId = world.myRank%numberOfFiles;

  parallelData fileGroup;
  splitCommunicatorFillNewParallelData(world, fileGroup, fileId);

  std::string dummyString = composeFileName(inputfileName,fileId);
  char * fileName = new char[dummyString.length() + 1];
  std::strcpy(fileName,dummyString.c_str());

  resetMinimaAndMaxima();

  int readings, reminder, maxReadings;
  MPI_Offset disp;
  {
    long long particleTotalNumber = getAndCheckFileNumberOfParticles(fileGroup, fileName);
    long long myparticlesToRead = calcParticlesToRead(fileGroup, particleTotalNumber);

    long long *particlesToRead = new long long[fileGroup.nProc];
    fillNumberParticlesToRead(fileGroup, particlesToRead, myparticlesToRead);

    readings = myparticlesToRead/readLength;
    reminder = myparticlesToRead%readLength;

    disp = findDispForSetView(fileGroup,particlesToRead);

    int maxReadingsFile = findMaxNumberOfReadings(fileGroup,particlesToRead);

    MPI_Allreduce(&maxReadingsFile, &maxReadings, 1, MPI_INT, MPI_MAX, world.comm);

    delete[] particlesToRead;
  }


  MPI_File theFile;
  MPI_File_open(fileGroup.comm, fileName, MPI_MODE_RDONLY, MPI_INFO_NULL , &theFile);
  MPI_File_set_view(theFile, disp, MPI_FLOAT, MPI_FLOAT, (char *) "native", MPI_INFO_NULL);





  for (int i = 0; i < readings; i++){
    read_next_chunk(theFile,readLength, outputfileName, world);
  }
  for (int i = 0; i < maxReadings-readings; i++){
    read_next_chunk(theFile, 0, outputfileName, world);
  }


  read_next_chunk(theFile, reminder, outputfileName, world);


  MPI_File_close(&theFile);

  MPI_Finalize();
  return 0;
}
Exemplo n.º 5
0
void worker(int connfd, struct tls* ctx) {

    // Process HEADER
    //--------------
    struct tls* cctx = NULL;
    tls_accept_socket(ctx, &cctx, connfd);
    json_t *header = recvOverTLS(cctx);
    const char* clientName = json_string_value(json_object_get(header, "from"));
    for (size_t i = 0; i < sizeof(clientName); i++) {
        if (clientName[i] == '/' || clientName[i] == '\\') {
            syslog(LOG_ERR, "ERROR in clientName!, aborting");
            return;
        }
    }
    const char* clientAuth = json_string_value(json_object_get(header, "auth"));
    if (strcmp(clientAuth, getClientAuth()) != 0) {
        syslog(LOG_ERR, "Authentication failed");
        return;
    }
    connType type = json_integer_value(json_object_get(header, "type"));
    int size = json_integer_value(json_object_get(header, "size"));

    // Process Payload
    //----------------

    // new Values for graphs
    if (type == NEWDATA) {

        for (int i = 0; i < size; i++) {
            json_t* payload = recvOverTLS(cctx);
            pasteJSON(payload, clientName);
        }
    } 

    // create .json or update displaysettings
    if (type == CREATE || type == UPDATE) {
        for (int i = 0; i < size; i++) {
            json_t* payload = recvOverTLS(cctx);
            const char * name = json_string_value(json_object_get(payload, "name"));
            if (name == NULL) {
                syslog(LOG_ERR, "Error in Message from Client");
                return;
            }

            json_t *root = json_object_get(payload, "payload");

            char* file = composeFileName(clientName, name, "json");
            if (type == CREATE) {
                dumpJSON(root, file);
            } else {
                mergeJSON(root,file);
            }
        }
    }

    // Look, which files are not available
    if (type == HELLO) {
        json_t *relist = json_array();

        json_t *list = recvOverTLS(cctx);

        for (int i = 0; i < size; i++) {
            const char * name = json_string_value(json_array_get(list, i));
            if (name == NULL) {
                syslog(LOG_ERR, "Error in Message from Client");
                return;
            }
            char* file = composeFileName(clientName, name, "json");
            if (access( file, F_OK ) == -1) {
                json_array_append_new(relist, json_string(name)); 
            }
        }
        char * relistStr = json_dumps(relist, JSON_COMPACT);
        sendOverTLS(cctx, relistStr);
        free(relistStr);
    }
    
    // Delete all files from this client
    if (type == DELETE) {
        json_t *list = recvOverTLS(cctx);

        for (int i = 0; i < size; i++) {
            const char * name = json_string_value(json_array_get(list, i));
            delete(clientName, name);
        }
    }

    tls_close(cctx);
    tls_free(cctx);

}
Exemplo n.º 6
0
// paste payload into existing .json
int pasteJSON(json_t *payload, const char *clientName) {
    // Extract data from payload
    const char * name = json_string_value(json_object_get(payload, "name"));
    int type = json_integer_value(json_object_get(payload, "type"));
    
    
    // Type 2: CSV
    if (type == 2) {
        FILE *fp;
        char* file = composeFileName(clientName, name, "csv");
        const char * output = json_string_value(json_object_get(payload, "payload"));
        fp = fopen(file, "w");
        fprintf(fp, "%s",output); 
        fclose(fp);
        return 1;
    }

    json_t *array = json_object_get(payload, "payload");
    
    json_t *root, *dataseq, *graph, *arry;
    json_error_t error;

    // Load *.json
    const char* file = composeFileName(clientName, name, "json");

    root = json_load_file(file, 0, &error);
    // Check for Errors
    if (!root) {
        syslog(LOG_ERR, "Unable to load json File! error: on line %d: %s\n", error.line, error.text); 
        exit(1);
    }
    // Get old Data
    graph = json_object_get(root, "graph");
    dataseq = getDataSequences(graph);
    if (!check(dataseq)) {
        printError(name);
    }
    // Process Every Datasequence
    size_t j;
    for (j = 0; j < json_array_size(dataseq); j++) {
        // If Type is 0 /Line (standard case) append new value at the bottom 
        arry = getSingleSeqeunce(dataseq, j);
        if (strncmp(getType(root), "line", 2) == 0) {
            if (json_array_size(arry) >= (size_t) getMaxCount()) {
                 if (json_array_remove(arry,0)) {
                     syslog(LOG_ERR, "error in processing %s.json\n", name);
                     return 0;
                 }
            }
            if (json_array_append_new(arry, json_array_get(array, j))) {
                syslog(LOG_ERR, "error in appending new entry in %s.json\n", name);
                return 0;
            }
        // When Type is Bar, every Entry has its own name and you change the value
        } else {
            size_t k;
            for (k = 0; k < json_array_size(arry); k++) {
                if (json_real_set(json_object_get(json_array_get(arry, k), "value"), json_real_value(json_array_get(array, k))) ) {
                    return 0;
                }
                    syslog(LOG_ERR, "error in changing entry in %s.json\n", name);
            }
        }
    }
    dumpJSON(root, file);
    return 1;

}