home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / EMXLIB8F.ZIP / EMX / LIB / IO / CRLF.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-02  |  1.3 KB  |  41 lines

  1. /* crlf.c (emx+gcc) -- Copyright (c) 1990-1993 by Eberhard Mattes */
  2.  
  3. #include <io.h>
  4. #include <string.h>
  5.  
  6. /* Do CR/LF -> LF conversion in the buffer BUF containing SIZE characters. */
  7. /* Store the number of resulting characters to *NEW_SIZE. This value does  */
  8. /* not include the final CR, if present. If the buffer ends with CR, 1 is  */
  9. /* returned, 0 otherwise.                                                  */
  10.  
  11. int _crlf (char *buf, size_t size, size_t *new_size)
  12. {
  13.   size_t src, dst;
  14.   char *p;
  15.  
  16.   p = memchr (buf, '\r', size); /* Avoid copying until CR reached */
  17.   if (p == NULL)                /* This is the trivial case */
  18.     {
  19.       *new_size = size;
  20.       return (0);
  21.     }
  22.   src = dst = p - buf;          /* Start copying here */
  23.   while (src < size)
  24.     {
  25.       if (buf[src] == '\r')     /* CR? */
  26.         {
  27.           ++src;                /* Skip the CR */
  28.           if (src >= size)      /* Is it the final char? */
  29.             {
  30.               *new_size = dst;  /* Yes -> don't include in new_size, */
  31.               return (1);       /*        notify caller              */
  32.             }
  33.           if (buf[src] != '\n') /* CR not followed by LF? */
  34.             --src;              /* Yes -> copy the CR */
  35.         }
  36.       buf[dst++] = buf[src++];  /* Copy a character */
  37.     }
  38.   *new_size = dst;
  39.   return (0);
  40. }
  41.