File: RemoveCarriageReturns.m

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (75 lines) | stat: -rw-r--r-- 1,539 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
function RemoveCarriageReturns(folder,qRecursive,qDryRun, exts_filter)
% finds all 13s and 10s (decimal char number)
% throws out all 13s, but if not followed by 10 it replaces it by 10
%
% make sure to do a dryrun first and set the fourth argument such that no
% files you dont want to touch get touched. e.g.: {'m','c'} to process only
% *.m and *.c files in the tree


if nargin<4
    exts_filter = {};
else
    exts_filter = {exts_filter};
end

if nargin<3
    qDryRun = true;
end

if nargin<2
    qRecursive = false;
end

if qRecursive
    [folds,nfolds] = FolderFromFolder(folder,'ssilent');
    for p=1:nfolds
        RemoveCarriageReturns(fullfile(folder,folds(p).name),qRecursive,qDryRun, exts_filter{:});
    end
end

[files,nfiles] = FileFromFolder(folder,'ssilent',exts_filter{:});

if nfiles==0
    return;
end
   
for p=1:nfiles
    % open it
    fname = fullfile(folder,files(p).name);
    fid = fopen(fname,'rt');
    dat = fread(fid);
    fclose(fid);
    
    % do replace
    newdat = DoReplace(dat);
    
    % save if changed
    if ~isequal(dat,newdat)
        fprintf('%s\n',files(p).name);
        
        if ~qDryRun
            fid = fopen([folder filesep files(p).name],'wt');
            fwrite(fid,newdat);
            fclose(fid);
        end
    end
end



function dat = DoReplace(dat)

% find all 13s
q13 = dat==13;
% find all 10s
q10 = dat==10;

% find all 13s followed by 10s
q = q13(1:end-1) & q10(2:end);

% remove them
dat(q) = [];

% all other 13s are orphans, replace them by 10s
dat(dat==13) = 10;