Main Page   Data Structures   File List   Data Fields   Globals   Related Pages  

misc.c

Go to the documentation of this file.
00001 /***************************************************************************
00002                                    misc.c
00003                              -------------------
00004     begin                : Sun Dec 09 2001
00005     copyright            : (C) 2001-2002 by Christian Hoenig & Gunter Ohrner
00006     email                : pdepp@CustomCDROM.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00023 #include "common.h"
00024 
00025 #include <ctype.h>
00026 #include <stdlib.h>
00027 #include <string.h>
00028 
00029 #include "misc.h"
00030 
00031 #include "buffer.h"
00032 #include "xmalloc.h"
00033 
00034 // Returns the next key seperatet by space chars of the passed str
00035 // beginning at "from"
00036 int getNextValue(int *beg, int *end, unsigned int from, const char *str)
00037 {
00038   //if we fall over a clrf, this is increased
00039   int crlf = 0;
00040 
00041   //we can not work with invalid data
00042   if (str == NULL || from >= strlen(str)) return -1;
00043 
00044   *beg = from;
00045   while (isspace((int) *(str+*beg)))  (*beg)++;
00046   
00047   if (*beg > 0)
00048     if(*(str+*beg-1) == '\n')
00049       crlf++;
00050 
00051   //is there no " " ?
00052   if (str[*beg] == '\0')  return -1;
00053 
00054   *end = (*beg) + 1;
00055   while (!isspace((int) *(str+*end)) && *(str+*end) != '\0') (*end)++;
00056 
00057   //so we point to the last nonspace char;
00058   (*end)--;
00059   return crlf;
00060 }
00061 
00062 
00063 
00064 #define SIZE_INC 256      
00065 
00066 // reliably read a line from a stream in a secure way
00067 char* getLine(FILE *file)
00068 {
00069   struct Buffer line = bufInit((char*)pd_malloc(SIZE_INC), SIZE_INC, 0);
00070   int cur_chr;
00071   
00072   while ((cur_chr = getc(file)) != EOF)
00073   {
00074     *(line.data + line.pos) = (char) cur_chr;
00075     //check if we reached an eol
00076     if (*(line.data + line.pos) == '\n')
00077     {
00078       *(line.data + line.pos) = '\0';
00079       return line.data;
00080     }
00081 
00082     //we have reached the end of the buffersize
00083     if (++(line.pos) == line.len)
00084     {
00085       char* tmp = (char*)pd_realloc(line.data, line.pos + SIZE_INC);
00086       
00087       if (tmp != NULL)
00088       {//reallocation succedded
00089         line.data = tmp;
00090         line.pos += SIZE_INC;
00091       }
00092       else
00093       {//realloc failed
00094         pd_free(line.data);
00095         return NULL;
00096       }
00097     }
00098   }
00099   
00100   *(line.data + line.pos) = '\0';
00101   return line.data;
00102 }
00103 

Generated on Fri Jan 25 22:40:31 2002 for PDepp Webserver by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001