inlib
1.2.0
|
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved. 00002 // See the file inlib.license for terms. 00003 00004 #ifndef inlib_fstring 00005 #define inlib_fstring 00006 00007 #include <string> 00008 #include <cstring> 00009 00010 namespace inlib { 00011 00012 class fstring { 00013 public: 00014 fstring(unsigned a_length):m_length(a_length),m_buffer(0){ 00015 m_length = (m_length==0?1:m_length); // in fortran character*0 does not exist 00016 m_buffer = new char[m_length]; 00017 if(!m_buffer) {m_length = 0;return;} 00018 for(unsigned int index=0;index<m_length;index++) m_buffer[index] = ' '; 00019 } 00020 fstring(const std::string& a_string):m_length(0),m_buffer(0){ 00021 m_length = a_string.size(); 00022 m_length = (m_length==0?1:m_length); // in fortran character*0 does not exist 00023 // Add one more white character so that CERNLIB LENOCC works : 00024 m_buffer = new char[m_length+1]; 00025 if(!m_buffer) {m_length = 0;return;} 00026 unsigned int l = m_length + 1; 00027 for(unsigned int index=0;index<l;index++) m_buffer[index] = ' '; 00028 ::strncpy(m_buffer,a_string.c_str(),a_string.size()); 00029 } 00030 fstring(void* aF77,int a_length):m_length(0),m_buffer(0){ 00031 if(a_length<0) return; 00032 m_length = a_length; 00033 m_length = (m_length==0?1:m_length); // in fortran character*0 does not exist 00034 // Add one more white character so that CERNLIB LENOCC works : 00035 m_buffer = new char[m_length+1]; 00036 if(!m_buffer) {m_length = 0;return;} 00037 unsigned int l = m_length + 1; 00038 for(unsigned int index=0;index<l;index++) m_buffer[index] = ' '; 00039 ::strncpy(m_buffer,(char*)aF77,a_length); 00040 } 00041 virtual ~fstring(){delete [] m_buffer;} 00042 private: 00043 fstring(const fstring&){} 00044 fstring& operator=(const fstring&){return *this;} 00045 public: 00046 char* f_str() const{return m_buffer;} 00047 unsigned int size() const {return m_length;} 00048 std::string std_string() const { 00049 std::string s; 00050 for(unsigned int index=0;index<m_length;index++) s += m_buffer[index]; 00051 return s; 00052 } 00053 private: 00054 unsigned int m_length; 00055 char* m_buffer; // m_buffer is not null terminated. 00056 }; 00057 00058 } 00059 00060 #endif