/* * Processor.cpp * stepmania * * Created by Steve Checkoway on Sun Sep 07 2003. * Copyright (c) 2003 Steve Checkoway. All rights reserved. * */ using namespace std; #include #include #include #include #include #include #include "StdString.h" #include "Processor.h" #include "Util.h" bool IsAVar(const CString& var) { return var[0] == '$'; } CString StripVar(const CString& var) { if (!IsAVar(var)) return var; return var.substr(1, var.npos); } Processor::Processor(CString& path, handleFileFunc f, getPathFunc p, askFunc a, bool i) : mDoGoto(false), mHandleFile(f), mGetPath(p), mAsk(a), mError(Processor::DefaultError), mInstalling(i) { char cwd[MAXPATHLEN]; getwd(cwd); mCWD = cwd; mConditionals["INSTALLING"] = i; if (mInstalling) { if (!DoesFileExist(path)) throw CString("Archive file not present."); char archivePath[] = "/tmp/installerXXXXXX.tar"; int fd = mkstemps(archivePath, 4); if (fd == -1) throw CString("Couldn't create temporary file"); mPath = archivePath; char toolPath[] = "/usr/bin/gunzip"; try { if(CallTool2(true, -1, fd, -1, toolPath, "-c", path.c_str(), NULL)) throw CString("Archive file not handled correctly."); } catch (CString& e) { close(fd); unlink(mPath); throw; } close(fd); } else mPath = path; } Processor::~Processor() { if (mInstalling) unlink(mPath); } void Processor::ProcessLine(const CString& line, unsigned& nextLine) { CStringArray parts; split(line, ":", parts); nextLine++; if (parts.size() == 0) return; #pragma mark LABEL if (parts[0] == "LABEL") { printf("%u: %s\n", nextLine - 1, line.c_str()); if (parts.size() != 2) goto error; if (mDoGoto && parts[1] == mLabel) { mDoGoto = false; mLabel = ""; } mLabels[parts[1]] = nextLine; return; } if (mDoGoto) return; printf("%u: %s\n", nextLine - 1, line.c_str()); #pragma mark FILE if (parts[0] == "FILE") { if (parts.size() != 3) goto error; CString file, dir; if (parts[1][0] == '/') { file = parts[1].substr(1); dir = "/"; } else { file = parts[1]; dir = mCWD; } if (mInstalling) (*mHandleFile)(file, dir, mPath, ResolveConditional(parts[2])); else { CStringArray list, dirs; if (IsADirectory((dir == "/" ? dir : dir + "/" ) + file)) { int fd = open(".", O_RDONLY, 0); chdir(dir); FileListingForDirectoryWithIgnore(file, list, dirs, mIgnore); fchdir(fd); close(fd); if (list.size() == 0) //empty directory or ignored directory (*mHandleFile)(file, dir, mPath, true); for (unsigned i=0; i(), false); for (unsigned i=0; i