function All = bladedPrjRead(fullFile)
% bladedPrjRead read $PJ file
%   bladedPrjRead reads $PJ project file information

%   Project: IMPACT
%   Title of Code: bladedPrjRead
%   Relevant additional documentation:
%   Developer: Carlos Gonzalez
%   External Dependencies: NA
%
%   Disclaimer
%   CAUTION: It is strongly recommended that changes to this code
%   should be avoided without first consulting with authors.

try
    fullFile = [fullFile, '.$PJ'];
    fid = fopen(fullFile);
    if fid == -1
        errordlgMLS(sprintf('Cannot open %s', fullFile));
        All = [];
        return
    end
    All = bladedPrjRead_true(fid);
    fclose(fid);
catch
    fclose(fid);
    errordlgMLS(sprintf('An error occurred when reading %s', fullFile));
    All = [];
    return
end

function All = bladedPrjRead_true(fid)
% This is the algorithm.
% Used for easy error checking by the main program.

All = [];
while 1
    tline = fgetl(fid);
    if ~ischar(tline)
        break;
    end
    endW = regexp(tline,'(\w)\s');
    if ~isempty(endW)
        word = lower(tline(1:endW(1)));
        switch word
            % version
            case 'version'
                version = sscanf(tline, '%*s %s');
                All.version = version;
            % calculation
            case 'calculation'
                calculation = sscanf(tline, '%*s %f');
                All.calculation = calculation;
            % options
            case 'options'
                options = sscanf(tline, '%*s %f');
                All.options = options;
                
            % projname
            case 'projname'
                projname = sscanf(tline, '%*s %[^\n]');
                All.projname = projname;
        end
    end
end
