[4] | 1 | /****************************************************************************** |
---|
| 2 | * Fast DXT - a realtime DXT compression tool |
---|
| 3 | * |
---|
| 4 | * Author : Luc Renambot |
---|
| 5 | * |
---|
| 6 | * Copyright (C) 2004 Electronic Visualization Laboratory, |
---|
| 7 | * University of Illinois at Chicago |
---|
| 8 | * |
---|
| 9 | * All rights reserved. |
---|
| 10 | * |
---|
| 11 | * Redistribution and use in source and binary forms, with or without |
---|
| 12 | * modification, are permitted provided that the following conditions are met: |
---|
| 13 | * |
---|
| 14 | * * Redistributions of source code must retain the above copyright |
---|
| 15 | * notice, this list of conditions and the following disclaimer. |
---|
| 16 | * * Redistributions in binary form must reproduce the above |
---|
| 17 | * copyright notice, this list of conditions and the following disclaimer |
---|
| 18 | * in the documentation and/or other materials provided with the distribution. |
---|
| 19 | * * Neither the name of the University of Illinois at Chicago nor |
---|
| 20 | * the names of its contributors may be used to endorse or promote |
---|
| 21 | * products derived from this software without specific prior written permission. |
---|
| 22 | * |
---|
| 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
| 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
| 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
| 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
---|
| 27 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
| 28 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
---|
| 29 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
| 30 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
| 31 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
| 32 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
| 33 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 34 | * |
---|
| 35 | * Direct questions, comments etc about SAGE to http://www.evl.uic.edu/cavern/forum/ |
---|
| 36 | * |
---|
| 37 | *****************************************************************************/ |
---|
| 38 | |
---|
| 39 | #include "util.h" |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | #if defined(WIN32) |
---|
| 43 | #include <io.h> |
---|
| 44 | #endif |
---|
| 45 | |
---|
| 46 | #if !defined(WIN32) |
---|
| 47 | #include <sys/stat.h> |
---|
| 48 | #include <unistd.h> |
---|
| 49 | #endif |
---|
| 50 | |
---|
| 51 | #include <fcntl.h> |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | void *aligned_malloc(size_t size, size_t align_size) { |
---|
| 56 | |
---|
| 57 | char *ptr,*ptr2,*aligned_ptr; |
---|
| 58 | int align_mask = (int)align_size - 1; |
---|
| 59 | |
---|
| 60 | ptr=(char *)malloc(size + align_size + sizeof(int)); |
---|
| 61 | if(ptr==NULL) return(NULL); |
---|
| 62 | |
---|
| 63 | ptr2 = ptr + sizeof(int); |
---|
| 64 | aligned_ptr = ptr2 + (align_size - ((size_t)ptr2 & align_mask)); |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | ptr2 = aligned_ptr - sizeof(int); |
---|
| 68 | *((int *)ptr2)=(int)(aligned_ptr - ptr); |
---|
| 69 | |
---|
| 70 | return(aligned_ptr); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | void aligned_free(void *ptr) |
---|
| 74 | { |
---|
| 75 | int *ptr2=(int *)ptr - 1; |
---|
| 76 | ptr = (char*)ptr - *ptr2; |
---|
| 77 | free(ptr); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | /// File findind |
---|
| 82 | // From Nvidia toolkit |
---|
| 83 | |
---|
| 84 | using namespace std; |
---|
| 85 | |
---|
| 86 | string data_path::get_path(std::string filename) |
---|
| 87 | { |
---|
| 88 | FILE* fp; |
---|
| 89 | bool found = false; |
---|
| 90 | for(unsigned int i=0; i < path.size(); i++) |
---|
| 91 | { |
---|
| 92 | path_name = path[i] + "/" + filename; |
---|
| 93 | fp = ::fopen(path_name.c_str(), "r"); |
---|
| 94 | |
---|
| 95 | if(fp != 0) |
---|
| 96 | { |
---|
| 97 | fclose(fp); |
---|
| 98 | found = true; |
---|
| 99 | break; |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | if (found == false) |
---|
| 104 | { |
---|
| 105 | path_name = filename; |
---|
| 106 | fp = ::fopen(path_name.c_str(),"r"); |
---|
| 107 | if (fp != 0) |
---|
| 108 | { |
---|
| 109 | fclose(fp); |
---|
| 110 | found = true; |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | if (found == false) |
---|
| 115 | return ""; |
---|
| 116 | |
---|
| 117 | int loc = path_name.rfind('\\'); |
---|
| 118 | if (loc == -1) |
---|
| 119 | { |
---|
| 120 | loc = path_name.rfind('/'); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | if (loc != -1) |
---|
| 124 | file_path = path_name.substr(0, loc); |
---|
| 125 | else |
---|
| 126 | file_path = "."; |
---|
| 127 | return file_path; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | string data_path::get_file(std::string filename) |
---|
| 131 | { |
---|
| 132 | FILE* fp; |
---|
| 133 | |
---|
| 134 | for(unsigned int i=0; i < path.size(); i++) |
---|
| 135 | { |
---|
| 136 | path_name = path[i] + "/" + filename; |
---|
| 137 | fp = ::fopen(path_name.c_str(), "r"); |
---|
| 138 | |
---|
| 139 | if(fp != 0) |
---|
| 140 | { |
---|
| 141 | fclose(fp); |
---|
| 142 | return path_name; |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | path_name = filename; |
---|
| 147 | fp = ::fopen(path_name.c_str(),"r"); |
---|
| 148 | if (fp != 0) |
---|
| 149 | { |
---|
| 150 | fclose(fp); |
---|
| 151 | return path_name; |
---|
| 152 | } |
---|
| 153 | return ""; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | // data files, for read only |
---|
| 157 | FILE * data_path::fopen(std::string filename, const char * mode) |
---|
| 158 | { |
---|
| 159 | |
---|
| 160 | for(unsigned int i=0; i < path.size(); i++) |
---|
| 161 | { |
---|
| 162 | std::string s = path[i] + "/" + filename; |
---|
| 163 | FILE * fp = ::fopen(s.c_str(), mode); |
---|
| 164 | |
---|
| 165 | if(fp != 0) |
---|
| 166 | return fp; |
---|
| 167 | else if (!strcmp(path[i].c_str(),"")) |
---|
| 168 | { |
---|
| 169 | FILE* fp = ::fopen(filename.c_str(),mode); |
---|
| 170 | if (fp != 0) |
---|
| 171 | return fp; |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | // no luck... return null |
---|
| 175 | return 0; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | // fill the file stats structure |
---|
| 179 | // useful to get the file size and stuff |
---|
| 180 | int data_path::fstat(std::string filename, |
---|
| 181 | #ifdef WIN32 |
---|
| 182 | struct _stat |
---|
| 183 | #else |
---|
| 184 | struct stat |
---|
| 185 | #endif |
---|
| 186 | * stat) |
---|
| 187 | { |
---|
| 188 | for(unsigned int i=0; i < path.size(); i++) |
---|
| 189 | { |
---|
| 190 | std::string s = path[i] + "/" + filename; |
---|
| 191 | #ifdef WIN32 |
---|
| 192 | int fh = ::_open(s.c_str(), _O_RDONLY); |
---|
| 193 | #else |
---|
| 194 | int fh = ::open(s.c_str(), O_RDONLY); |
---|
| 195 | #endif |
---|
| 196 | if(fh >= 0) |
---|
| 197 | { |
---|
| 198 | #ifdef WIN32 |
---|
| 199 | int result = ::_fstat( fh, stat ); |
---|
| 200 | #else |
---|
| 201 | int result = ::fstat (fh,stat); |
---|
| 202 | #endif |
---|
| 203 | if( result != 0 ) |
---|
| 204 | { |
---|
| 205 | fprintf( stderr, "An fstat error occurred.\n" ); |
---|
| 206 | return 0; |
---|
| 207 | } |
---|
| 208 | #ifdef WIN32 |
---|
| 209 | ::_close( fh ); |
---|
| 210 | #else |
---|
| 211 | ::close (fh); |
---|
| 212 | #endif |
---|
| 213 | return 1; |
---|
| 214 | } |
---|
| 215 | } |
---|
| 216 | // no luck... |
---|
| 217 | return 0; |
---|
| 218 | } |
---|