Guidance
指路人
g.yi.org
Software / Reginald / Examples / join.rex

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
/* Read all the lines of a text file and join lines that end with a comma. */

/* The filename */
filename = 'test.txt'

/* Where to save the converted file. This can't be the same as 'filename'.
 * In order to support that, we'd have to read 'filename' completely
 * into memory before we do our conversion, and that's not what we
 * do below
 */
tofilename = 'test2.txt'

/* Store the last, non-blank character before the end of line here, so
 * we can test for a comma at the end of the line
 */
lastchar = 0

/* To check for two end of line characters */
extrachar = ""

/* No line read yet */
line = ""

/* Open the file for reading, but only if it exists */
IF STREAM(filename, 'C', 'OPEN READ') == 'READY:' THEN DO

   /* Open the new file for writing and delete old contents */
   IF STREAM(tofilename, 'C', 'OPEN WRITE REPLACE') \== 'READY:' THEN DO
      SAY STREAM(tofilename, 'D')
      CALL STREAM filename, 'C', 'CLOSE'
      RETURN
   END

   /* Get the size of the file (ie, how many chars to read) */
   total = CHARS(filename)

   /* Any more chars? */
   DO WHILE total > 0

      /* Read the next char */
      IF extrachar == "" THEN char = CHARIN(filename, , 1)
      ELSE DO
          char = extrachar
          extrachar = ""
      END

      /* Decrement the count of characters still to read */
      total = total - 1

      /* Is it a carriage return or line feed? */
      IF char == "0A"x | char == "0D"x THEN DO

         /* Yes, we have the end of a line now */

         /* Did it end with a comma? */
         IF lastchar == ',' THEN DO

            /* It ended with a comma. So just keep reading the
             * next line, and append it to the line we just read. We
             * won't write out anything yet. But first, we do need
             * to trim off that trailing comma and replace it with
             * a space
             */
            offset = LASTPOS(',', line)
            IF offset > 1 THEN DO
               line = STRIP(LEFT(line, offset - 1), 'T')
            END
            line = line || " "

            /* No lastchar yet */
            lastchar = 0

            /* Now the tricky part. The line may or may not have
             * another end of line character. If the char we matched
             * was a "0D"x, and the file was created on Windows,
             * we could have a "0A"x we need to discard.
             */
            IF total > 0 & char == "0D"x THEN DO

               /* Possibly get the second end of line character */
               char = CHARIN(filename, , 1)

               /* If not a second end of line char, use it for the next loop */
               IF char \== "0A"x THEN DO
                  extrachar = char
               END

               /* Discard the second end of line character */
               ELSE DO
                  total = total - 1
               END

            END /* Test for a second end of line char */

         END

         ELSE DO

            /* It didn't end with a comma. So write out the line now, and add
             * one or two end of line characters
             */
            IF line \== "" THEN CALL LINEOUT tofilename, line

            /* No lastchar yet */
            lastchar = 0

            /* No chars of this line read yet */
            line = ""

         END

      END /* IF "0A"x | "0D"x */

      /* Not the end of the line yet */
      ELSE DO

         /* Append the char to the line */
         line = line || char

         /* Save as the lastchar read if not a space */
         IF char \== ' ' & char \== "09"x THEN lastchar = char

      END  /* Not the end of the line */

      CATCH NOTREADY
	SAY STREAM(tofilename, 'D')
	line = ""

   END /* WHILE total > 0 */

   /* Write out any remaining line, adding end of line characters */
   IF line \== "" THEN CALL LINEOUT tofilename, line

   /* Close the files */
   CALL STREAM filename, 'C', 'CLOSE'
   CALL STREAM tofilename, 'C', 'CLOSE'

END /* STREAM() */

/* File can't be opened. SAY why */
ELSE SAY STREAM(filename, 'D')

RETURN
掌柜推荐
 
 
¥420.00 ·
 
 
¥470.00 ·
 
 
¥890.00 ·
 
 
¥660.00 ·
 
 
¥748.00 ·
 
 
¥1,800.00 ·
© Sun 2024-11-24  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0 Last modified:2010-07-16 20:46:05