root/src/strsplt.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. strsplt

   1 
   2 #include <unistd.h>
   3 #include <stdio.h>
   4 #include <math.h>
   5 #include <stdlib.h>
   6 #include <pthread.h>
   7 #include "CalculiX.h"
   8 
   9 #include "readfrd.h"
  10 
  11 /* split rec_str at each breakchar into dat[] */
  12 /* dat should be unused before */
  13 ITG strsplt( char *rec_str, char breakchar, char ***ptr)
  14 {
  15   ITG i,j;
  16   ITG nextarg=0, letter=0, skip_breakchar=0;
  17   char **dat;
  18 
  19   if( (dat= (char **)malloc( 1*sizeof(char *))) == NULL )
  20     printf(" ERROR: malloc failed\n");
  21   if( (dat[0]= (char *)malloc(MAX_LINE_LENGTH *sizeof(char))) == NULL )
  22     printf(" ERROR: malloc failed\n");
  23 
  24   /* scan all args divided by breakchar */
  25   nextarg=0;letter=0;
  26   for(j=0; j<MAX_LINE_LENGTH; j++) dat[nextarg][j]='\0'; 
  27   for(i=0; i<MAX_LINE_LENGTH; i++)
  28   {
  29     if(rec_str[i]==(char)EOF) {  break; } 
  30     if(rec_str[i]=='\n') {  break; } 
  31     if(rec_str[i]==0) {  break; } 
  32     if((rec_str[i]==breakchar)&&(!skip_breakchar))
  33     {
  34       /* check if the former dat has chars */
  35       if(strlen(dat[nextarg]))
  36       {
  37         nextarg++;
  38         letter=0;
  39         if( (dat= (char **)realloc((char **)dat, (nextarg+1)*sizeof(char *))) == NULL )
  40           printf(" ERROR: realloc failed\n");
  41         if( (dat[nextarg]= (char *)malloc(MAX_LINE_LENGTH *sizeof(char))) == NULL )
  42           printf(" ERROR: malloc failed\n");
  43         for(j=0; j<MAX_LINE_LENGTH; j++) dat[nextarg][j]='\0'; 
  44       }
  45     }
  46     else
  47     {
  48       if(rec_str[i]=='"') skip_breakchar=!skip_breakchar;
  49       else if(skip_breakchar)
  50       {
  51         dat[nextarg][letter]=rec_str[i];
  52         letter++;
  53       }
  54       else if(rec_str[i]!=' ')
  55       {
  56         dat[nextarg][letter]=rec_str[i];
  57         letter++;
  58       }
  59     }
  60   }
  61   *ptr=dat;
  62 
  63   /* check if the former dat has chars */
  64   if(strlen(dat[nextarg])) nextarg++;
  65 
  66   /* for(i=0; i<nextarg; i++) printf("dat:%s|\n",dat[i]); */
  67   return(nextarg);
  68 }
  69 

/* [<][>][^][v][top][bottom][index][help] */