On Thursday 14 July 2011 16:36:58 Vincent Torri wrote:
> raster added a 'dos2unix' function in epp to remove trailing \r (rev
> 59799).

As I understand it, it converts '\r\n' to '\n' in a string.


> However, when i compare the files generated by epp on Windows and
> linux (just after line 751 of edje_cc_parse.c), they have a different size
> (windows one is bigger). When I run the dos2unix tool I have on the
> windows file, then there is no difference between the unix and window file
> anymore.

Where exactly is the difference in the file? Does it have multiple lines at 
all? Try running hexdump/hd on it to get a bytewise comparison.


There is a problem with the dos2unix function, it reads beyond the supplied 
buffer if the last byte is a '\r'. I don't think this is actually a problem, 
because the allocated buffer is two bytes (why two?) larger than the file's 
size. Also, it allocates another, even larger buffer although it can only 
shrink the required length. I'm not sure if any combination of events there 
can lead to any weirdness. Just to be sure, can you try the attached patch.


Cheers!


Uli
Index: cpplib.c
===================================================================
--- cpplib.c	(revision 61414)
+++ cpplib.c	(working copy)
@@ -5529,30 +5529,34 @@
 
 #endif /* USE_FILE_NAME_MAPS */
 
-static int
-dos2unix(cpp_buffer *fp, int length)
+/* normalize line endings
+Any of '\r', '\n' and '\r\n' are replaced with a single '\n' in place. Returns
+the new length of the buffer. Note that no trailing null byte is inserted or
+expected. */
+static size_t
+any2unix(char* buffer, size_t length)
 {
-   unsigned char *tbuf;
-   int nlen = 0, i;
-   
-   tbuf = xmalloc(length + 4);
-   if (!tbuf) return length;
-   for (i = 0; i < length; i++)
+   size_t i = 0;
+   size_t j = 0;
+   for (; i!=length; ++i)
      {
-        if ((fp->buf[i] == '\r') &&
-            (fp->buf[i + 1] == '\n'))
+        char c = buffer[i++];
+        if (c == '\r')
+          {
+             // replace '\r' with '\n'
+             buffer[j++] = '\n';
+             // skip an optionally following '\n' in the input sequence
+             if (i != length && buffer[i] == '\n')
+                ++i;
+          }
+        else
           {
-             // skip \r in \r\n
-             continue;
+             // copy all other characters as-is
+             buffer[j++] = c;
           }
-        tbuf[nlen] = fp->buf[i];
-        nlen++;
      }
-   tbuf[nlen] = 0;
-   
-   free(fp->buf);
-   fp->buf = tbuf;
-   return nlen;
+
+   return j;
 }
 
 /* Process the contents of include file FNAME, already open on descriptor F,
@@ -5597,8 +5601,9 @@
 	/* Read the file contents, knowing that st_size is an upper bound
 	 * on the number of bytes we can read.  */
 	length = safe_read(f, (char *)fp->buf, st_size);
-        length = dos2unix(fp, length);
-        
+    length = any2unix(fp->buf, length);
+    fp->buf[length] = 0;
+
 	fp->alimit = fp->buf + st_size + 2;
 	fp->cur = fp->buf;
 	fp->rlimit = fp->buf + length;
@@ -5634,7 +5639,8 @@
 	     fp->buf = (unsigned char *)xrealloc(fp->buf, bsize + 2);
 	  }
 	length = st_size;
-        length = dos2unix(fp, length);
+        length = any2unix(fp->buf, length);
+        fp->buf[length] = 0;
    }
 
    if ((length > 0 && fp->buf[length - 1] != '\n')
------------------------------------------------------------------------------
AppSumo Presents a FREE Video for the SourceForge Community by Eric 
Ries, the creator of the Lean Startup Methodology on "Lean Startup 
Secrets Revealed." This video shows you how to validate your ideas, 
optimize your ideas and identify your business strategy.
http://p.sf.net/sfu/appsumosfdev2dev
_______________________________________________
enlightenment-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/enlightenment-devel

Reply via email to