private static int GIOFile(LinyeeState L, int f, CharPtr mode) { if (!LinyeeIsNoneOrNil(L, 1)) { CharPtr filename = LinyeeToString(L, 1); if (filename != null) { FilePtr pf = NewFile(L); pf.file = fopen(filename, mode); if (pf.file == null) { FileError(L, 1, filename); } } else { ToFile(L); /* check that it's a valid file file */ LinyeePushValue(L, 1); } LinyeeRawSetI(L, LINYEE_ENVIRONINDEX, f); } /* return current value */ LinyeeRawGetI(L, LINYEE_ENVIRONINDEX, f); return(1); }
/* ** function to close regular files */ private static int IoFClose(LinyeeState L) { FilePtr p = ToFilePointer(L); int ok = (fclose(p.file) == 0) ? 1 : 0; p.file = null; return(PushResult(L, ok, null)); }
/* ** this function has a separated environment, which defines the ** correct __close for 'popen' files */ private static int IoPopen(LinyeeState L) { CharPtr filename = LinyeeLCheckString(L, 1); CharPtr mode = LinyeeLOptString(L, 2, "r"); FilePtr pf = NewFile(L); pf.file = LinyeePopen(L, filename, mode); return((pf.file == null) ? PushResult(L, 0, filename) : 1); }
/* ** When creating file files, always creates a `closed' file file ** before opening the actual file; so, if there is a memory error, the ** file is not left opened. */ private static FilePtr NewFile(LinyeeState L) { FilePtr pf = (FilePtr)LinyeeNewUserData(L, typeof(FilePtr)); pf.file = null; /* file file is currently `closed' */ LinyeeLGetMetatable(L, LINYEE_FILEHANDLE); LinyeeSetMetatable(L, -2); return(pf); }
private static Stream ToFile(LinyeeState L) { FilePtr f = ToFilePointer(L); if (f.file == null) { LinyeeLError(L, "attempt to use a closed file"); } return(f.file); }
private static int IoTmpFile(LinyeeState L) { FilePtr pf = NewFile(L); #if XBOX LinyeeLError(L, "io_tmpfile not supported on Xbox360"); #else pf.file = tmpfile(); #endif return((pf.file == null) ? PushResult(L, 0, null) : 1); }
private static int IoLines(LinyeeState L) { if (LinyeeIsNoneOrNil(L, 1)) /* no arguments? */ /* will iterate over default input */ { LinyeeRawGetI(L, LINYEE_ENVIRONINDEX, IOINPUT); return(FLines(L)); } else { CharPtr filename = LinyeeLCheckString(L, 1); FilePtr pf = NewFile(L); pf.file = fopen(filename, "r"); if (pf.file == null) { FileError(L, 1, filename); } AuxLines(L, LinyeeGetTop(L), 1); return(1); } }