Loading ...
Sorry, an error occurred while loading the content.

C code needed

Expand Messages
  • Jereme Kuperman
    Hi all. I m working (when I get a chance) on the SVG export, and was looking for someone to help me out with two pretty basic functions in C. I need one that
    Message 1 of 3 , Nov 17, 2003
    • 0 Attachment
      Hi all. I'm working (when I get a chance) on the SVG export, and
      was looking for someone to help me out with two pretty basic
      functions in C. I need one that will take a string (char*) and turn
      it into an array of strings (char**), broken on new lines. The
      second function should take a string (char*) and return a string
      with the following substitutions made: "&" becomes "&" and "<"
      becomes "<".

      For extra credit, I'd love a function that takes a string and does
      both sets of operations to it (break into lines, and do XML
      escaping). Order of operations is not important.

      Thanks,
      Jereme
    • Bruce A. Carson
      Here, I whipped this up for you! It s been a long time since I ve done plain-old-vanilla C. The main() routine is just for testing to show how to call the
      Message 2 of 3 , Nov 17, 2003
      • 0 Attachment
        Here, I whipped this up for you! It's been a long time since I've done plain-old-vanilla C.  The main() routine is just for testing to show how to call the other routines.
         
        Cheers,
            Bruce
        -----------------------------------------------------------------------------
         
        #include <stdio.h>
        #include <string.h>
        #include <stdlib.h>
         

        /* NOTE: The number of lines is determined first (rather than doing
         the entire operation in one pass), so that we know how much
         memory to allocate to the return parameter.      
         IMPORTANT: The caller is responsible for freeing memory returned.*/
        char **breakit (const char *input, int *size)
        {
         char **rval ;
         int  nlines = 0 ;
         int i = 0 ;
         char *tempint = NULL ;
         char *tempstr ;
         char *line ;
         
         if ( *input == '\0') return NULL ;  // Exit if no input
         
         tempint = malloc ( strlen(input + 1) * sizeof (char *) ) ;
         
         strcpy (tempint, input ) ;
         
         /* Make sure that the Input ends in a line terminator */
         if ( input[strlen(input) - 1] != '\n' ) strcat (tempint,"\n") ;
         
         /* Determine the number of lines required */
         tempstr = tempint ;
         while ( (tempstr = strchr (tempstr, '\n')) != NULL )
         {
          nlines++ ;
          tempstr ++ ;  /* Skip past the newline being pointed to */
         }
         
         rval = malloc(nlines * sizeof (char *)) ;
         tempstr = tempint ;
         for (i = 0 ; i < nlines ; i++)
         {
          line = strtok (tempstr, "\n") ;
          rval[i] = malloc ( strlen (line) * sizeof (char *)) ;
          strcpy (rval[i], line ) ;
          tempstr += strlen(line) + 1 ;
         }
         
         free (tempint) ;
         *size = nlines ;
         return rval ;
        }
         
        /* Use to free all memory associated with a returned char ** */
        free_charstarstar (char **arr, const int size)
        {
         int i ;
         for (i=0 ; i < size ; i++)
          free (arr[i]) ;
         free (arr) ;
        }
         
        /* Replace all & and < with &amp; and &lt; respectively      */
        /* Caller must pass in a string buffer and the available length of the buffer
         in general the buffer should be 5xlength of input string to accommodate
         a parameter consisting of all ampersands (&)       */
        void replace_escapes (char *str_output, int output_buffersize, const char *input )
        {
         char *buf ;
         char *p ;
         char *p_last;
         char *p_less ;     /* Pointer into the buffer */
         char *p_ampersand;
         int  done = 0 ;
         
         str_output[0] = '\0';    /* Empty the return variable */
         
         if (input[0] == '\0') return ;  /* No input = no output */
         
         buf = malloc (strlen(input) + 1) ;
         strcpy (buf, input) ;    /* make a copy if the input */
         p = buf ;
         
         while ( ! done )
         {
          p_last = p ;     /* record for posterity */
          p_less = strchr(p, '<') ;
          p_ampersand = strchr (p,'&') ;
         
          if (p_less == NULL && p_ampersand == NULL)
          {
           strcat(str_output, p_last ) ;
           done = 1;
          }
          else
          {
           if (p_less == NULL) p_less = buf + output_buffersize ;
           if (p_ampersand == NULL) p_ampersand = buf + output_buffersize ;
           if (p_less < p_ampersand )
            p = p_less ;
           else if (p_ampersand < p_less )
            p = p_ampersand ;
         
           strncat (str_output, p_last, p - p_last) ;
           switch (*p)
           {
            case '<':
             strcat (str_output, "&lt;") ; break;
            case '&':
             strcat (str_output, "&amp;") ; break;
           }
           p += 1 ; /* move past the character for the next loop */
          }
         }
         free (buf) ;
         return ;
        }
         
        int main ()
        {
         char *p="this is\nan input\nstring.";
         char **arr = NULL ;
         char out[999] ;
         int i ;
         int  size ;
         arr = breakit (p, &size) ;
         
         for (i =0; i < size ; i++)
          printf("Line %d: %s\n", i, arr[i]) ;
         
         free_charstarstar ( arr , size) ;
         
         replace_escapes (out, 999, "&this<is a &&test<<<&") ;
         printf("\n%s\n", out) ;
         return 0;
        }
         
        ----- Original Message -----
        From: "Jereme Kuperman" <jeremek@...>
        Sent: Monday, November 17, 2003 3:00 PM
        Subject: [cc2-dev-l] C code needed

        > Hi all.  I'm working (when I get a chance) on the SVG export, and
        > was looking for someone to help me out with two pretty basic
        > functions in C.  I need one that will take a string (char*) and
        turn
        > it into an array of strings (char**), broken on new lines. 
        The
        > second function should take a string (char*) and return a string
        > with the following substitutions made:  "&" becomes
        "&amp;" and "<"
        > becomes "&lt;".
        >
        > For extra
        credit, I'd love a function that takes a string and does
        > both sets of
        operations to it (break into lines, and do XML
        > escaping).  Order
        of operations is not important.
        >
        > Thanks,
        > Jereme
        >
        >
        > ------------------------ Yahoo! Groups Sponsor
        ---------------------~-->
        > KnowledgeStorm has over 22,000 B2B
        technology solutions. The most comprehensive IT buyers' information available. Research, compare, decide. E-Commerce | Application Dev | Accounting-Finance | Healthcare | Project Mgt | Sales-Marketing | More
        >
        href="http://us.click.yahoo.com/IMai8D/UYQGAA/cIoLAA/f.ewlB/TM">http://us.click.yahoo.com/IMai8D/UYQGAA/cIoLAA/f.ewlB/TM
        > ---------------------------------------------------------------------~->
        >
        > To Post a message, send it to:  
        href="mailto:cc2-dev-l@...">cc2-dev-l@...
        > To Unsubscribe, send a blank message to: cc2-dev-l-unsubscribe@...
        >
        > Your use of Yahoo! Groups is subject to
        href="http://docs.yahoo.com/info/terms/">http://docs.yahoo.com/info/terms/
        >
        >
      • Jeff Osborn
        Jereme, I wrote these up and tested them. They work, but you have to remember to free() the pointers you get back because the routines allocate memory. The
        Message 3 of 3 , Nov 17, 2003
        • 0 Attachment
          Jereme,

          I wrote these up and tested them. They work, but you have to remember to
          free() the
          pointers you get back because the routines allocate memory.

          The Main.c code shows you how to use the stuff, while Lib.c has the 3
          routines in it.

          Later,
          Jeff

          -----Original Message-----
          From: Jereme Kuperman [mailto:jeremek@...]
          Sent: Monday, November 17, 2003 1:00 PM
          To: cc2-dev-l@yahoogroups.com
          Subject: [cc2-dev-l] C code needed


          Hi all. I'm working (when I get a chance) on the SVG export, and
          was looking for someone to help me out with two pretty basic
          functions in C. I need one that will take a string (char*) and turn
          it into an array of strings (char**), broken on new lines. The
          second function should take a string (char*) and return a string
          with the following substitutions made: "&" becomes "&" and "<"
          becomes "<".

          For extra credit, I'd love a function that takes a string and does
          both sets of operations to it (break into lines, and do XML
          escaping). Order of operations is not important.

          Thanks,
          Jereme



          To Post a message, send it to: cc2-dev-l@...
          To Unsubscribe, send a blank message to: cc2-dev-l-unsubscribe@...

          Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
        Your message has been successfully submitted and would be delivered to recipients shortly.