LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, const char *mode) { LoadF lf; int status, readstatus; int c; int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ if (filename == NULL) { lua_pushliteral(L, "=stdin"); lf.f = stdin; } else { lua_pushfstring(L, "@%s", filename); lf.f = fopen(filename, "r"); if (lf.f == NULL) return errfile(L, "open", fnameindex); } if (skipcomment(&lf, &c)) /* read initial portion */ lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */ if (c == LUA_SIGNATURE[0] && filename) /* binary file? */ { lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ if (lf.f == NULL) return errfile(L, "reopen", fnameindex); skipcomment(&lf, &c); /* re-read initial portion */ } if (c != EOF) lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode); readstatus = ferror(lf.f); if (filename) fclose(lf.f); /* close file (even in case of errors) */ if (readstatus) { lua_settop(L, fnameindex); /* ignore results from `lua_load' */ return errfile(L, "read", fnameindex); } lua_remove(L, fnameindex); return status; }
LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { LoadF lf; int status, readstatus; int c; int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ lf.extraline = 0; if (filename == NULL) { lua_pushliteral(L, "=stdin"); lf.f = stdin; } else { lua_pushfstring(L, "@%s", filename); lf.f = fopen(filename, "r"); if (lf.f == NULL) return errfile(L, "open", fnameindex); } c = getc(lf.f); if (c == '#') { /* Unix exec. file? */ lf.extraline = 1; while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */ if (c == '\n') c = getc(lf.f); } if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ if (lf.f == NULL) return errfile(L, "reopen", fnameindex); /* skip eventual `#!...' */ while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ; lf.extraline = 0; } ungetc(c, lf.f); status = lua_load(L, getF, &lf, lua_tostring(L, -1)); readstatus = ferror(lf.f); if (filename) fclose(lf.f); /* close file (even in case of errors) */ if (readstatus) { lua_settop(L, fnameindex); /* ignore results from `lua_load' */ return errfile(L, "read", fnameindex); } lua_remove(L, fnameindex); return status; }
cppdom::Document PredTest::loadGameDoc() { cppdom::ContextPtr ctx( new cppdom::Context ); cppdom::Document doc( ctx ); std::string filename(cppdomtest::game_xml_filename); try { doc.loadFile( filename ); } catch (cppdom::Error e) { cppdom::Location where( ctx->getLocation() ); std::string errmsg = e.getStrError(); // print out where the error occured std::cout << filename << ":" << where.getLine() << " " << "at position " << where.getPos() << ": error: " << errmsg.c_str() << std::endl; // print out line where the error occured std::ifstream errfile( filename.c_str() ); if(!errfile) { std::cerr << "Can't open file [" << filename << "] to output error" << std::endl; } int linenr = where.getLine(); char linebuffer[1024]; for(int i=0; i<linenr && !errfile.eof(); i++) errfile.getline( linebuffer,1024 ); int pos = where.getPos(); if (pos>=80) pos %= 80; std::string err_line( linebuffer + (where.getPos()-pos) ); if (err_line.length()>=79) err_line.erase(79); std::cout << err_line << std::flush << err_line.c_str() << std::endl << linebuffer << std::endl; for(int j=2;j<pos;j++) std::cout << " "; std::cout << '^' << std::endl; } return doc; }
LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { LoadF lf; lua_Chunkreader chunkReader = getF; int status, readstatus; int c; int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ if (filename == NULL) { lua_pushliteral(L, "=stdin"); lf.f = stdin; } else { lua_pushfstring(L, "@%s", filename); lf.f = fopen(filename, "r"); } if (lf.f == NULL) return errfile(L, fnameindex); /* unable to open file */ c = ungetc(getc(lf.f), lf.f); if (!(isspace(c) || isprint(c)) && lf.f != stdin) { /* binary file? */ lua_WChar w; fclose(lf.f); lf.f = fopen(filename, "rb"); /* reopen in binary mode */ if (lf.f == NULL) return errfile(L, fnameindex); /* unable to reopen file */ if (fread(&w, 1, 2, lf.f) == 2 && w == 0xFEFF) chunkReader = getWF; else fseek(lf.f, 0, SEEK_SET); } status = lua_load(L, chunkReader, &lf, lua_tostring(L, -1)); readstatus = ferror(lf.f); if (lf.f != stdin) fclose(lf.f); /* close file (even in case of errors) */ if (readstatus) { lua_settop(L, fnameindex); /* ignore results from `lua_load' */ return errfile(L, fnameindex); } lua_remove(L, fnameindex); return status; }
void fmycatall(char *filename) { int c; FILE *file; file=fopen(filename,"rt"); if(file == NULL) errfile(); while((c=fgetc(file)) != EOF) { fputc(c,stdout); } fclose(file); }
void fmycatn(char *filename, int n) { int count=0; int c=0; FILE *file; file=fopen(filename,"rt"); if(file == NULL) errfile(); while((c=fgetc(file)) != EOF && count < n) { fputc(c,stdout); if(c == '\n')// считаем количество строк count++; } fclose(file); }
void xmlerror::show_line(XMLContextPtr c, string filename) { xmllocation where(c->get_location()); ifstream errfile(filename.c_str()); int linenr = where.get_line(); char linebuffer[1024]; for(int i=0;i<linenr&&!errfile.eof();i++) errfile.getline(linebuffer,1024); int pos = where.get_pos(); if (pos>=80) pos%=80; string line(linebuffer + (where.get_pos()-pos)); if (line.length()>=79) line.erase(79); cout << line.c_str() << endl; for(int j=2;j<pos;j++) cout << ' '; cout << '^' << endl; }
LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { LoadF lf; int status, readstatus; int c; char fullFilename[128]; int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ lf.extraline = 0; if (filename == NULL) { lua_pushliteral(L, "=stdin"); lf.f = stdin; } else { // Note: Always open as the requested file, Lua should not care about // our crazy directory remapping. lua_pushfstring(L, "@%s", filename); #if (PS3) // On the PS3, check always the /app_home/Lua/ directory! // This works both for dev-testing via remote HDD and on the game disc. // These work probably too just for developing, but app_home is fine! // /host_root/Lua/ or /dev_bdvd/PS3_GAME/USRDIR/Lua/ CheckForValidFilenameWithPath(fullFilename, filename, "/app_home/Lua/"); #elif XBOX // For the Xbox we have to make sure always to load files from D:\ because // fopen ALWAYS expects a full paths, there are no current directories on the // Xbox 360 and therefore no relative paths! Check always "D:\Lua\<file>" CheckForValidFilenameWithPath(fullFilename, filename, "D:\\Lua\\"); #else // On the PC we just use the default search logic (see luaconf.h) and we // don't care about directories since we will already be in the correct one! // In earlier versions we had a lot of extra checks here. strcpy(fullFilename, filename); #endif // Rest of this code is untouched, we just use fullFilename now! lf.f = fopen(fullFilename, "r"); if (lf.f == NULL) return errfile(L, "open", fnameindex); } c = getc(lf.f); if (c == '#') { /* Unix exec. file? */ lf.extraline = 1; while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */ if (c == '\n') c = getc(lf.f); } if (c == LUA_SIGNATURE[0] && fullFilename) { /* binary file? */ lf.f = freopen(fullFilename, "rb", lf.f); /* reopen in binary mode */ if (lf.f == NULL) return errfile(L, "reopen", fnameindex); /* skip eventual `#!...' */ while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ; lf.extraline = 0; } ungetc(c, lf.f); status = lua_load(L, getF, &lf, lua_tostring(L, -1)); readstatus = ferror(lf.f); if (filename) fclose(lf.f); /* close file (even in case of errors) */ if (readstatus) { lua_settop(L, fnameindex); /* ignore results from `lua_load' */ return errfile(L, "read", fnameindex); } lua_remove(L, fnameindex); return status; }
/* Write out a compressed dictionary graph, using only one node from * each partition group. */ static void writetreetofile(int groupcount) { int *xtable, *groups; treenode *node; arc lastarc; long i, n, g; lastarc = 0; setarcend(lastarc, TRUE); xtable = xmalloc((groupcount + 1) * sizeof(int)); groups = xmalloc((groupcount + 1) * sizeof(int)); memset(groups, 0, (groupcount + 1) * sizeof(int)); g = 0; for (n = 1, node = wordtreeroot ; n <= nodecount ; ++n, ++node) { if (node->wordend) continue; i = node->group; if (!groups[i]) { groups[i] = ++g; xtable[g] = n; } node->group = groups[i]; } dictfinalstates = g + 1; for (n = 1, node = wordtreeroot ; n <= nodecount ; ++n, ++node) { if (!node->wordend) continue; i = node->group; if (!groups[i]) { if (node->arccount) { groups[i] = ++g; xtable[g] = n; } else { groups[i] = groupcount; xtable[groupcount] = n; node->arccount = 1; node->arcs = &lastarc; } } node->group = groups[i]; } groups[1] = 0; for (g = 1 ; g < groupcount ; ++g) groups[g + 1] = groups[g] + wordtree[xtable[g]].arccount; dictarccount = groups[groupcount] + wordtree[xtable[groupcount]].arccount; for (node = wordtreeroot ; node->group ; ++node) node->group = groups[node->group]; dictfinalstates = groups[dictfinalstates]; report("%ld bytes for the completed dictionary file.\n", dictfilesize()); currfilename = dictfilename; if (!(currfp = fopen(currfilename, "wb"))) errfile(); if (writefilehead(currfp)) errfile(); for (g = 1 ; g <= groupcount ; ++g) { node = wordtree + xtable[g]; for (i = 0 ; i < node->arccount ; ++i) setarc(node->arcs[i], getarcletter(node->arcs[i]), FALSE, wordtree[getarcnext(node->arcs[i])].group); setarcend(node->arcs[i - 1], TRUE); if (writefilenodes(currfp, node->arcs, node->arccount)) errfile(); } closefile(); }
/* Takes an uncompressed dictionary graph and reorganizes it, creating * the treenode structures, and making all of a node's arcs contiguous * in memory. */ static void serializetree(void) { arc temparcs[MAX_ALPHABET]; arc *arcset; treearc *arcpool, *tarc, *ta; treenode *node; unsigned char count; long i, n; currfilename = "temporary file"; if (!(currfp = tmpfile())) errfile(); for (arcpool = arcpools ; arcpool ; arcpool = arcpool->sibling) { for (tarc = arcpool + 1 ; tarc != arcpool->child ; ++tarc) { if (!tarc->nodeid) continue; if (fwrite(&tarc->nodeid, sizeof tarc->nodeid, 1, currfp) != 1) errfile(); if (fwrite(&tarc->wordend, sizeof tarc->wordend, 1, currfp) != 1) errfile(); count = 0; if (tarc->letter) { memset(temparcs, 0, sizeof temparcs); for (ta = tarc ; ta ; ta = ta->sibling, ++count) { n = ta->letter; setarcletter(temparcs[n], n); setarcnext(temparcs[n], ta->child->nodeid); } if (fwrite(&count, sizeof count, 1, currfp) != 1) errfile(); for (n = 1 ; n < MAX_ALPHABET ; ++n) { if (temparcs[n]) if (fwrite(&temparcs[n], sizeof(arc), 1, currfp) != 1) errfile(); } } else { if (fwrite(&count, sizeof count, 1, currfp) != 1) errfile(); } } } report("%ld bytes in temporary file.\n", ftell(currfp)); rewind(currfp); for (arcpool = arcpools ; arcpool ; arcpool = tarc) { tarc = arcpool->sibling; free(arcpool); } report("Allocating %d bytes for the ordered tree.\n", (nodecount + 2) * sizeof(treenode) + (nodecount - 1) * sizeof(arc) * 2); arcpools = NULL; wordtree = xmalloc((nodecount + 2) * sizeof(treenode)); arcset = arcsetpool = xmalloc((nodecount - 1) * sizeof(arc) * 2); wordtreeroot = wordtree + 1; for (i = 0 ; i < nodecount ; ++i) { if (fread(&n, sizeof n, 1, currfp) != 1) errfile(); node = wordtree + n; if (fread(&count, sizeof count, 1, currfp) != 1) errfile(); node->wordend = count ? 1 : 0; if (fread(&count, sizeof count, 1, currfp) != 1) errfile(); node->arcs = arcset; arcset += count; node->parcs = arcset; arcset += count; node->arccount = count; if (count) { if (fread(node->arcs, count * sizeof(arc), 1, currfp) != 1) errfile(); } } closefile(); }
/* Reads through all the given files for words, and buids an * uncompressed and disorganized dictionary graph, in which the nodes * are represented as linked lists of arcs. */ static int readwordlists(int filecount, char *files[], treearc *root) { treearc *tarc; char word[WORDBUFSIZ]; unsigned char *ltr; int clear = FALSE; long count = 0; int usestdin; int i, n; memset(frequencies, 0, sizeof frequencies); for (i = 0 ; i < filecount ; ++i) { usestdin = files[i][0] == '-' && files[i][1] == '\0'; if (usestdin) { currfilename = "standard input"; currfp = stdin; } else { currfilename = files[i]; if (!(currfp = fopen(files[i], "r"))) errfile(); } report("Reading words from %s ...\n", currfilename); while (fgets(word, sizeof word, currfp)) { n = checkover(word); if (n == sizeof word - 1) { fprintf(stderr, "Overlong word \"%s...\" rejected\n", word); clear = TRUE; continue; } if (clear || !n) { clear = FALSE; continue; } tarc = root; for (ltr = (unsigned char*)word ; *ltr ; ++ltr) ++frequencies[*ltr]; for (ltr = (unsigned char*)word ; *ltr ; ++ltr) { while (tarc->letter != *ltr && tarc->sibling) tarc = tarc->sibling; if (tarc->letter != *ltr) { if (tarc->letter) tarc = tarc->sibling = grabnewtreearc(*ltr, 0); else tarc->letter = *ltr; } if (!tarc->child) break; tarc = tarc->child; } while (*ltr++) { tarc->child = grabnewtreearc(*ltr, ++nodecount); tarc = tarc->child; } tarc->wordend = TRUE; ++count; } if (usestdin) { currfp = NULL; currfilename = NULL; } else closefile(); } if (!count) { fprintf(stderr, "No words found - nothing to build.\n"); return 0; } report("\n%d words, %d nodes, %d arcs\n", count, nodecount, arccount); return count; }