Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
anl_cbuf.cxx
Go to the documentation of this file.
00001 /*
00002 
00003   Copyright (C) 2000, 2001 Silicon Graphics, Inc.  All Rights Reserved.
00004 
00005   This program is free software; you can redistribute it and/or modify it
00006   under the terms of version 2 of the GNU General Public License as
00007   published by the Free Software Foundation.
00008 
00009   This program is distributed in the hope that it would be useful, but
00010   WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00012 
00013   Further, this software is distributed without any warranty that it is
00014   free of the rightful claim of any third person regarding infringement 
00015   or the like.  Any license provided herein, whether implied or 
00016   otherwise, applies only to this software file.  Patent licenses, if 
00017   any, provided herein do not apply to combinations of this program with 
00018   other software, or any other product whatsoever.  
00019 
00020   You should have received a copy of the GNU General Public License along
00021   with this program; if not, write the Free Software Foundation, Inc., 59
00022   Temple Place - Suite 330, Boston MA 02111-1307, USA.
00023 
00024   Contact information:  Silicon Graphics, Inc., 1600 Amphitheatre Pky,
00025   Mountain View, CA 94043, or:
00026 
00027   http://www.sgi.com
00028 
00029   For further information regarding this notice, see:
00030 
00031   http://oss.sgi.com/projects/GenInfo/NoticeExplan
00032 
00033 */
00034 
00035 
00036 /* -*-Mode: c++;-*- (Tell emacs to use c++ mode) */
00037 
00038 #include "defs.h"  // Basic types, such as INT32 */
00039 #include "cxx_memory.h"    /* CXX_NEW/CXX_DELETE */
00040 #include "anl_cbuf.h"
00041 
00042 
00043 BOOL
00044 ANL_CBUF::_Is_Splitc(char splitc)
00045 {
00046    INT i;
00047 
00048    for (i = 0; _splitchars[i] != '\0' && _splitchars[i] != splitc; i++);
00049    return (_splitchars[i] == splitc);
00050 
00051 } // ANL_CBUF::_Is_Splitc
00052 
00053 
00054 void
00055 ANL_CBUF::_Split()
00056 {
00057    // Preconditions: _buf must be '\0' terminated
00058    //                _linelength == _max_linelength
00059    //
00060    // Note that we never split a line such that the resultant line,
00061    // after concatenating in the continuation string, again must be
00062    // split (since this could lead to an infinite recursion of splits).
00063    //
00064    INT splitpt, reduction;
00065 
00066    for (splitpt = _next-1;
00067         splitpt >= 0 && _buf[splitpt] != '\n' && !_Is_Splitc(_buf[splitpt]);
00068         splitpt--);
00069 
00070    reduction = _next-(splitpt+1);
00071    if (strlen(_continuation)+reduction < _max_linelength &&
00072        _buf[splitpt] != '\n')
00073    {
00074       // Do the split immediately after the splitpt; any other 
00075       // cases are considered impossible to split.
00076       //
00077       ANL_CBUF remainder(_pool, &_buf[splitpt+1]);
00078 
00079       _linelength -= reduction; // Start again from split-point
00080       _next = splitpt+1;
00081       Write_String(_continuation);      // Insert continuation on a new line
00082       Write_String(remainder.Chars());  // Insert characters after splitpt
00083    }
00084 } // ANL_CBUF::_Split
00085 
00086 
00087 ANL_CBUF::ANL_CBUF(MEM_POOL *pool, const char *s):
00088    _splitchars(""),
00089    _continuation(""),
00090    _linelength(0),
00091    _max_linelength(UINT32_MAX),
00092    _chunk_size(CBUF_SMALLEST_ALLOC_SIZE),
00093    _pool(pool)
00094 {
00095    if (s == NULL)
00096    {
00097       _next = 0;
00098       _linelength = 0;
00099       _size = 0;
00100       _buf = NULL;
00101    }
00102    else
00103    {
00104       // Allocate a multiple of chunks large enough to hold s plus
00105       // one additional character.
00106       //
00107       _next = strlen(s);
00108       _size = _A_Number_Of_Chunks(_next+1);
00109       _buf = _Alloc(_size);
00110       for (INT i=0; i<_next; i++)
00111       {
00112          _buf[i] = s[i];
00113          if (s[i]=='\n')
00114             _linelength = 0;
00115          else
00116             _linelength++;
00117       }
00118       _buf[_next] = '\0';
00119    }
00120 } // ANL_CBUF::ANL_CBUF
00121 
00122 
00123 void
00124 ANL_CBUF::Write_Char(char c)
00125 {
00126    if (_size == 0)
00127    {
00128       _size = _chunk_size;
00129       _buf = _Alloc(_size);
00130    }
00131    else if (_next+1 >= _size)
00132    {
00133       char *reclaim = _buf;
00134 
00135       _size = _size + _chunk_size;
00136       _buf = strcpy(_Alloc(_size), reclaim);
00137       CXX_DELETE_ARRAY(reclaim, _pool);
00138    }
00139    _buf[_next++] = c;
00140    _buf[_next] = '\0';
00141 
00142    if (c == '\n')
00143       _linelength = 0;
00144    else if (++_linelength >= _max_linelength)
00145       _Split();
00146 
00147 } // ANL_CBUF::Write_Char
00148 
00149 
00150 void
00151 ANL_CBUF::Write_String(const char *s)
00152 {
00153    UINT len;
00154 
00155    if (s==NULL)
00156       return;
00157 
00158    len = strlen(s);
00159    if (_next+len >= _size)
00160    {
00161       // Allocate a multiple of chunks large enough to hold s + '\0'
00162       //
00163       _size = _A_Number_Of_Chunks(_next+len+1);
00164       if (_next > 0)
00165       {
00166          char *reclaim = _buf;
00167 
00168          _buf = strcpy(_Alloc(_size), reclaim);
00169          CXX_DELETE_ARRAY(reclaim, _pool);
00170       }
00171       else
00172          _buf = _Alloc(_size);
00173    }
00174    for (INT i=0; i<len; i++)
00175    {
00176       _buf[_next++] = s[i];
00177 
00178       if (s[i] == '\n')
00179          _linelength = 0;
00180       else if (++_linelength >= _max_linelength)
00181       {
00182          _buf[_next] = '\0';
00183          _Split();  // NOTE: may call CBUF::Write_String() recursively
00184 
00185          // May need to reallocate the buffer, so do the rest recursively
00186          //
00187          if (i+1<len)
00188             Write_String(&s[i+1]);
00189          i = len;
00190       }
00191    }
00192    _buf[_next] = '\0';
00193 } // ANL_CBUF::Write_String
00194 
00195 
00196 void
00197 ANL_CBUF:: Append_Pragma_Preamble(BOOL is_omp, BOOL is_lower_case)
00198 {
00199   // Append the appropriate preamble for OMP pragmas 
00200   // or just add a space.
00201 
00202    if (is_omp)
00203      if (is_lower_case)
00204        Write_String("omp_");
00205      else
00206       Write_String(" OMP_");
00207    else 
00208      if (!is_lower_case)
00209        Write_Char(' ');
00210 
00211 }  // ANL_CBUF:: Append_Pragma_Preamble
00212 
00213 
00214  
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines