Changeset 8 for trunk/pbs_drmaa/util.c


Ignore:
Timestamp:
03/02/11 22:08:19 (13 years ago)
Author:
mmatloka
Message:

log reader improvements

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/pbs_drmaa/util.c

    r7 r8  
    286286}  
    287287 
     288ssize_t fsd_getline_buffered(char * line,char * buf, ssize_t size, int fd, int * idx, int * end_idx, int * line_idx) 
     289{ 
     290        int i = -1; 
     291        int rc = -1; 
     292 
     293        memset(line,0,size); 
     294         
     295start: 
     296        /* idx - start of data to parse (in buffer) 
     297           end_idx - end of data read from log (in buffer) 
     298           line_idx - place to write data in output line */ 
     299        if(*idx < *end_idx) 
     300        { 
     301                /* take line from buffer */ 
     302                for(i = *idx; i<= *end_idx;i++) 
     303                {                
     304                        if(buf[i] == '\n') 
     305                        { 
     306                                int tmp = i - *idx; 
     307                                strncpy(line + *line_idx,buf + *idx,tmp);                                
     308                                *idx = i + 1; 
     309                         
     310                                tmp+= *line_idx; 
     311                                *line_idx = 0; 
     312                                 
     313                                return tmp; 
     314                        } 
     315                } 
     316                 
     317                /* there was no '\n' so next part of log needs to be read. save lines beginning */ 
     318                if(*line_idx + i - *idx > size ) 
     319                        fsd_exc_raise_fmt(FSD_ERRNO_INTERNAL_ERROR,"Line longer than %d unsupported",size); 
     320                 
     321                strncpy(line + *line_idx,buf + *idx,i - *idx); 
     322                *line_idx += i - *idx; 
     323                *idx = 0; 
     324                *end_idx = 0; 
     325                goto start; 
     326        } 
     327        else 
     328        {                
     329                /* read log */ 
     330                if((rc = read(fd,buf,size)) > 0) 
     331                {                
     332                        *end_idx = rc - 1; 
     333                        *idx = 0; 
     334                        goto start; 
     335                } 
     336                else if (rc == 0)  
     337                        return 0; 
     338                else 
     339                        return -1; 
     340        } 
     341}  
     342 
Note: See TracChangeset for help on using the changeset viewer.