Changeset 16177


Ignore:
Timestamp:
09/18/13 16:16:46 (12 years ago)
Author:
Mathieu Morlighem
Message:

CHG: safer to convert string to char*

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/c/shared/Exceptions/Exceptions.cpp

    r16155 r16177  
    1313#include "../io/Print/Print.h"
    1414#include "../io/Comm/IssmComm.h"
     15#include "../MemOps/MemOps.h"
    1516
    1617ErrorException::ErrorException(const string & what_arg){/*{{{*/
     
    8081        /*Output*/
    8182        std::ostringstream buffer;
     83        char *message = NULL;
    8284
    8385        /*WINDOWS*/
    8486        if(!function_name || file_line==0){
    8587                buffer << " error message: " << this->what_str;
    86                 const string buffer2 = buffer.str();
    87                 return buffer2.c_str();
     88        }
     89        else{
     90                buffer << "\nError in ==> " << this->file_name << ":" << file_line << "\n";
     91                buffer << this->function_name << " error message: " << this->what_str;
    8892        }
    8993
    90         buffer << "\nError in ==> " << this->file_name << ":" << file_line << "\n";
    91         buffer << this->function_name << " error message: " << this->what_str;
    92 
    93         const string buffer2 = buffer.str();
    94         return buffer2.c_str();
     94        /*Convert std::ostringstream to std::string and then create char* */
     95        std::string buffer2 = buffer.str();
     96        message = xNew<char>(strlen(buffer2.c_str())+1); sprintf(message,"%s",buffer2.c_str());
     97        return message;
    9598}/*}}}*/
    9699const char* ErrorException::PythonReport() const{/*{{{*/
     
    98101        /*Output*/
    99102        std::ostringstream buffer;
     103        char *message = NULL;
    100104
    101105        /*WINDOWS*/
    102106        if(!function_name || file_line==0){
    103107                buffer << " error message: " << this->what_str;
    104                 const string buffer2 = buffer.str();
    105                 return buffer2.c_str();
     108        }
     109        else{
     110                buffer << "\nError in ==> " << this->file_name << ":" << file_line << "\n";
     111                buffer << this->function_name << " error message: " << this->what_str;
    106112        }
    107113
    108         buffer << "\nError in ==> " << this->file_name << ":" << file_line << "\n";
    109         buffer << this->function_name << " error message: " << this->what_str;
    110 
    111         const string buffer2 = buffer.str();
    112         return buffer2.c_str();
     114        /*Convert std::ostringstream to std::string and then create char* */
     115        std::string buffer2 = buffer.str();
     116        message = xNew<char>(strlen(buffer2.c_str())+1); sprintf(message,"%s",buffer2.c_str());
     117        return message;
    113118}/*}}}*/
Note: See TracChangeset for help on using the changeset viewer.