OpenADFortTk (basic)
src/lib/support/sexputil.h
Go to the documentation of this file.
00001 // -*-Mode: C++;-*-
00002 // $Header: /m_home/m_utkej/Argonne/cvs2svn/cvs/OpenADFortTk/src/lib/support/sexputil.h,v 1.4 2005-02-01 21:52:05 eraxxon Exp $
00003 
00004 //***************************************************************************
00005 //
00006 // File:
00007 //   $Source: /m_home/m_utkej/Argonne/cvs2svn/cvs/OpenADFortTk/src/lib/support/sexputil.h,v $
00008 //
00009 // Purpose:
00010 //   Utilities for use with the sexp library.  Essentially, a set of
00011 //   wrappers for accessing data.
00012 //
00013 // Description:
00014 //   [The set of functions, macros, etc. defined in the file]
00015 //
00016 //***************************************************************************
00017 
00018 #ifndef sexputil_h
00019 #define sexputil_h
00020 
00021 //************************** System Include Files ***************************
00022 
00023 #include <inttypes.h>
00024 
00025 //*************************** Sexp Include Files ****************************
00026 
00027 #include <sexp.h>
00028 
00029 //*************************** User Include Files ****************************
00030 
00031 //************************** Forward Declarations ***************************
00032 
00033 //***************************************************************************
00034 
00035 namespace sexp {
00036 
00037 // ---------------------------------------------------------
00038 // sexp type (atom, list), 
00039 // ---------------------------------------------------------
00040 
00041 inline bool
00042 is_atom(sexp_t* sx) 
00043 { 
00044   return (sx->ty == SEXP_VALUE); 
00045 }
00046   
00047 inline bool 
00048 is_list(sexp_t* sx) 
00049 { 
00050   return (sx->ty == SEXP_LIST); 
00051 }
00052 
00053 inline bool 
00054 is_null_list(sexp_t* sx) 
00055 { 
00056   return (sx->list == NULL); 
00057 }
00058 
00059 
00060 // ---------------------------------------------------------
00061 // Atom/value operations
00062 // ---------------------------------------------------------
00063 
00064 inline bool  
00065 is_atom_basic(sexp_t* sx) 
00066 { 
00067   return (sx->aty == SEXP_BASIC); 
00068 }
00069 
00070 
00071 inline char* 
00072 get_value(sexp_t* sx) 
00073 { 
00074   return sx->val; 
00075 }
00076 
00077 inline long
00078 get_value_l(sexp_t* sx, long default_val = 0)
00079 {
00080   long val = default_val;
00081   if (sx->val) {
00082     val = strtol(sx->val, (char **)NULL, 0); // select correct base
00083   } 
00084   return val;
00085 }
00086 
00087 inline unsigned long
00088 get_value_ul(sexp_t* sx, unsigned long default_val = 0)
00089 {  
00090   unsigned long val = default_val;
00091   if (sx->val) {
00092     val = strtoul(sx->val, (char **)NULL, 0); // select correct base
00093   } 
00094   return val;
00095 }
00096 
00097 inline int32_t
00098 get_value_i32(sexp_t* sx, int32_t default_val = 0)
00099 {
00100   return (int32_t)get_value_l(sx, default_val);
00101 }
00102 
00103 inline uint32_t
00104 get_value_ui32(sexp_t* sx, uint32_t default_val = 0)
00105 {
00106   return (uint32_t)get_value_ul(sx, default_val);
00107 }
00108 
00109 inline int64_t
00110 get_value_i64(sexp_t* sx, int64_t default_val = 0)
00111 {
00112   int64_t val = default_val;
00113   if (sx->val) {
00114     val = (int64_t)strtoll(sx->val, (char **)NULL, 0); // select correct base
00115   } 
00116   return val;
00117 }
00118 
00119 inline uint64_t
00120 get_value_ui64(sexp_t* sx, uint64_t default_val = 0)
00121 {  
00122   uint64_t val = default_val;
00123   if (sx->val) {
00124     val = (uint64_t)strtoull(sx->val, (char **)NULL, 0); // select correct base
00125   } 
00126   return val;
00127 }
00128 
00129 
00130 // ---------------------------------------------------------
00131 // List operations
00132 // ---------------------------------------------------------
00133 
00134 // get_elemx: Given a list, get the nth element in the list (beginning at 0)
00135 inline sexp_t*
00136 get_elem(sexp_t* sx, unsigned int n) 
00137 { 
00138   sexp_t* elm = sx->list; // first element
00139   for (int i = 0; elm && i < n; ++i, elm = elm->next) { }
00140   return elm;
00141 }
00142 
00143 inline sexp_t*
00144 get_elem0(sexp_t* sx) 
00145 { 
00146   return sx->list; 
00147 }
00148 
00149 inline sexp_t*
00150 get_elem1(sexp_t* sx) 
00151 { 
00152   return (sx->list) ? sx->list->next : NULL; 
00153 }
00154   
00155 inline sexp_t*
00156 get_elem2(sexp_t* sx) 
00157 {
00158   return (sx->list && sx->list->next) ? sx->list->next->next : NULL; 
00159 }
00160 
00161 inline sexp_t*
00162 get_elem3(sexp_t* sx) 
00163 {
00164   return get_elem(sx, 3);
00165 }
00166 
00167 
00168 // get_next: Given an element in a list, return its immediate sibling
00169 inline sexp_t*
00170 get_next(sexp_t* sx) 
00171 {
00172   return sx->next; 
00173 }
00174 
00175 }; /* namespace sexp */
00176 
00177 //***************************************************************************
00178 
00179 namespace sexp {
00180 
00181 // routines specific to S-expression WHIRL
00182 
00183 inline sexp_t*
00184 get_wnast_attrs(sexp_t* sx)
00185 {
00186   return get_elem3(sx); // fourth element
00187 }
00188 
00189 inline sexp_t*
00190 get_wnast_kid0(sexp_t* sx)
00191 {
00192   return get_elem(sx, 4); // begin at fifth element
00193 }
00194 
00195 
00196 }; /* namespace sexp */
00197 
00198 //***************************************************************************
00199 
00200 #endif /* sexputil_h */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines