ADMB Documentation  11.1.2532
 All Classes Files Functions Variables Typedefs Friends Defines
adstring.hpp
Go to the documentation of this file.
00001 /*
00002  * $Id: adstring.hpp 2421 2014-09-28 03:59:00Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  *
00007  * ADModelbuilder and associated libraries and documentations are
00008  * provided under the general terms of the "New BSD" license
00009  *
00010  * License:
00011  *
00012  * Redistribution and use in source and binary forms, with or without
00013  * modification, are permitted provided that the following conditions are
00014  * met:
00015  *
00016  * 1. Redistributions of source code must retain the above copyright
00017  * notice, this list of conditions and the following disclaimer.
00018  *
00019  * 2.  Redistributions in binary form must reproduce the above copyright
00020  * notice, this list of conditions and the following disclaimer in the
00021  * documentation and/or other materials provided with the distribution.
00022  *
00023  * 3.  Neither the name of the  University of California, Otter Research,
00024  * nor the ADMB Foundation nor the names of its contributors may be used
00025  * to endorse or promote products derived from this software without
00026  * specific prior written permission.
00027  *
00028  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00029  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00030  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00031  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00032  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00033  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00034  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00035  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00036  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00037  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00038  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00039  *
00040  */
00041 #ifndef __ADSTRING_HPP__
00042 #define __ADSTRING_HPP__
00043 
00044 #include "clist.h"
00045 #include <string.h>
00046 //#include <fvar.hpp>
00047 #include <ostream>
00048 
00049 #include <stdio.h>
00050 
00051 class adstring_shape
00052 {
00053   size_t sz;
00054 
00055 protected:
00056   adstring_shape(const size_t size)
00057   {
00058     sz = size;
00059   }
00060   size_t& size()
00061   {
00062     return sz;
00063   }
00064 
00065   friend class adstring;
00066 };
00067 
00068 class adstring_array;
00069 
00070 class adstring : public clist
00071 {
00072   adstring_shape * shape;
00073 #if (defined __ZTC__) || (defined __NDPX__)
00074   char * s;
00075 #else
00076   unsigned char * s;
00077 #endif
00078   void allocate(const size_t sz);
00079   void deallocate(void);
00080   friend class adstring_array;
00081 public :
00082   adstring();
00083   adstring(const adstring &v);
00084   adstring(const char v);
00085   adstring(const char *t);
00086   adstring(const size_t lb, const size_t ub);
00087   ~adstring();
00088 
00089   void allocate(const char *t);
00090   void realloc(const char * t);
00091   //adstring(const unsigned char v);
00092 
00093   size_t size() const;
00094   size_t buff_size() const;
00095   unsigned char &operator()(const size_t i);
00096   unsigned char &operator[](const size_t i);
00097   const unsigned char& operator()(const size_t i) const;
00098   const unsigned char& operator[](const size_t i) const;
00099 
00100 #ifdef __INTEL_COMPILER
00101   friend adstring operator+(const adstring &u, const char* v)
00102   {
00103     adstring a(v);
00104     return u + a;
00105   }
00106 #endif
00107 
00108   adstring& operator+=(const adstring &u);
00109   inline adstring& operator+=(const char u)
00110     { return(*this += adstring(u)); }
00111 
00112   int operator==(const adstring &u);
00113 
00114   inline int operator == (const char* u)
00115     { return *this == adstring(u); }
00116 
00117   inline int operator!=(const adstring &u)
00118     { return(!(*this==u)); }
00119 
00120   adstring operator()(const size_t i, const size_t j);
00121   adstring operator()(const size_t i, const size_t j) const;
00122 
00123   int operator==(const char* u) const;
00124   int operator==(const adstring &u) const;
00125 
00126   adstring& operator=(const adstring &t);
00127   adstring& operator=(const char t);
00128    // { return (*this = adstring(t)); }
00129 
00130   operator unsigned char * ();
00131   operator char * ();
00132   operator const unsigned char*() const;
00133   operator const char*() const;
00134 
00135   // Pascal-like adstring functions
00136 
00137   // returns the starting position of substr
00138   size_t pos(const adstring& substr) const;
00139 
00140   // converts a double into a adstring
00141   friend adstring str(double x, int minwidth, int decplaces);
00142   friend adstring str(const int x);
00143   friend void val(const adstring& s, int& v, int& code);
00144   friend int val(const adstring& s);
00145 
00146   void to_upper(void);
00147   void to_lower(void);
00148   adstring to_upper(adstring& s);
00149   adstring to_lower(adstring& s);
00150 
00151   friend std::ostream& operator<<(std::ostream& c, const adstring& t);
00152   friend std::istream& operator>>(std::istream& c, adstring& t);
00153   friend adstring operator+(const adstring &u, const adstring &v);
00154   friend adstring operator+(const adstring &u, const unsigned char v);
00155   friend adstring operator+(const adstring &u, const signed char v);
00156   friend adstring operator+(const adstring &u, const unsigned char *v);
00157   friend adstring operator+(const adstring & u, const signed char *v);
00158   friend adstring operator+(const unsigned char *v, const adstring& u);
00159   friend adstring operator+(const char *v, const adstring &u);
00160   friend adstring operator+(const unsigned char u, const adstring &v);
00161 };
00162 adstring to_lower(adstring& s);
00163 adstring to_upper(adstring& s);
00164 adstring str(double x, int minwidth=17, int decplaces=-1);
00165 adstring str(const int x);
00166 void val(const adstring& s, int& v, int& code);
00167 int val(const adstring& s);
00168 
00169 class cifstream;
00170 
00171 class line_adstring : public adstring
00172 {
00173 public:
00174   line_adstring(void) : adstring() {}
00175   line_adstring(const adstring& s) : adstring(s) {}
00176   line_adstring& operator=(const adstring& s);
00177   line_adstring& operator=(const char *s);
00178   friend std::istream & operator >> (std::istream & c, line_adstring & t);
00179 };
00180 
00181 adstring itoa(int n,int d);
00182 
00183 void str(const int, adstring&);
00184 adstring chr(int c);
00185 size_t length(const adstring& t);
00186 size_t pos(const adstring& substr, const adstring& s);
00187 
00188 //adstring operator+(const char u, const char v)
00189 //  { return (adstring(u) + adstring(v) ); }
00190 #if defined(__BORLANDC__)
00191 #  if (__BORLANDC__  <= 0x0520)
00192      class ifstream;
00193      class ofstream;
00194      class cifstream;
00195 #  endif
00196 #endif
00197 
00198 class vector_shape;
00199 
00200 class adstring_array : public clist
00201 {
00202   vector_shape* shape;
00203   adstring** ptr;
00204 public:
00205   int size() const;
00206   int indexmin(void) const;
00207   int indexmax(void) const;
00208   adstring_array(const adstring_array& sa);
00209   ~adstring_array();
00210   adstring_array(int min,int max);
00211   void allocate(int min,int max);
00212   adstring_array(void);
00213   const adstring& operator[](int i) const;
00214   const adstring& operator()(int i) const;
00215   adstring& operator [] (int i);
00216   adstring& operator () (int i);
00217   adstring_array& operator += (const adstring& s);
00218   adstring_array& append_distinct(const adstring& s);
00219   friend std::ifstream& operator >> (std::ifstream& ifs,adstring_array& sa);
00220   friend std::ostream& operator<<(const std::ostream& ifs,
00221     const adstring_array& sa);
00222   friend cifstream& operator >> (cifstream& ifs,adstring_array& sa);
00223 
00224   void to_upper(void);
00225   void to_lower(void);
00226   adstring_array to_upper(adstring_array& s);
00227   adstring_array to_lower(adstring_array& s);
00228   adstring_array& operator=(const adstring_array&);
00229 };   // end class adstring_array
00230 adstring_array to_lower(adstring_array& s);
00231 adstring_array to_upper(adstring_array& s);
00232 
00233 std::ostream& operator<<(const std::ostream& ifs, const adstring_array& sa);
00234 
00235 int atoi(adstring& s);
00236 #endif //#ifndef __STRING_HPP__