IOFunctions.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003  * 
00004  * Copyright (c) 2012 
00005  * 
00006  * SCHUNK GmbH & Co. KG
00007  *  
00008  * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
00009  * 
00010  * Project name: Drivers for "Amtec M5 Protocol" Electronics V4
00011  *                                                                        
00012  * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
00013  * 
00014  * Email:robotics@schunk.com
00015  * 
00016  * ToDo: 
00017  * 
00018  * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
00019  * 
00020  * Redistribution and use in source and binary forms, with or without 
00021  * modification, are permitted provided that the following conditions are met: 
00022  * 
00023  *  * Redistributions of source code must retain the above copyright 
00024  *    notice, this list of conditions and the following disclaimer. 
00025  *  * Redistributions in binary form must reproduce the above copyright 
00026  *    notice, this list of conditions and the following disclaimer in the 
00027  *    documentation and/or other materials provided with the distribution. 
00028  *  * Neither the name of SCHUNK GmbH & Co. KG nor the names of its 
00029  *    contributors may be used to endorse or promote products derived from 
00030  *    this software without specific prior written permission. 
00031  * 
00032  * This program is free software: you can redistribute it and/or modify 
00033  * it under the terms of the GNU Lesser General Public License LGPL as 
00034  * published by the Free Software Foundation, either version 3 of the 
00035  * License, or (at your option) any later version. 
00036  * 
00037  * This program is distributed in the hope that it will be useful, 
00038  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
00039  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
00040  * GNU Lesser General Public License LGPL for more details. 
00041  * 
00042  * You should have received a copy of the GNU Lesser General Public 
00043  * License LGPL along with this program. 
00044  * If not, see <http://www.gnu.org/licenses/>.
00045  * 
00046  ******************************************************************************/
00047 
00048 
00049 #include "IOFunctions.h"
00050 #include <string.h>
00051 
00052 int util_ignore(int iSize, char cDelimiter, FILE* hFileHandle)
00053 {
00054         char cChar;
00055         for(int i = 0; i < iSize; i++)
00056         {
00057                 cChar = fgetc(hFileHandle);
00058                 if(cChar == EOF)
00059                         return -1;
00060                 if(cChar == cDelimiter)
00061                         return 0;
00062         }
00063         return 0;
00064 }
00065 
00066 int util_skipWhiteSpace(FILE* hFileHandle)
00067 {
00068         char cChar;
00069         do
00070         {
00071                 cChar = fgetc(hFileHandle);
00072                 if(cChar == EOF)
00073                         return -1;
00074                 if(cChar != ' ' && cChar != '"' && cChar != '\t')
00075                 {
00076                         ungetc(cChar, hFileHandle);
00077                         return 0;
00078                 }
00079         }while(1);
00080         return 0;
00081 }
00082 
00083 int util_getStringCutWhiteSpace(char* acReturnString, int iSize, FILE* hFileHandle)
00084 {
00085         char cChar;
00086         fgets(acReturnString, iSize, hFileHandle);
00087         for(int i = 0; i < iSize; i++)
00088         {
00089                 cChar = acReturnString[i];
00090                 if(cChar == ' ' || cChar == '"' || cChar == '#' || cChar == ';' || cChar == '\t' || cChar == '\r' || cChar == '\n' || cChar == '\0')
00091                 {
00092                         acReturnString[i] = '\0';
00093                         break;
00094                 }
00095         }
00096         return 0;
00097 }
00098 
00099 int util_searchSection(const char* acSectionName, FILE* hFileHandle)
00100 {
00101         int iRetVal = 1;
00102         int iSectionLength;
00103         char cChar;
00104         char acBuffer[512];
00105         iSectionLength = strlen(acSectionName);
00106         do
00107         {
00108                 cChar = fgetc(hFileHandle);
00109                 if(cChar == EOF)                                                // check for end of file
00110                         iRetVal = -1;
00111                 else if(cChar == '#' || cChar == ';')   // check for comment
00112                 {
00113                         iRetVal = util_ignore(0x7FFF,'\n', hFileHandle);                // skip all characters up to '\n'
00114                         if(iRetVal == 0)
00115                                 iRetVal = 1;
00116                 }
00117                 else if(cChar == '[')                                   // no comment so parse it
00118                 {
00119                         fgets(acBuffer, iSectionLength+1, hFileHandle);
00120 
00121                         if(strncmp(acBuffer, acSectionName, iSectionLength) == 0)
00122                         {
00123                                 cChar = fgetc(hFileHandle);
00124                                 if(cChar == ']')
00125                                         iRetVal = util_ignore(0x7FFF,'\n', hFileHandle);        // skip all characters up to '\n'
00126                                 else
00127                                         iRetVal = -1;
00128                         }
00129                 }
00130         }
00131         while(iRetVal > 0);
00132         return iRetVal;
00133 }
00134 
00135 int util_searchKey(const char* acKeyName, FILE* hFileHandle)
00136 {
00137         int iRetVal = 1;
00138         int iKeyLength, iBufferLength;
00139         char cChar;
00140         char acBuffer[512];
00141         iKeyLength = strlen(acKeyName);
00142         do
00143         {
00144                 cChar = fgetc(hFileHandle);
00145                 if(cChar == EOF)                                                // check for end of file
00146                         iRetVal = -1;
00147                 else if(cChar == '[')                                   // check for new section
00148                 {
00149                         ungetc(cChar, hFileHandle);
00150                         iRetVal = -1;
00151                 }
00152                 else if(cChar == '#' || cChar == ';')   // check for comment
00153                 {
00154                         iRetVal = util_ignore(0x7FFF,'\n', hFileHandle);                // skip all characters up to '\n'
00155                         if(iRetVal == 0)
00156                                 iRetVal = 1;
00157                 }
00158                 else if(cChar != ' ' && cChar != '\t' && cChar != '\r' && cChar != '\n')        // no comment or whitespace so parse it
00159                 {
00160                         acBuffer[0] = cChar;
00161                         if (iKeyLength > 1)
00162                                 fgets(acBuffer + 1, iKeyLength, hFileHandle);
00163 
00164                         iBufferLength = strlen(acBuffer);
00165                         if(iBufferLength > iKeyLength || (iBufferLength == iKeyLength && acBuffer[iBufferLength - 1] != '\n'))
00166                         {
00167                                 if(strncmp(acBuffer, acKeyName, iKeyLength) == 0)
00168                                 {
00169                                         iRetVal = util_skipWhiteSpace(hFileHandle);
00170                                         cChar = fgetc(hFileHandle);
00171                                         if(cChar == '=')                         // check for delimiter
00172                                                 iRetVal = util_skipWhiteSpace(hFileHandle);
00173                                         else
00174                                                 return -1;
00175                                 }
00176                                 else
00177                                 {
00178                                         iRetVal = util_ignore(0x7FFF,'\n', hFileHandle);                // skip all characters up to '\n'
00179                                         if(iRetVal == 0)
00180                                                 iRetVal = 1;
00181                                 }
00182                         }
00183                 }
00184         }
00185         while(iRetVal > 0);
00186         return iRetVal;
00187 }
00188 
00189 int util_searchString(const char* acSectionName, const char* acKeyName, const char* acDefaultString, char* acReturnString, int iSize, const char* acFileName)
00190 {
00191         FILE* hFileHandle = fopen( acFileName, "r" );
00192         if(hFileHandle <= 0)
00193         {
00194                 strncpy(acReturnString, acDefaultString, iSize);
00195                 return -1;
00196         }
00197 
00198         if(util_searchSection(acSectionName, hFileHandle) < 0)
00199         {
00200                 strncpy(acReturnString, acDefaultString, iSize);
00201                 fclose(hFileHandle);
00202                 return 0;
00203         }
00204         if(util_searchKey(acKeyName, hFileHandle) < 0)
00205         {
00206                 strncpy(acReturnString, acDefaultString, iSize);
00207                 fclose(hFileHandle);
00208                 return 0;
00209         }
00210         util_getStringCutWhiteSpace(acReturnString, iSize, hFileHandle);
00211         fclose(hFileHandle);
00212         return strlen(acReturnString);
00213 }
00214 
00215 int util_setSection(const char* acSectionName, FILE* hFileHandle)
00216 {
00217         int iRetVal = fseek(hFileHandle,0,SEEK_CUR);
00218         if(iRetVal < 0)
00219         {
00220 //              std::cout << "Section set error" << std::endl;
00221                 return -1;
00222         }
00223         iRetVal = fprintf(hFileHandle, "\n\n[%s]", acSectionName);
00224         if(iRetVal == strlen(acSectionName) + 4)
00225         {
00226 //              std::cout << "Section set" << std::endl;
00227                 fseek(hFileHandle,0,SEEK_CUR);
00228                 return 0;
00229         }
00230         else
00231         {
00232 //              std::cout << "Section set error" << std::endl;
00233                 return -1;
00234         }
00235 }
00236 
00237 int util_setKey(const char* acKeyName, FILE* hFileHandle)
00238 {
00239         int iRetVal = fseek(hFileHandle,0,SEEK_CUR);
00240         if(iRetVal < 0)
00241         {
00242 //              std::cout << "Section set error" << std::endl;
00243                 return -1;
00244         }
00245         iRetVal = fprintf(hFileHandle, "\n%s = ", acKeyName);
00246         if(iRetVal == strlen(acKeyName) + 4)
00247         {
00248 //              std::cout << "Key set" << std::endl;
00249                 fseek(hFileHandle,0,SEEK_CUR);
00250                 return 0;
00251         }
00252         else
00253         {
00254 //              std::cout << "Key set error" << std::endl;
00255                 return -1;
00256         }
00257 }
00258 
00259 int util_setString(const char* acSectionName, const char* acKeyName, const char* acString, const char* acFileName)
00260 {
00261         int iRetVal = 0;
00262         int iLength = 0;
00263         char* acBuffer = NULL;
00264         FILE* hFileHandle = fopen( acFileName, "r+" );
00265         if(hFileHandle <= 0)
00266         {
00267                 hFileHandle = fopen( acFileName, "w+" );
00268                 if(hFileHandle <= 0)
00269                 {
00270 //                      std::cout << "File open error" << std::endl;
00271                         return -1;
00272                 }
00273         }
00274 //      std::cout << "File open" << std::endl;
00275         if(util_searchSection(acSectionName, hFileHandle) < 0)
00276         {
00277 //              std::cout << "Section not found" << std::endl;
00278                 iRetVal = util_setSection(acSectionName, hFileHandle);
00279                 if(iRetVal < 0)
00280                 {
00281                         fclose(hFileHandle);
00282                         return -1;
00283                 }
00284         }
00285 //      else
00286 //              std::cout << "Section found" << std::endl;
00287         if(util_searchKey(acKeyName, hFileHandle) < 0)
00288         {
00289 //              std::cout << "Key not found" << std::endl;
00290                 fpos_t fposRest;
00291                 iRetVal = fgetpos(hFileHandle, &fposRest);
00292                 if(iRetVal < 0)
00293                 {
00294 //                      std::cout << "get Rest pos error" << std::endl;
00295                         fclose(hFileHandle);
00296                         return -1;
00297                 }
00298                 char cChar;
00299                 do
00300                 {
00301 #if defined(__LINUX__)
00302                         fposRest.__pos--;
00303 #else
00304                         fposRest--;
00305 #endif
00306                         iRetVal = fsetpos(hFileHandle, &fposRest);
00307                         if(iRetVal < 0)
00308                         {
00309 //                              std::cout << "set Rest pos error" << std::endl;
00310                                 fclose(hFileHandle);
00311                                 return -1;
00312                         }
00313                         cChar = fgetc(hFileHandle);
00314                         if(cChar != '\n')
00315                         {
00316 #if defined(__LINUX__)
00317                                 fposRest.__pos++;
00318 #else
00319                                 fposRest++;
00320 #endif
00321                                 iRetVal = fsetpos(hFileHandle, &fposRest);
00322                                 if(iRetVal < 0)
00323                                 {
00324 //                                      std::cout << "set Rest pos error" << std::endl;
00325                                         fclose(hFileHandle);
00326                                         return -1;
00327                                 }
00328                                 break;
00329                         }
00330                 }while(1);
00331 
00332                 do
00333                 {
00334                         cChar = fgetc(hFileHandle);
00335                         if(cChar == EOF)
00336                                 break;
00337                 }while(1);
00338                 fpos_t fposEnd;
00339                 iRetVal = fgetpos(hFileHandle, &fposEnd);
00340                 if(iRetVal < 0)
00341                 {
00342 //                      std::cout << "get End pos error" << std::endl;
00343                         fclose(hFileHandle);
00344                         return -1;
00345                 }
00346 #if defined(__LINUX__)
00347                 iLength = fposEnd.__pos - fposRest.__pos;
00348 #else
00349                 iLength = fposEnd - fposRest;
00350 #endif
00351                 if(iLength > 0)
00352                 {
00353                         acBuffer = new char[iLength];
00354         //              std::cout << "Rest length: " << iLength << std::endl;
00355 
00356                         iRetVal = fsetpos(hFileHandle, &fposRest);
00357                         if(iRetVal < 0)
00358                         {
00359         //                      std::cout << "set Rest pos error" << std::endl;
00360                                 fclose(hFileHandle);
00361                                 if(acBuffer != NULL)
00362                                         delete[] acBuffer;
00363                                 return -1;
00364                         }
00365                                         
00366                         iLength = fread(acBuffer, sizeof(char), iLength, hFileHandle);
00367                         if(iLength < 0)
00368                         {
00369         //                      std::cout << "read Rest error" << std::endl;
00370                                 fclose(hFileHandle);
00371                                 if(acBuffer != NULL)
00372                                         delete[] acBuffer;
00373                                 return -1;
00374                         }
00375                         acBuffer[iLength] = '\0';
00376                 }
00377 //              std::cout << "read Rest:" << acBuffer << std::endl;
00378                 iRetVal = fsetpos(hFileHandle, &fposRest);
00379                 if(iRetVal < 0)
00380                 {
00381 //                      std::cout << "set String pos error" << std::endl;
00382                         fclose(hFileHandle);
00383                         if(acBuffer != NULL)
00384                                 delete[] acBuffer;
00385                         return -1;
00386                 }
00387                 iRetVal = util_setKey(acKeyName, hFileHandle);
00388                 if(iRetVal < 0)
00389                 {
00390                         fclose(hFileHandle);
00391                         if(acBuffer != NULL)
00392                                 delete[] acBuffer;
00393                         return -1;
00394                 }
00395                 iRetVal = fprintf(hFileHandle, "%s", acString);
00396                 if(iRetVal != strlen(acString))
00397                 {
00398 //                      std::cout << "String set error" << std::endl;
00399                         fclose(hFileHandle);
00400                         if(acBuffer != NULL)
00401                                 delete[] acBuffer;
00402                         return -1;
00403                 }
00404 //              else
00405 //                      std::cout << "String set" << std::endl;
00406                 if(iLength > 0)
00407                 {
00408                         iLength = fwrite(acBuffer, sizeof(char), iLength, hFileHandle);
00409                         if(iLength != iLength)
00410                         {
00411         //                      std::cout << "write Rest error" << std::endl;
00412                                 fclose(hFileHandle);
00413                                 if(acBuffer != NULL)
00414                                         delete[] acBuffer;
00415                                 return -1;
00416                         }
00417                         if(acBuffer != NULL)
00418                                 delete[] acBuffer;
00419                 }
00420         }
00421         else
00422         {       
00423 //              std::cout << "Key found" << std::endl;
00424                 fpos_t fposString;
00425                 iRetVal = fgetpos(hFileHandle, &fposString);
00426                 if(iRetVal < 0)
00427                 {
00428 //                      std::cout << "get String pos error" << std::endl;
00429                         fclose(hFileHandle);
00430                         return -1;
00431                 }
00432                 
00433                 char cChar;
00434                 int iErase = 0;
00435                 do
00436                 {
00437                         cChar = fgetc(hFileHandle);
00438                         if(cChar == EOF || cChar == '\n' || cChar == ';' || cChar == '#')
00439                         {
00440                                 ungetc(cChar, hFileHandle);
00441                                 break;
00442                         }
00443                         iErase++;
00444                 }while(1);
00445                 fpos_t fposRest;
00446                 iRetVal = fgetpos(hFileHandle, &fposRest);
00447                 if(iRetVal < 0)
00448                 {
00449 //                      std::cout << "get Rest pos error" << std::endl;
00450                         fclose(hFileHandle);
00451                         return -1;
00452                 }
00453 
00454                 do
00455                 {
00456                         cChar = fgetc(hFileHandle);
00457                         if(cChar == EOF)
00458                                 break;
00459                 }while(1);
00460                 fpos_t fposEnd;
00461                 iRetVal = fgetpos(hFileHandle, &fposEnd);
00462                 if(iRetVal < 0)
00463                 {
00464                         std::cout << "get End pos error" << std::endl;
00465                         fclose(hFileHandle);
00466                         return -1;
00467                 }
00468 #if defined(__LINUX__)
00469                 iLength = fposEnd.__pos - fposRest.__pos;
00470 #else
00471                 iLength = fposEnd - fposRest;
00472 #endif
00473                 if(iLength > 0)
00474                 {
00475                         acBuffer = new char[iLength];
00476         //              std::cout << "Rest length: " << iLength << std::endl;
00477 
00478                         iRetVal = fsetpos(hFileHandle, &fposRest);
00479                         if(iRetVal < 0)
00480                         {
00481         //                      std::cout << "set Rest pos error" << std::endl;
00482                                 fclose(hFileHandle);
00483                                 if(acBuffer != NULL)
00484                                         delete[] acBuffer;
00485                                 return -1;
00486                         }
00487                                         
00488                         iLength = fread(acBuffer, sizeof(char), iLength, hFileHandle);
00489                         if(iLength < 0)
00490                         {
00491         //                      std::cout << "read Rest error" << std::endl;
00492                                 fclose(hFileHandle);
00493                                 if(acBuffer != NULL)
00494                                         delete[] acBuffer;
00495                                 return -1;
00496                         }
00497                         acBuffer[iLength] = '\0';
00498                 }
00499 //              std::cout << "read Rest:" << acBuffer << std::endl;
00500                 iRetVal = fsetpos(hFileHandle, &fposString);
00501                 if(iRetVal < 0)
00502                 {
00503 //                      std::cout << "set String pos error" << std::endl;
00504                         fclose(hFileHandle);
00505                         if(acBuffer != NULL)
00506                                 delete[] acBuffer;
00507                         return -1;
00508                 }
00509 
00510                 iRetVal = fprintf(hFileHandle, "%s ", acString);
00511                 iErase -= strlen(acString) + 1;
00512                 if(iRetVal != strlen(acString) + 1)
00513                 {
00514 //                      std::cout << "String set error" << std::endl;
00515                         fclose(hFileHandle);
00516                         if(acBuffer != NULL)
00517                                 delete[] acBuffer;
00518                         return -1;
00519                 }
00520 //              else
00521 //                      std::cout << "String set" << std::endl;
00522 
00523                 if(iLength > 0)
00524                 {
00525                         iLength = fwrite(acBuffer, sizeof(char), iLength, hFileHandle);
00526                         if(iLength != iLength)
00527                         {
00528         //                      std::cout << "write Rest error" << std::endl;
00529                                 fclose(hFileHandle);
00530                                 if(acBuffer != NULL)
00531                                         delete[] acBuffer;
00532                                 return -1;
00533                         }
00534         //              std::cout << "erase " << iErase << std::endl;
00535                         for(int i = 0; i < iErase; i++)
00536                         {
00537                                 cChar = fputc('\0', hFileHandle);
00538                                 if(cChar == EOF)
00539                                         break;
00540                         }
00541                         if(acBuffer != NULL)
00542                                 delete[] acBuffer;
00543                 }
00544         }
00545         fclose(hFileHandle);
00546         return 0;
00547 }
00548 
00549 // -------------------------------------------------------------------------- ;
00550 
00551 //\fdd{ This function helps to read input formatted as keyword (or phrase),
00552 //              number (optional), delimiter, followed by an argument, which will be
00553 //              read by some other function. Leading white space and comments
00554 //              (everything from a '#' or ';' sign until the end of that line) will be
00555 //              skipped. The next std::string (including any white space) will be compared
00556 //              against the keyword. If this does not match the function returns
00557 //              imediately and indicates the error. If the number is positive
00558 //              (including zero) the next std::string after skipping white space will be
00559 //              read in as an integral number. If the numbers are not equal the
00560 //              function returns and indicates the error. Otherwise white space will
00561 //              be skipped and the next character will be compared with the delimiter.
00562 //              In case of mismatch the function returns an error code, otherwise it
00563 //              skips white space a last time. Now the stream should be positioned at
00564 //              the argument, which could be read by an adequate read function. }
00565 //\xmp{ The following examples could be processed by this function:
00566 //              \begin{itemize}
00567 //                      \item   \verb|count of events = 5|
00568 //                      \item   \verb|item 001: first item|
00569 //                      \item   \verb|position = [ 12, 34 ]|
00570 //                      \item   \verb|circle:|\newline
00571 //                                      \verb|center = [ 43, 21]|\newline
00572 //                                      \verb|radius = 7|
00573 //              \end{itemize}
00574 //              In the last example \texttt{circle} is the keyword, no number is used
00575 //              and the delimiter is a colon. Therefore the corresponding call to this
00576 //              function would look as follows:\newline
00577 //              \begin{quote}
00578 //                      \verb|error = posArgForKey(inL, "circle", -1, ':');|\newline
00579 //              \end{quote}
00580 //              This function will check for "circle" and the colon. Then it positions
00581 //              the stream just ahead of the std::string "center". Given a class
00582 //              \texttt{circle} with a method read able to process this format, we
00583 //              could just call:\newline
00584 //              \begin{quote}
00585 //                      \verb|circle::read(inL);|\newline
00586 //              \end{quote}
00587 //              in order to read the circle from the input stream. }
00588 //\arg{ in:             a reference to an input stream }
00589 //\arg{ key:    the keyword (or phrase) to look for }
00590 //\arg{ number: an optional number following the keyword }
00591 //\arg{ delim:  the sign preceeding the argument for the given keyword }
00592 //\ret{ The constant \texttt{OKAY} indicates successful operation. In case of
00593 //              an error the following cases are distinguished:
00594 //              \begin{itemize}
00595 //                      \item   \texttt{NO\_KEY} the keyword did not match
00596 //                      \item   \texttt{KEY\_BUT\_WRONG\_NUMBER} the number did not match
00597 //                      \item   \texttt{KEY\_BUT\_NO\_EQUAL} the delimiter did not match
00598 //                      \item   \texttt{FOUND\_EOF} EOF reached
00599 //              \end{itemize}. }
00600 
00601 #ifdef WITHSTREAMS
00602 int util_posArgForKey(
00603                 std::istream&           in,
00604                 const char*             key,
00605                 int                             number,
00606                 char                    delim)
00607 {
00608         static char buf[BUFFER_LENGTH];
00609            char cL;
00610            int  count;
00611         
00612         while( !in.eof() )
00613         {
00614                 in >> cL;
00615 
00616                 if(cL == '#' || cL == ';')      // check for comment
00617                         in.ignore(0x7FFF,'\n'); // skip all characters up to '\n'
00618                 else                                            // no comment so parse it
00619                 {
00620                         buf[0] = cL;
00621 
00622                         if (strlen(key) > 1)  // Workaround for bug in WATCOM's std::istream::get
00623                                 in.get(buf+1, strlen(key), '\n');
00624 
00625                         if(strncmp(buf, key, strlen(key)) == 0)
00626                         {
00627                                 if (number >= 0)
00628                                 {
00629                                         in >> count;
00630                                         
00631                                         if (count != number)
00632                                                 return KEY_BUT_WRONG_NUMBER;
00633                                 };
00634                                 
00635                                 in >> std::ws;         // skip whitespace
00636                                 in >> cL;
00637                                 if(cL == delim)   // check for delimiter
00638                                 {
00639                                         // skip whitespace
00640                                         
00641                                         in >> std::ws;
00642                                         
00643                                         // the argument should follow now
00644                                         
00645                                         return OKAY;
00646                                 }
00647                                 else
00648                                         return KEY_BUT_NO_EQUAL;
00649                         }       
00650                         else
00651                                 return NO_KEY;
00652                 };
00653         };
00654 
00655         return FOUND_EOF;
00656 };      
00657 
00658 // -------------------------------------------------------------------------- ;
00659 /* \fdd{Generates error messsages according to an error status for example generated 
00660 by posArgForKey(..)} 
00661 */
00662 void util_parseError(
00663                 int                             status,
00664                 const char*             key,
00665                 int                             number)
00666 {
00667         switch(status)
00668         {
00669                 case OKAY:
00670                         break;
00671                 case KEY_BUT_NO_EQUAL:
00672                         std::cerr << "\nread(in) parse error : '=' expected behind";
00673                         std::cerr << key;
00674                         if (number >= 0)
00675                                 std::cerr << " " << number;
00676                         std::cerr << " !";
00677                         break;
00678                 case NO_KEY:
00679                         std::cerr << "\nread(in) parse error : '";
00680                         std::cerr << key;
00681                         if (number >= 0)
00682                                 std::cerr << " " << number;
00683                         std::cerr << "' expected !";
00684                         break;
00685                 case FOUND_EOF:
00686                         std::cerr << "\nread(in) parse error : premature EOF '";
00687                         std::cerr << key;
00688                         if (number >= 0)
00689                                 std::cerr << " " << number;
00690                         std::cerr << "' expected !";
00691                         break;
00692                 case NO_OPEN_BRACKET:
00693                         std::cerr << "\nread(in) parse error : '[' expected before";
00694                         std::cerr << key;
00695                         if (number >= 0)
00696                                 std::cerr << " " << number;
00697                         std::cerr << " argument !";
00698                         break;
00699                 case NO_SEPERATOR:
00700                         std::cerr << "\nread(in) parse error : ', ' expected ";
00701                         std::cerr << " between components of " << key;
00702                         if (number >= 0)
00703                                 std::cerr << " " << number;
00704                         std::cerr << " argument !";
00705                         break;
00706                 case NO_CLOSED_BRACKET:
00707                         std::cerr << "\nread(in) parse error : ']' expected behind";
00708                         std::cerr << key;
00709                         if (number >= 0)
00710                                 std::cerr << " " << number;
00711                         std::cerr << " argument !";
00712                         break;
00713                 default:
00714                         std::cerr << "\nread(in) : unknown error !?!?!?!?!?!?!?!?!";
00715                         break;
00716         };
00717 };
00718 
00719 // -------------------------------------------------------------------------- ;
00720 /*
00721 \fdd{combines posArgForKey(..) and parseError(..) to one function call.}
00722 \arg{std::istream& inA}
00723 \arg{const char* const keyA}
00724 \arg{int numberA}
00725 \arg{char delimA}
00726 \ret{{\tt const int} is returned. If eof occured before anything else than 
00727   FOUND\_EOF is returned. If no key was found NO\_KEY is returned. 
00728   If a key was found but the wrong number followed KEY\_BUT\_WRONG\_NUMBER is returned.
00729   If a key was found, the number was negativ and no equal sign followed KEY\_BUT\_NO\_EQUAL 
00730   is returned. }
00731 */
00732 void util_posArgForKeyWithCheck(
00733                 std::istream&   in, 
00734                 const char*             key, 
00735                 int                             number, 
00736                 char                    delim )
00737 {
00738         int        status;
00739         
00740         status = util_posArgForKey(in, key, number, delim);
00741         
00742         if(status != OKAY)
00743                 util_parseError(status, key);
00744 };
00745 
00746 #endif


schunk_libm5api
Author(s): Florian Weisshardt
autogenerated on Mon Oct 6 2014 07:30:34