Commit 68af0091 authored by jan.koester's avatar jan.koester
Browse files

test

parent 111d6deb
Loading
Loading
Loading
Loading
+43 −0
Changes for parser.cpp: 43 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1726,6 +1726,49 @@ std::string Parser::expand_function(const std::string& name, const std::string&
        return "";
    }

    if (name == "file") {
        // $(file OP FILENAME[,TEXT]) — OP is <, > or >>.
        //   $(file < name)        read file contents (one trailing newline stripped)
        //   $(file > name,text)   write text (truncate); a trailing newline is added
        //   $(file >> name,text)  append text
        std::string a = do_expand(args, mf, auto_vars);
        size_t p = a.find_first_not_of(" \t");
        if (p == std::string::npos) return "";
        std::string op;
        if (a.compare(p, 2, ">>") == 0) { op = ">>"; p += 2; }
        else if (a[p] == '>')          { op = ">";  p += 1; }
        else if (a[p] == '<')          { op = "<";  p += 1; }
        else return "";
        p = a.find_first_not_of(" \t", p);
        std::string rest = (p == std::string::npos) ? "" : a.substr(p);
        if (op == "<") {
            std::string fname = rest;
            size_t e = fname.find_last_not_of(" \t");
            fname = (e == std::string::npos) ? "" : fname.substr(0, e + 1);
            std::ifstream f(fname);
            if (!f.is_open()) return "";
            std::stringstream ss;
            ss << f.rdbuf();
            std::string content = ss.str();
            if (!content.empty() && content.back() == '\n') content.pop_back();
            return content;
        }
        // write / append
        size_t comma = rest.find(',');
        std::string fname = (comma == std::string::npos) ? rest : rest.substr(0, comma);
        std::string text  = (comma == std::string::npos) ? "" : rest.substr(comma + 1);
        size_t e = fname.find_last_not_of(" \t");
        fname = (e == std::string::npos) ? "" : fname.substr(0, e + 1);
        if (!fname.empty()) {
            std::ofstream f(fname, (op == ">>") ? std::ios::app : std::ios::trunc);
            if (f.is_open()) {
                f << text;
                if (text.empty() || text.back() != '\n') f << '\n';
            }
        }
        return "";
    }

    if (name == "warning") {
        std::string text = do_expand(args, mf, auto_vars);
        std::cerr << "makeplus: *** warning: " << text << std::endl;