2
0
Эх сурвалжийг харах

easy the way to accept other modifiers for 'mode' in 'io.open'

Roberto Ierusalimschy 9 жил өмнө
parent
commit
71344b5cac
1 өөрчлөгдсөн 13 нэмэгдсэн , 7 устгасан
  1. 13 7
      liolib.c

+ 13 - 7
liolib.c

@@ -1,5 +1,5 @@
 /*
-** $Id: liolib.c,v 2.146 2015/07/07 17:03:34 roberto Exp roberto $
+** $Id: liolib.c,v 2.147 2015/07/15 14:40:28 roberto Exp roberto $
 ** Standard I/O (and system) library
 ** See Copyright Notice in lua.h
 */
@@ -23,18 +23,24 @@
 #include "lualib.h"
 
 
-#if !defined(l_checkmode)
+
 
 /*
-** Check whether 'mode' matches '[rwa]%+?b?'.
 ** Change this macro to accept other modes for 'fopen' besides
 ** the standard ones.
 */
+#if !defined(l_checkmode)
+
+/* accepted extensions to 'mode' in 'fopen' */
+#if !defined(L_MODEEXT)
+#define L_MODEEXT	"b"
+#endif
+
+/* Check whether 'mode' matches '[rwa]%+?[L_MODEEXT]*' */
 #define l_checkmode(mode) \
 	(*mode != '\0' && strchr("rwa", *(mode++)) != NULL &&	\
-	(*mode != '+' || ++mode) &&  /* skip if char is '+' */	\
-	(*mode != 'b' || ++mode) &&  /* skip if char is 'b' */	\
-	(*mode == '\0'))
+	(*mode != '+' || (++mode, 1)) &&  /* skip if char is '+' */	\
+	(strspn(mode, L_MODEEXT) == strlen(mode)))
 
 #endif
 
@@ -469,7 +475,7 @@ static int read_line (lua_State *L, FILE *f, int chop) {
   int c = '\0';
   luaL_buffinit(L, &b);
   while (c != EOF && c != '\n') {  /* repeat until end of line */
-    char *buff = luaL_prepbuffer(&b);  /* pre-allocate buffer */
+    char *buff = luaL_prepbuffer(&b);  /* preallocate buffer */
     int i = 0;
     l_lockfile(f);  /* no memory errors can happen inside the lock */
     while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n')