diff options
author | OctaneSnail <os@v12pwr.com> | 2009-01-18 14:12:23 +0300 |
---|---|---|
committer | OctaneSnail <os@v12pwr.com> | 2009-01-18 14:12:23 +0300 |
commit | 847c4930cc8ef57513df52b86df0d8087581d56b (patch) | |
tree | ea80e260a5638402f3a19e5cb79cb22d347f63b8 /File.cpp | |
parent | 358b514e12c89300b5614b8e9715cd6ecb5958a2 (diff) |
Refactor code to remove duplication of FindStartPart.
Diffstat (limited to 'File.cpp')
-rw-r--r-- | File.cpp | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/File.cpp b/File.cpp new file mode 100644 index 0000000..d36efcf --- /dev/null +++ b/File.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2008-2009, OctaneSnail <os@v12pwr.com> + * + * 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; version 2 of the License. + * + * 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/>. + */ + +#include <windows.h> +#include <streams.h> + +#include "File.h" + +static int compare (const void *pos, const void *part) +{ + if (*((LONGLONG *) pos) < ((FilePart *) part)->in_file_offset) + return -1; + + if (*((LONGLONG *) pos) >= ((FilePart *) part)->in_file_offset + ((FilePart *) part)->size) + return 1; + + return 0; +} + +int File::FindStartPart (LONGLONG position) +{ + if (position > size) + return -1; + + // Check if the previous lookup up still matches. + if (m_prev_part && !compare (&position, m_prev_part)) + return (int) (m_prev_part - array); + + m_prev_part = (FilePart *) bsearch (&position, array, parts, sizeof (FilePart), compare); + + if (!m_prev_part) + return -1; + + return (int) (m_prev_part - array); +} |