mws := module()
export setplotsize, mws2string;

    setplotsize := proc(file::string
                        , newfile::string
                        , height::posint
                        , width::posint
                       )
        use StringTools in
            writebytes(newfile
                       , RegSubs( "({GLPLOT[2-3]D )[0-9 ]+"
                                  = sprintf("\\1%d %d %d "
                                            , width
                                            , height
                                            , width
                                           )
                                  , mws2string(file)
                                ));
            fclose(file,newfile);
        end use;
        NULL;
    end proc:

    mws2string := proc(file)
    local buf,line,fd;
        use StringTools,FileTools,FileTools:-Text in

            if not (Exists(file)
                    and IsReadable(file)
                    and not IsDirectory(file)) then
                error "%1 is not a readable file", file;
            elif IsOpen(file) then
                error "%1 is already open", file;
            end if;

            try
                fd := Open(file);
                buf := StringBuffer();
                line := ReadLine(file);
                while line <> NULL do
                    buf:-append(`if`(line[-2..-1] = "\\+"
                                     , line[1..-3]
                                     , line
                                    ));
                    line := ReadLine(file);
                end do;
            finally
                Close(fd);
            end try;
            buf:-value();
        end use;
    end proc:

end module:
