/usr/share/octave/packages/io-2.4.5/dbfread.m is in octave-io 2.4.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | ## Copyright (C) 2014-2016 Philip Nienhuis
##
## This program is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
## -*- texinfo -*-
## @deftypefn {Function File} [@var{data}, @var{datinfo}] = dbfread (@var{fname})
## @deftypefnx {Function File} [@var{data}, @var{datinfo}] = dbfread (@var{fname}, @var{recs})
## @deftypefnx {Function File} [@var{data}, @var{datinfo}] = dbfread (@var{fname}, @var{recs}, @var{cols})
## @deftypefnx {Function File} [@var{data}, @var{datinfo}] = dbfread (@var{fname}, @var{recs}, @var{cols}, @var{re})
## Read contents of a dbase (dbf) file, provisionally dbase III+, IV or V.
##
## @itemize
## @item
## @var{fname} should be the name of a valid dbase file; the file extension
## isn't required.
##
## @item
## @var{recs} can be an integer or logical array containing record numbers or
## record indicators for those records that need to be returned. If omitted,
## all records are read. Indices supplied in @var{recs} can be specified in
## any order, but the returned data are sorted in order of records in the file.
##
## @item
## @var{cols} can be a logical, integer, cellstr or character array indicating
## from which file columns the data should be returned. If a numeric array is
## supplied, it is considered to be like a logical array if the maximum entry
## value equals 1. Character arrays should have column names stacked in the
## vertical (first) dimension. @var{cols} cellstr or char arrays can be
## supplied in any order, yet the returned data column order matches that of
## the columns order in the dbase file. For dbase files containing multiple
## columns with the same name, specify a numeric or logical array to select
## columns to be returned. If omitted, data from all file columns are
## returned.
##
## @item
## If a value of 1 or true is entered for @var{re}, dbfread also tries to
## return data from erased records. No guarantee can be given for these data
## to be correct or consistent! If omitted, erased records are skipped.
##
## @item
## Return value @var{data} is a N+1 x M cellstr array where the uppermost row
## contains the column names and the rest of the rows the record data.
##
## @item
## Optional return argument @var{datinfo} is a struct array containing various
## information of the dbase file and record build-up.
## @end itemize
##
## Arguments @var{recs} and @var{cols} need not be as long as the number of
## records and columns in the file, resp.; dbfread will stop reading data if
## any of @var{recs} or @var{cols} (if supplied) is exhausted.
##
## Sometimes dbase files contain records indicated as being erased. The data
## in such records is silently skipped, unless the @var{re} flag is set
## and/or @var{recs} is supplied and erased records happen to be present in the
## requested record numbers.
##
## Examples:
##
## @example
## A = dbfread ("file.dbf");
## (returns all data in file.dbf in array A)
## @end example
##
##
## @example
## [A, B] = dbfread ("file.dbf", [], ["colB"; "colF"]);
## (returns all data in columns named "colB" and "colF" from
## file.dbf in array A and information on the database
## build-up in struct B)
## @end example
##
## @example
## A = dbfread ("file.dbf", [0 1 0 0 1 0 0]);
## -or-
## A = dbfread ("file.dbf", [2 5]);
## (returns data from record numbers 2 and 5 in
## file.dbf in array A)
## @end example
##
## @example
## A = dbfread ("file", [0 1 0 0 1 0]);
## (returns data from record numbers 2 and 5 in
## file.dbf in array A)
## @end example
##
## @example
## [~, B] = dbfread ("file.dbf", 0);
## (to returns info on column names and number of
## records, plus more info)
## @end example
##
## @example
## [A] = dbfread ("file", [], @{"Header1", "Col5"@});
## (returns data from columns with names (headers)
## Header1 and Col5, resp.)
## @end example
##
## @seealso{xlsread}
## @end deftypefn
## Author: Philip Nienhuis <prnienhuis@users.sf.net>
## Created: 2014-11-03
## References:
## http://ulisse.elettra.trieste.it/services/doc/dbase/DBFstruct.htm
## http://www.dbf2002.com/dbf-file-format.html
## http://www.dbase.com/KnowledgeBase/int/db7_file_fmt.htm
function [data, datinfo] = dbfread (fname, recs=[], cols=[], rd_erased='')
## Check file name
if (ischar (fname))
[~, fnm, ext] = fileparts (fname);
if (isempty (ext))
fname = [fname ".dbf"];
endif
else
error ("dbfread: file name expected for arg # 1.\n");
endif
## Check recs arg. If needed turn into indices
if (! isempty (recs))
if (! (isnumeric (recs) || islogical (recs)))
error ("dbfread: numeric or logical array expected for arg # 2\n");
elseif (isnumeric (recs))
if (any (recs < 0))
error ("dbfread: illegal record selection indices\n");
elseif (min (recs) == 0 && max (recs) < 2)
recs = find (recs);
if (isempty (recs))
recs = -1;
endif
endif
elseif (islogical (recs))
recs = find (recs);
endif
endif
## Check cols arg. If needed turn into indices
if (! isempty (cols))
if (! (isnumeric (cols) || ischar (cols) || iscellstr (cols) || islogical (cols)))
error ("dbfread: numeric, cellstr, logical, or character array expected for arg # 3\n");
elseif (isnumeric (cols))
if (any (cols < 0))
error ("dbfread: illegal column selection indices\n");
elseif (min (cols) == 0 && max (cols) < 2)
cols = find (cols);
if (isempty (cols))
cols = -1;
endif
endif
elseif (islogical (cols))
cols = find (cols);
endif
endif
## Check rd_erased arg.
if (! isempty (rd_erased) && ! (islogical (rd_erased) || isnumeric (rd_erased)))
error ("dbfread: numeric or logical value expected for arg # 4\n");
endif
## Open file
fid = fopen (fname, "r");
if (fid < 0)
error ("dbfread: file %s couldn't be opened.\n", fname);
endif
## Rewind, just to be sure
fseek (fid, 0, "bof");
## First check proper type
fbyte = uint8 (fread (fid, 1, "uint8"));
## Provisional type check.
## 3 = dbase III+ w/o memos
## 83 = dbase III+ w memos
## ... <to be cont'd>
vsn = bitand (fbyte, 7);
if (! ismember (vsn, [3, 4, 5]))
error ("dbfread: unsupported file type, only dbase III[+], IV & V supported.\n");
endif
## Memos present for fbyte == 83, and bits 3 and/or 7 set (1-based bit pos.)
hasmemo = (fbyte == 83) || (bitand (fbyte, 8)) || (bitand (fbyte, uint8 (128)));
## Start reading header
lasty = fread (fid, 1, "uint8") + 1900; ## Last dbf update
lastm = fread (fid, 1, "uint8"); ## month
lastd = fread (fid, 1, "uint8"); ## day
nrecs = fread (fid, 1, "uint32");
lhdr = fread (fid, 1, "uint16");
recl = fread (fid, 1, "uint16");
## Field descriptors
nfields = 0;
fseek (fid, 32, "bof");
fdesc = fread (fid, 32, "char=>char")';
do
++nfields;
## Get fields into struct
dbf(nfields).fldnam = deblank (fdesc(1:11)); ## Field name
dbf(nfields).fldtyp = fdesc(12); ## Field type
## Skip field dspl.
dbf(nfields).fldlng = int32 (fdesc(17)); ## Field length
dbf(nfields).flddec = int32 (fdesc(18)); ## Decimal places
dbf(nfields).fldflg = int8 (fdesc(19)); ## Flags
## Get next field descriptors
fdesc = fread (fid, 32, "char=>char")';
until (ftell (fid) >= lhdr)
## Seek to position after header terminator byte
fseek (fid, lhdr, "bof");
## Read rest of data. Skip if no records need be read. Turn into char array
if (recs > 0 || isempty (recs))
txt = fread (fid, [recl, nrecs], "char=>char")';
else
txt = "";
endif
## .dbf file is no longer needed
fclose (fid);
## Preallocate upper data row
data = cell (1, numel (dbf));
data(1, :) = {dbf.fldnam};
## If required, select requested records. Beware; truncate indices > nrecs
recs = sort (recs);
if (any (recs > nrecs))
recs (find (recs > nrecs)) = [];
## Check if we still have a selection...
if (isempty (recs))
## No more, signal this to below code
recs = -1;
endif
endif
if (! isempty (recs) && ! isempty (txt))
if (any (recs < 0))
## No data returned. Explore erased records anyway
wipedrec = txt(:, 1)' == "*";
txt = "";
recs = 0;
scol = numel (dbf);
else
txt = txt(recs, :);
## Preallocate data cell array for selected records
data = [data; cell(numel (recs), numel (dbf))];
endif
else
## Preallocate data cell array for all records
data = [data; cell(nrecs, numel (dbf))];
endif
if (! isempty (txt))
## There's something to read ;-) First, read memo file, if any
if (hasmemo)
## Also open accompanying .dbt file
fjd = fopen ([fnm ".dbt"], "r");
if (fjd < 0)
warning ("dbfread: associated memo file (%s) couldn't be opened.\n", ...
[fnm ".dbt"]);
printf ("(dbfread: skipping memo fields)\n");
memos = {};
else
fseek (fjd,0, "bof");
## Read memo fields
memos = fread (fjd, Inf, "char=>char")';
fclose (fjd);
## Pimp memos: replace all non-alphanumeric chars by space
memos(find (int8(memos) > 122)) = char(32);
memos(find (int8(memos) < 32)) = char(32);
switch fbyte
case {83, 131} ## Dbase III[+]
## Make it into a Nx512 char array. Pad unit length = multiple of 512
pad = ceil (length (memos) / 512) * 512 - length (memos);
memos = [memos repmat(int8 (32), 1, pad)];
memos = cellstr (reshape (memos, 512, [])');
otherwise
## FIXME: Dbase V, VII to follow
endswitch
endif
endif
txtp = 2;
## Init output array column pointer
scol = 0;
for ii=1:numel (dbf)
## First process selection if arg. cols was given...
if (! isempty (cols))
try
## try-catch, as cols array < nr. of cols is allowed.
## Switch dependent of cols input arg. type
switch class (cols)
case "cell"
getcol = any (strcmp (cols{scol+1}, dbf(ii).fldnam));
case "char"
getcol = strcmp (strtrim (cols(scol+1, :)), dbf(ii).fldnam);
case "double"
getcol = ! isempty (find (cols == ii));
otherwise
endswitch
catch
getcol = 0;
end_try_catch
else
## No cols arg. was given, so we'll read this column anyway
getcol = 1;
endif
if (getcol)
## Read column # ii
++scol;
fld = txt(:, txtp : txtp+dbf(ii).fldlng - 1);
switch (dbf(ii).fldtyp)
case {"B", "G"}
## Block number into .dbt file, other than memo
data(2:end, scol) = num2cell (str2double (fld));
case "C"
## Text
data(2:end, scol) = cellstr (fld);
case "D"
## Date
dlf = cellstr (fld);
## Catch empty date fields. Put ridiculous value in
dlf(find (strcmp (cellstr (fld), "00000000"))) = "99991231";
dlf(find (cellfun ("isempty", dlf))) = "99991231";
dlf = datenum (dlf, "yyyymmdd");
## Reset temp values for empty dates
dlf (dlf >= 3652425) = 0;
data(2:end, scol) = num2cell (dlf);
case "L"
## Logical / boolean
data(2:end, scol) = false;
data(regexpi (fld, "[yt]")+1, scol) = true;
case {"F", "N"}
## Numeric
data(2:end, scol) = num2cell (str2double (fld));
case "M"
## Memo field pointer into .dbt file
if (! isempty (memos))
switch (fbyte)
case {83, 131} ## Dbase III[+]
idx = str2double (fld);
idx (find (isnan (idx))) = 1;
data(2:end, scol) = memos(idx);
otherwise
## FIXME other .dbf file versions to be implemented...
endswitch
endif
otherwise
endswitch
else
## Remove data column header & associated column from output array
data(:, scol+1) = [];
endif
## Next column of data from .dbf file
txtp += dbf(ii).fldlng;
endfor
## Only now check for erased records, to avoid false positives below
wipedrec = txt(:, 1)' == "*";
if (! rd_erased)
## No erased records in user-supplied record selection => no worries
data (find (wiperec), :) = [];
elseif (sum (wipedrec) && ! isempty (recs))
## User did request erased record # => warn
warning ("(dbfread: %d erased records read\n", wipedrec);
endif
else
wipedrec = [];
scol = 0;
endif
if (isempty (data))
data = {};
endif
if (nargout <= 1)
## Only data requested
datinfo = [];
else
## Also (or only) dbf info struct requested
if (isempty (recs))
## Infer nr. of records from file
recs = size (data, 1) - 1;
endif
datinfo.type = fbyte;
datinfo.date = datenum (lasty, lastm, lastd);
datinfo.erasedrec = find (wipedrec);
datinfo.nrec = nrecs;
datinfo.srecs = recs;
datinfo.recl = recl;
datinfo.ncols = numel (dbf);
datinfo.scols = scol;
datinfo.data = dbf;
endif
endfunction
|