-
Notifications
You must be signed in to change notification settings - Fork 2
/
db_addVideoDbRecord.m
54 lines (47 loc) · 1.78 KB
/
db_addVideoDbRecord.m
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
function [ added, msg ] = db_addVideoDbRecord( videoName, extension, indexMap, indexMapLocations )
%ADDVIDEODBINDEX Summary of this function goes here
% Assumes inputs are correct, so not performing input validation.
% Will not be used by any function other than db_insertVideo().
%% Set initial output vars
added = true;
msg = 'addVideoDbRecord: ';
%% Load dbConfig
load(db_getDbConfigFileURI());
%% Input Formatting
% Pad extension with 0, if length is less than maxExtensionChars
extension = sprintf('%0*s', maxExtensionChars, extension);
%% Open videoDbRecordFile and move write pointer to correct position
fileID = fopen(videoDbRecordFile, 'r+');
if fileID == -1
added = false;
msg = [msg, videoDbRecordFile, ': ', ferror(fileID)];
return;
end
currentBytePos = ftell(fileID);
if currentBytePos == -1
added = false;
msg = [msg, videoDbRecordFile, ': ', ferror(fileID)];
fclose(fileID);
return;
end
insertBytePos = sizeof(headerIntType) + (videoName-1)*(videoDbRecordByteSize);
offset = insertBytePos - currentBytePos;
if fseek(fileID, offset, 'cof') == -1
added = false;
msg = [msg, videoDbRecordFile, ': ', ferror(fileID)];
fclose(fileID);
return;
end
%% Write Record to videoDbRecordFile
% In deletion of a video, videoName would change to 0 and indexMap(0)
% would point to position of next deleted video
fwrite(fileID, videoName, videoNameIntType);
fwrite(fileID, extension, extensionType);
% TODO: can it be optimized in terms of writes?
len = length(indexMap);
for i = 1 : len
fwrite(fileID, indexMap(i), indexClassIntType);
fwrite(fileID, indexMapLocations(i), bytePositionIntType);
end
fclose(fileID);
end