LCOV - code coverage report
Current view: top level - alma/ASDM - StringTokenizer.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 72 88 81.8 %
Date: 2024-11-06 17:42:47 Functions: 9 11 81.8 %

          Line data    Source code
       1             : /*
       2             :  * ALMA - Atacama Large Millimeter Array
       3             :  * (c) European Southern Observatory, 2002
       4             :  * (c) Associated Universities Inc., 2002
       5             :  * Copyright by ESO (in the framework of the ALMA collaboration),
       6             :  * Copyright by AUI (in the framework of the ALMA collaboration),
       7             :  * All rights reserved.
       8             :  * 
       9             :  * This library is free software; you can redistribute it and/or
      10             :  * modify it under the terms of the GNU Lesser General Public
      11             :  * License as published by the Free software Foundation; either
      12             :  * version 2.1 of the License, or (at your option) any later version.
      13             :  * 
      14             :  * This library is distributed in the hope that it will be useful,
      15             :  * but WITHOUT ANY WARRANTY, without even the implied warranty of
      16             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      17             :  * Lesser General Public License for more details.
      18             :  * 
      19             :  * You should have received a copy of the GNU Lesser General Public
      20             :  * License along with this library; if not, write to the Free Software
      21             :  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
      22             :  * MA 02111-1307  USA
      23             :  *
      24             :  * File StringTokenizer.cpp
      25             :  */
      26             : 
      27             : #include <alma/ASDM/StringTokenizer.h>
      28             : 
      29             : #include <iostream>
      30             : using namespace std;
      31             : 
      32             : namespace asdm {
      33             : 
      34      654382 : StringTokenizer::StringTokenizer(const string &s, const string &delim, bool returnDelims) {
      35      654382 :         currentPosition = 0;
      36      654382 :         newPosition = -1;
      37      654382 :         delimsChanged = false;
      38      654382 :         maxPosition = s.length();
      39      654382 :         str = new char [maxPosition + 1];
      40      654382 :         int i = 0;
      41   233806563 :         for (i = 0; i < maxPosition; i++)
      42   233152181 :                 str[i] = s.at(i);
      43      654382 :         str[i] = '\0';
      44      654382 :         ostr = new string (str);
      45      654382 :         numDelimiters = delim.length();
      46      654382 :         strDelimiter = delim;
      47             :         //delimiter = new char [numDelimiters];
      48      654382 :         delimiter = strDelimiter.data();
      49             :         //for (i = 0; i < numDelimiters; i++)
      50             :         //      delimiter[i] = delim.at(i);
      51      654382 :         retDelims = returnDelims;
      52      654382 :     setMaxDelimChar();
      53      654382 : }
      54             : 
      55      654382 : StringTokenizer::~StringTokenizer() {
      56      654382 :         delete [] str;
      57      654382 :         delete ostr;
      58             :         //delete [] delimiter;
      59      654382 : }
      60             : 
      61      657501 : void StringTokenizer::setMaxDelimChar() {
      62      657501 :     if (numDelimiters == 0) {
      63           0 :         throw InvalidArgumentException("Delimiters cannot be null.");
      64             :     }
      65      657501 :         char m = 0;
      66     1315002 :         for (int i = 0; i < numDelimiters; i++) {
      67      657501 :             char c = delimiter[i];
      68      657501 :             if (m < c)
      69      657501 :                         m = c;
      70             :         }
      71      657501 :         maxDelimChar = m;
      72      657501 : }
      73             : 
      74    29633173 : int StringTokenizer::indexOfDelimiters(char c) {
      75             :         // int nd = 0;  // set but never used
      76    29641533 :         for (int i = 0; i < numDelimiters; ++i) {
      77             :                 //  nd = delimiter[i];  // set but never used
      78    29633173 :                 if (c == delimiter[i]) {
      79    29624813 :                         return i;
      80             :                 }
      81             :         }
      82        8360 :         return -1;
      83             : }
      84             : 
      85    15974639 : int StringTokenizer::skipDelimiters(int startPos) {
      86    15974639 :     if (numDelimiters == 0) {
      87           0 :         throw InvalidArgumentException("Delimiters cannot be null.");
      88             :     }
      89    15974639 :     int position = startPos;
      90    30785486 :         while (!retDelims && position < maxPosition) {
      91    30276076 :         char c = str[position];
      92    30276076 :         if ((c > maxDelimChar) || (indexOfDelimiters(c) < 0))
      93    15465229 :             break;
      94    14810847 :         position++;
      95             :         }
      96    15974639 :     return position;
      97             : }
      98             : 
      99    15465229 : int StringTokenizer::scanToken(int startPos) {
     100    15465229 :     int position = startPos;
     101   233806563 :     while (position < maxPosition) {
     102   233155300 :         char c = str[position];
     103   233155300 :         if ((c <= maxDelimChar) && (indexOfDelimiters(c) >= 0))
     104    14813966 :             break;
     105   218341334 :         position++;
     106             :         }
     107    15465229 :         if (retDelims && (startPos == position)) {
     108           0 :         char c = str[position];
     109           0 :                 if ((c <= maxDelimChar) && (indexOfDelimiters(c) >= 0))
     110           0 :             position++;
     111             :     }
     112    15465229 :     return position;
     113             : }
     114             : 
     115      509410 : bool StringTokenizer::hasMoreTokens() {
     116             :         // Temporary store this position and use it in the following
     117             :         // nextToken() method only if the delimiters have'nt been changed in
     118             :         // that nextToken() invocation.
     119      509410 :         newPosition = skipDelimiters(currentPosition);
     120      509410 :         return (newPosition < maxPosition);
     121             : }
     122             : 
     123    15465229 : string StringTokenizer::nextToken() {
     124             :         // If next position already computed in hasMoreElements() and
     125             :         // delimiters have changed between the computation and this invocation,
     126             :         // then use the computed value.
     127             : 
     128    15465229 :         currentPosition = (newPosition >= 0 && !delimsChanged) ?  
     129    15465229 :             newPosition : skipDelimiters(currentPosition);
     130             : 
     131             :         // Reset these anyway
     132    15465229 :         delimsChanged = false;
     133    15465229 :         newPosition = -1;
     134             : 
     135    15465229 :         if (currentPosition >= maxPosition)
     136           0 :             throw OutOfBoundsException("No more tokens.");
     137    15465229 :         int start = currentPosition;
     138    15465229 :         currentPosition = scanToken(currentPosition);
     139    15465229 :         return ostr->substr(start, (currentPosition - start));
     140             : }
     141             : 
     142        3119 : string StringTokenizer::nextToken(const string delim) {
     143        3119 :         numDelimiters = delim.length();
     144        3119 :         strDelimiter = delim;
     145        3119 :         delimiter = strDelimiter.data();
     146             :         //delimiter = new char [numDelimiters];
     147             :         //for (int i = 0; i < numDelimiters; ++i)
     148             :         //      delimiter[i] = delim.at(i);
     149             : 
     150             :         // delimiter string specified, so set the appropriate flag.
     151        3119 :         delimsChanged = true;
     152             : 
     153        3119 :     setMaxDelimChar();
     154        3119 :         return nextToken();
     155             : }
     156             : 
     157           0 : int StringTokenizer::countTokens() {
     158           0 :         int count = 0;
     159           0 :         int currpos = currentPosition;
     160           0 :         while (currpos < maxPosition) {
     161           0 :         currpos = skipDelimiters(currpos);
     162           0 :             if (currpos >= maxPosition)
     163           0 :                         break;
     164           0 :         currpos = scanToken(currpos);
     165           0 :             count++;
     166             :         }
     167           0 :         return count;
     168             : }
     169             : 
     170             : } // End namespace.

Generated by: LCOV version 1.16