Showing the changes to src/core/chuck_oo.cpp (1 of 26 files). Show all
Unified · Split
| @@ -56,12 +56,14 @@ const t_CKINT Chuck_IO::INT16 = 0x2; · 55 unmodified lines | ||
| 56 | 56 | const t_CKINT Chuck_IO::INT8 = 0x4; |
| 57 | 57 | const t_CKINT Chuck_IO::MODE_SYNC = 0; |
| 58 | 58 | const t_CKINT Chuck_IO::MODE_ASYNC = 1; |
| 59 | −//const t_CKINT Chuck_IO_File::FLAG_READ_WRITE = 0x8; | |
| 60 | −//const t_CKINT Chuck_IO_File::FLAG_READONLY = 0x10; | |
| 61 | −//const t_CKINT Chuck_IO_File::FLAG_WRITEONLY = 0x20; | |
| 62 | −//const t_CKINT Chuck_IO_File::FLAG_APPEND = 0x40; | |
| 63 | −//const t_CKINT Chuck_IO_File::TYPE_ASCII = 0x80; | |
| 64 | −//const t_CKINT Chuck_IO_File::TYPE_BINARY = 0x100; | |
| 59 | +#ifndef __DISABLE_FILEIO__ | |
| 60 | +const t_CKINT Chuck_IO_File::FLAG_READ_WRITE = 0x8; | |
| 61 | +const t_CKINT Chuck_IO_File::FLAG_READONLY = 0x10; | |
| 62 | +const t_CKINT Chuck_IO_File::FLAG_WRITEONLY = 0x20; | |
| 63 | +const t_CKINT Chuck_IO_File::FLAG_APPEND = 0x40; | |
| 64 | +const t_CKINT Chuck_IO_File::TYPE_ASCII = 0x80; | |
| 65 | +const t_CKINT Chuck_IO_File::TYPE_BINARY = 0x100; | |
| 66 | +#endif | |
| 65 | 67 | |
| 66 | 68 | |
| 67 | 69 | |
| @@ -1967,7 +1969,9 @@ t_CKUINT Chuck_Event::our_can_wait = 0; · 1899 unmodified lines | ||
| 1967 | 1969 | //----------------------------------------------------------------------------- |
| 1968 | 1970 | void Chuck_Event::signal() |
| 1969 | 1971 | { |
| 1970 | −// m_queue_lock.acquire(); | |
| 1972 | + #ifndef __DISABLE_THREADS__ | |
| 1973 | + m_queue_lock.acquire(); | |
| 1974 | + #endif | |
| 1971 | 1975 | if( !m_queue.empty() ) |
| 1972 | 1976 | { |
| 1973 | 1977 | // get the shred on top of the queue |
| @@ -1975,7 +1979,9 @@ void Chuck_Event::signal() · 1 unmodified line | ||
| 1975 | 1979 | // pop the top |
| 1976 | 1980 | m_queue.pop(); |
| 1977 | 1981 | // release it! |
| 1978 | −// m_queue_lock.release(); | |
| 1982 | + #ifndef __DISABLE_THREADS__ | |
| 1983 | + m_queue_lock.release(); | |
| 1984 | + #endif | |
| 1979 | 1985 | // REFACTOR-2017: BUG-FIX |
| 1980 | 1986 | // release the extra ref we added when we started waiting for this event |
| 1981 | 1987 | SAFE_RELEASE( shred->event ); |
| @@ -1991,7 +1997,9 @@ void Chuck_Event::signal() · 9 unmodified lines | ||
| 1991 | 1997 | } |
| 1992 | 1998 | else |
| 1993 | 1999 | { |
| 1994 | −// m_queue_lock.release(); | |
| 2000 | + #ifndef __DISABLE_THREADS__ | |
| 2001 | + m_queue_lock.release(); | |
| 2002 | + #endif | |
| 1995 | 2003 | } |
| 1996 | 2004 | } |
| 1997 | 2005 | |
| @@ -2008,7 +2016,9 @@ t_CKBOOL Chuck_Event::remove( Chuck_VM_Shred * shred ) · 10 unmodified lines | ||
| 2008 | 2016 | t_CKBOOL removed = FALSE; |
| 2009 | 2017 | |
| 2010 | 2018 | // lock |
| 2011 | −// m_queue_lock.acquire(); | |
| 2019 | + #ifndef __DISABLE_THREADS__ | |
| 2020 | + m_queue_lock.acquire(); | |
| 2021 | + #endif | |
| 2012 | 2022 | // while something in queue |
| 2013 | 2023 | while( !m_queue.empty() ) |
| 2014 | 2024 | { |
| @@ -2038,7 +2048,9 @@ t_CKBOOL Chuck_Event::remove( Chuck_VM_Shred * shred ) · 23 unmodified lines | ||
| 2038 | 2048 | // copy temp back to queue |
| 2039 | 2049 | m_queue = temp; |
| 2040 | 2050 | // release lock |
| 2041 | −// m_queue_lock.release(); | |
| 2051 | + #ifndef __DISABLE_THREADS__ | |
| 2052 | + m_queue_lock.release(); | |
| 2053 | + #endif | |
| 2042 | 2054 | |
| 2043 | 2055 | return removed; |
| 2044 | 2056 | } |
| @@ -2077,7 +2089,9 @@ t_CKBOOL Chuck_Event::remove_listen( void (* cb)(void) ) · 32 unmodified lines | ||
| 2077 | 2089 | t_CKBOOL removed = FALSE; |
| 2078 | 2090 | |
| 2079 | 2091 | // lock |
| 2080 | −// m_queue_lock.acquire(); | |
| 2092 | + #ifndef __DISABLE_THREADS__ | |
| 2093 | + m_queue_lock.acquire(); | |
| 2094 | + #endif | |
| 2081 | 2095 | // while something in queue |
| 2082 | 2096 | while( !m_global_queue.empty() ) |
| 2083 | 2097 | { |
| @@ -2100,7 +2114,9 @@ t_CKBOOL Chuck_Event::remove_listen( void (* cb)(void) ) · 16 unmodified lines | ||
| 2100 | 2114 | // copy temp back to queue |
| 2101 | 2115 | m_global_queue = temp; |
| 2102 | 2116 | // release lock |
| 2103 | −// m_queue_lock.release(); | |
| 2117 | + #ifndef __DISABLE_THREADS__ | |
| 2118 | + m_queue_lock.release(); | |
| 2119 | + #endif | |
| 2104 | 2120 | |
| 2105 | 2121 | return removed; |
| 2106 | 2122 | } |
| @@ -2114,7 +2130,9 @@ t_CKBOOL Chuck_Event::remove_listen( void (* cb)(void) ) · 7 unmodified lines | ||
| 2114 | 2130 | //----------------------------------------------------------------------------- |
| 2115 | 2131 | void Chuck_Event::signal_global() |
| 2116 | 2132 | { |
| 2117 | −// m_queue_lock.acquire(); | |
| 2133 | + #ifndef __DISABLE_THREADS__ | |
| 2134 | + m_queue_lock.acquire(); | |
| 2135 | + #endif | |
| 2118 | 2136 | |
| 2119 | 2137 | if( !m_global_queue.empty() ) |
| 2120 | 2138 | { |
| @@ -2134,7 +2152,9 @@ void Chuck_Event::signal_global() · 13 unmodified lines | ||
| 2134 | 2152 | } |
| 2135 | 2153 | } |
| 2136 | 2154 | |
| 2137 | −// m_queue_lock.release(); | |
| 2155 | + #ifndef __DISABLE_THREADS__ | |
| 2156 | + m_queue_lock.release(); | |
| 2157 | + #endif | |
| 2138 | 2158 | } |
| 2139 | 2159 | |
| 2140 | 2160 | |
| @@ -2146,7 +2166,9 @@ void Chuck_Event::signal_global() · 5 unmodified lines | ||
| 2146 | 2166 | //----------------------------------------------------------------------------- |
| 2147 | 2167 | void Chuck_Event::broadcast_global() |
| 2148 | 2168 | { |
| 2149 | −// m_queue_lock.acquire(); | |
| 2169 | + #ifndef __DISABLE_THREADS__ | |
| 2170 | + m_queue_lock.acquire(); | |
| 2171 | + #endif | |
| 2150 | 2172 | std::queue< Chuck_Global_Event_Listener > call_again; |
| 2151 | 2173 | |
| 2152 | 2174 | while( !m_global_queue.empty() ) |
| @@ -2169,8 +2191,10 @@ void Chuck_Event::broadcast_global() · 16 unmodified lines | ||
| 2169 | 2191 | |
| 2170 | 2192 | // for those that should be called again, store them again |
| 2171 | 2193 | m_global_queue = call_again; |
| 2172 | − | |
| 2173 | −// m_queue_lock.release(); | |
| 2194 | + | |
| 2195 | + #ifndef __DISABLE_THREADS__ | |
| 2196 | + m_queue_lock.release(); | |
| 2197 | + #endif | |
| 2174 | 2198 | } |
| 2175 | 2199 | |
| 2176 | 2200 | |
| @@ -2185,19 +2209,25 @@ void Chuck_Event::broadcast_global() · 8 unmodified lines | ||
| 2185 | 2209 | void Chuck_Event::queue_broadcast( CBufferSimple * event_buffer ) |
| 2186 | 2210 | { |
| 2187 | 2211 | // TODO: handle multiple VM |
| 2188 | −// m_queue_lock.acquire(); | |
| 2212 | + #ifndef __DISABLE_THREADS__ | |
| 2213 | + m_queue_lock.acquire(); | |
| 2214 | + #endif | |
| 2189 | 2215 | if( !m_queue.empty() ) |
| 2190 | 2216 | { |
| 2191 | 2217 | // get shred (only to get the VM ref) |
| 2192 | 2218 | Chuck_VM_Shred * shred = m_queue.front(); |
| 2193 | 2219 | // release lock |
| 2194 | −// m_queue_lock.release(); | |
| 2220 | + #ifndef __DISABLE_THREADS__ | |
| 2221 | + m_queue_lock.release(); | |
| 2222 | + #endif | |
| 2195 | 2223 | // queue the event on the vm (added 1.3.0.0: event_buffer) |
| 2196 | 2224 | shred->vm_ref->queue_event( this, 1, event_buffer ); |
| 2197 | 2225 | } |
| 2198 | 2226 | else |
| 2199 | 2227 | { |
| 2200 | −// m_queue_lock.release(); | |
| 2228 | + #ifndef __DISABLE_THREADS__ | |
| 2229 | + m_queue_lock.release(); | |
| 2230 | + #endif | |
| 2201 | 2231 | } |
| 2202 | 2232 | } |
| 2203 | 2233 | |
| @@ -2211,19 +2241,27 @@ void Chuck_Event::queue_broadcast( CBufferSimple * event_buffer ) · 7 unmodified lines | ||
| 2211 | 2241 | void Chuck_Event::broadcast() |
| 2212 | 2242 | { |
| 2213 | 2243 | // lock queue |
| 2214 | −// m_queue_lock.acquire(); | |
| 2244 | + #ifndef __DISABLE_THREADS__ | |
| 2245 | + m_queue_lock.acquire(); | |
| 2246 | + #endif | |
| 2215 | 2247 | // while not empty |
| 2216 | 2248 | while( !m_queue.empty() ) |
| 2217 | 2249 | { |
| 2218 | 2250 | // release first |
| 2219 | −// m_queue_lock.release(); | |
| 2251 | + #ifndef __DISABLE_THREADS__ | |
| 2252 | + m_queue_lock.release(); | |
| 2253 | + #endif | |
| 2220 | 2254 | // signal the next shred |
| 2221 | 2255 | this->signal(); |
| 2222 | 2256 | // lock again |
| 2223 | −// m_queue_lock.acquire(); | |
| 2257 | + #ifndef __DISABLE_THREADS__ | |
| 2258 | + m_queue_lock.acquire(); | |
| 2259 | + #endif | |
| 2224 | 2260 | } |
| 2225 | 2261 | // release |
| 2226 | −// m_queue_lock.release(); | |
| 2262 | + #ifndef __DISABLE_THREADS__ | |
| 2263 | + m_queue_lock.release(); | |
| 2264 | + #endif | |
| 2227 | 2265 | } |
| 2228 | 2266 | |
| 2229 | 2267 | |
| @@ -2255,9 +2293,13 @@ void Chuck_Event::wait( Chuck_VM_Shred * shred, Chuck_VM * vm ) · 25 unmodified lines | ||
| 2255 | 2293 | shred->is_running = FALSE; |
| 2256 | 2294 | |
| 2257 | 2295 | // add to waiting list |
| 2258 | −// m_queue_lock.acquire(); | |
| 2296 | + #ifndef __DISABLE_THREADS__ | |
| 2297 | + m_queue_lock.acquire(); | |
| 2298 | + #endif | |
| 2259 | 2299 | m_queue.push( shred ); |
| 2260 | −// m_queue_lock.release(); | |
| 2300 | + #ifndef __DISABLE_THREADS__ | |
| 2301 | + m_queue_lock.release(); | |
| 2302 | + #endif | |
| 2261 | 2303 | |
| 2262 | 2304 | // add event to shred |
| 2263 | 2305 | assert( shred->event == NULL ); |
| @@ -2302,677 +2344,678 @@ Chuck_IO::~Chuck_IO() · 38 unmodified lines | ||
| 2302 | 2344 | |
| 2303 | 2345 | |
| 2304 | 2346 | |
| 2305 | −////----------------------------------------------------------------------------- | |
| 2306 | −//// name: Chuck_IO_File() | |
| 2307 | −//// desc: constructor | |
| 2308 | −////----------------------------------------------------------------------------- | |
| 2309 | −//Chuck_IO_File::Chuck_IO_File( Chuck_VM * vm ) | |
| 2310 | −//{ | |
| 2311 | −// m_vmRef = vm; | |
| 2312 | −// // zero things out | |
| 2313 | −// m_flags = 0; | |
| 2314 | −// m_iomode = MODE_SYNC; | |
| 2315 | −// m_path = ""; | |
| 2316 | −// m_dir = NULL; | |
| 2317 | −// m_dir_start = 0; | |
| 2318 | −// m_asyncEvent = new Chuck_Event; | |
| 2319 | −// initialize_object( m_asyncEvent, vm->env()->t_event ); | |
| 2320 | −// m_thread = new XThread; | |
| 2321 | −//} | |
| 2322 | −// | |
| 2323 | −// | |
| 2324 | −// | |
| 2325 | −// | |
| 2326 | −////----------------------------------------------------------------------------- | |
| 2327 | −//// name: ~Chuck_IO_File() | |
| 2328 | −//// desc: destructor | |
| 2329 | −////----------------------------------------------------------------------------- | |
| 2330 | −//Chuck_IO_File::~Chuck_IO_File() | |
| 2331 | −//{ | |
| 2332 | −// // clean up | |
| 2333 | −// this->close(); | |
| 2334 | −// delete m_asyncEvent; | |
| 2335 | −// delete m_thread; | |
| 2336 | −//} | |
| 2337 | −// | |
| 2338 | −// | |
| 2339 | −// | |
| 2340 | −// | |
| 2341 | −////----------------------------------------------------------------------------- | |
| 2342 | −//// name: open | |
| 2343 | −//// desc: open file from disk | |
| 2344 | −////----------------------------------------------------------------------------- | |
| 2345 | −//t_CKBOOL Chuck_IO_File::open( const string & path, t_CKINT flags ) | |
| 2346 | −//{ | |
| 2347 | −// // log | |
| 2348 | −// EM_log( CK_LOG_INFO, "FileIO: opening file from disk..." ); | |
| 2349 | −// EM_log( CK_LOG_INFO, "FileIO: path: %s", path.c_str() ); | |
| 2350 | −// EM_pushlog(); | |
| 2351 | −// | |
| 2352 | −// // if no flag specified, make it READ by default | |
| 2353 | −// if( !(flags & FLAG_READ_WRITE) && | |
| 2354 | −// !(flags & FLAG_READONLY) && | |
| 2355 | −// !(flags & FLAG_WRITEONLY) && | |
| 2356 | −// !(flags & FLAG_APPEND) ) | |
| 2357 | −// { | |
| 2358 | −// flags |= FLAG_READONLY; | |
| 2359 | −// } | |
| 2360 | −// | |
| 2361 | −// // if both read and write, enable read and write | |
| 2362 | −// if( flags & FLAG_READONLY && flags & FLAG_WRITEONLY ) | |
| 2363 | −// { | |
| 2364 | −// flags ^= FLAG_READONLY; | |
| 2365 | −// flags ^= FLAG_WRITEONLY; | |
| 2366 | −// flags |= FLAG_READ_WRITE; | |
| 2367 | −// } | |
| 2368 | −// | |
| 2369 | −// // check flags for errors | |
| 2370 | −// if ((flags & TYPE_ASCII) && | |
| 2371 | −// (flags & TYPE_BINARY)) | |
| 2372 | −// { | |
| 2373 | −// EM_error3( "[chuck](via FileIO): cannot open file in both ASCII and binary mode" ); | |
| 2374 | −// goto error; | |
| 2375 | −// } | |
| 2376 | −// | |
| 2377 | −// if ((flags & FLAG_READ_WRITE) && | |
| 2378 | −// (flags & FLAG_READONLY)) | |
| 2379 | −// { | |
| 2380 | −// EM_error3( "[chuck](via FileIO): conflicting flags: READ_WRITE and READ" ); | |
| 2381 | −// goto error; | |
| 2382 | −// } | |
| 2383 | −// | |
| 2384 | −// if ((flags & FLAG_READ_WRITE) && | |
| 2385 | −// (flags & FLAG_WRITEONLY)) | |
| 2386 | −// { | |
| 2387 | −// EM_error3( "[chuck](via FileIO): conflicting flags: READ_WRITE and WRITE" ); | |
| 2388 | −// goto error; | |
| 2389 | −// } | |
| 2390 | −// | |
| 2391 | −// if ((flags & FLAG_READ_WRITE) && | |
| 2392 | −// (flags & FLAG_APPEND)) | |
| 2393 | −// { | |
| 2394 | −// EM_error3( "[chuck](via FileIO): conflicting flags: READ_WRITE and APPEND" ); | |
| 2395 | −// goto error; | |
| 2396 | −// } | |
| 2397 | −// | |
| 2398 | −// if ((flags & FLAG_WRITEONLY) && | |
| 2399 | −// (flags & FLAG_READONLY)) | |
| 2400 | −// { | |
| 2401 | −// EM_error3( "[chuck](via FileIO): conflicting flags: WRITE and READ" ); | |
| 2402 | −// goto error; | |
| 2403 | −// } | |
| 2404 | −// | |
| 2405 | −// if ((flags & FLAG_APPEND) && | |
| 2406 | −// (flags & FLAG_READONLY)) | |
| 2407 | −// { | |
| 2408 | −// EM_error3( "[chuck](via FileIO): conflicting flags: APPEND and FLAG_READ" ); | |
| 2409 | −// goto error; | |
| 2410 | −// } | |
| 2411 | −// | |
| 2412 | −// // set open flags | |
| 2413 | −// ios_base::openmode mode; | |
| 2414 | −// | |
| 2415 | −// if (flags & FLAG_READ_WRITE) | |
| 2416 | −// mode = ios_base::in | ios_base::out; | |
| 2417 | −// else if (flags & FLAG_READONLY) | |
| 2418 | −// mode = ios_base::in; | |
| 2419 | −// else if (flags & FLAG_APPEND) | |
| 2420 | −// mode = ios_base::out | ios_base::app; | |
| 2421 | −// else if (flags & FLAG_WRITEONLY) | |
| 2422 | −// mode = ios_base::out | ios_base::trunc; | |
| 2423 | −// | |
| 2424 | −// if (flags & TYPE_BINARY) | |
| 2425 | −// mode |= ios_base::binary; | |
| 2426 | −// | |
| 2427 | −// // close first | |
| 2428 | −// if (m_io.is_open()) | |
| 2429 | −// this->close(); | |
| 2430 | −// | |
| 2431 | −// // try to open as a dir first (fixed 1.3.0.0 removed warning) | |
| 2432 | −// if( (m_dir = opendir( path.c_str() )) ) | |
| 2433 | −// { | |
| 2434 | −// EM_poplog(); | |
| 2435 | −// return TRUE; | |
| 2436 | −// } | |
| 2437 | −// | |
| 2438 | −// // not a dir, create file if it does not exist unless flag is | |
| 2439 | −// // readonly | |
| 2440 | −// if ( !(flags & FLAG_READONLY) ) | |
| 2441 | −// { | |
| 2442 | −// m_io.open( path.c_str(), ios_base::in ); | |
| 2443 | −// if ( m_io.fail() ) | |
| 2444 | −// { | |
| 2445 | −// m_io.clear(); | |
| 2446 | −// m_io.open( path.c_str(), ios_base::out | ios_base::trunc ); | |
| 2447 | −// m_io.close(); | |
| 2448 | −// } | |
| 2449 | −// else | |
| 2450 | −// m_io.close(); | |
| 2451 | −// } | |
| 2452 | −// | |
| 2453 | −// //open file | |
| 2454 | −// m_io.open( path.c_str(), mode ); | |
| 2455 | −// | |
| 2456 | −// // seek to beginning if necessary | |
| 2457 | −// if (flags & FLAG_READ_WRITE) | |
| 2458 | −// { | |
| 2459 | −// m_io.seekp(0); | |
| 2460 | −// m_io.seekg(0); | |
| 2461 | −// } | |
| 2462 | −// | |
| 2463 | −// /* ATODO: Ge's code | |
| 2464 | −// // windows sucks for being creative in the wrong places | |
| 2465 | −// #ifdef __PLATFORM_WIN32__ | |
| 2466 | −// // if( flags ^ Chuck_IO::TRUNCATE && flags | Chuck_IO::READ ) nMode |= ios::nocreate; | |
| 2467 | −// m_io.open( path.c_str(), nMode ); | |
| 2468 | −// #else | |
| 2469 | −// m_io.open( path.c_str(), (_Ios_Openmode)nMode ); | |
| 2470 | −// #endif | |
| 2471 | −// */ | |
| 2472 | −// | |
| 2473 | −// // check for error | |
| 2474 | −// if( !(m_io.is_open()) ) | |
| 2475 | −// { | |
| 2476 | −// // EM_error3( "[chuck](via FileIO): cannot open file: '%s'", path.c_str() ); | |
| 2477 | −// goto error; | |
| 2478 | −// } | |
| 2479 | −// | |
| 2480 | −// // set path | |
| 2481 | −// m_path = path; | |
| 2482 | −// // set flags | |
| 2483 | −// m_flags = flags; | |
| 2484 | −// if (!(flags & TYPE_BINARY)) | |
| 2485 | −// m_flags |= Chuck_IO_File::TYPE_ASCII; // ASCII is default | |
| 2486 | −// // set mode | |
| 2487 | −// m_iomode = MODE_SYNC; | |
| 2488 | −// | |
| 2489 | −// // pop | |
| 2490 | −// EM_poplog(); | |
| 2491 | −// | |
| 2492 | −// return TRUE; | |
| 2493 | −// | |
| 2494 | −//error: | |
| 2495 | −// | |
| 2496 | −// // pop | |
| 2497 | −// EM_poplog(); | |
| 2498 | −// | |
| 2499 | −// // reset | |
| 2500 | −// m_path = ""; | |
| 2501 | −// m_flags = 0; | |
| 2502 | −// m_iomode = MODE_SYNC; | |
| 2503 | −// m_io.clear(); | |
| 2504 | −// m_io.close(); | |
| 2505 | −// | |
| 2506 | −// return FALSE; | |
| 2507 | −//} | |
| 2508 | −// | |
| 2509 | −// | |
| 2510 | −// | |
| 2511 | −// | |
| 2512 | −////----------------------------------------------------------------------------- | |
| 2513 | −//// name: close | |
| 2514 | −//// desc: close file | |
| 2515 | −////----------------------------------------------------------------------------- | |
| 2516 | −//void Chuck_IO_File::close() | |
| 2517 | −//{ | |
| 2518 | −// // log | |
| 2519 | −// EM_log( CK_LOG_INFO, "FileIO: closing file '%s'...", m_path.c_str() ); | |
| 2520 | −// // close it | |
| 2521 | −// m_io.close(); | |
| 2522 | −// m_flags = 0; | |
| 2523 | −// m_path = ""; | |
| 2524 | −// m_iomode = Chuck_IO::MODE_SYNC; | |
| 2525 | −// if ( m_dir ) { | |
| 2526 | −// closedir( m_dir ); | |
| 2527 | −// m_dir = NULL; | |
| 2528 | −// m_dir_start = 0; | |
| 2529 | −// } | |
| 2530 | −//} | |
| 2531 | −// | |
| 2532 | −// | |
| 2533 | −// | |
| 2534 | −// | |
| 2535 | −////----------------------------------------------------------------------------- | |
| 2536 | −//// name: good() | |
| 2537 | −//// desc: ... | |
| 2538 | −////----------------------------------------------------------------------------- | |
| 2539 | −//t_CKBOOL Chuck_IO_File::good() | |
| 2540 | −//{ | |
| 2541 | −// return m_dir || m_io.is_open(); | |
| 2542 | −// // return (!m_dir) && (!m_io.is_open()); | |
| 2543 | −//} | |
| 2544 | −// | |
| 2545 | −// | |
| 2546 | −// | |
| 2547 | −// | |
| 2548 | −////----------------------------------------------------------------------------- | |
| 2549 | −//// name: flush() | |
| 2550 | −//// desc: ... | |
| 2551 | −////----------------------------------------------------------------------------- | |
| 2552 | −//void Chuck_IO_File::flush() | |
| 2553 | −//{ | |
| 2554 | −// // sanity | |
| 2555 | −// if ( m_dir ) | |
| 2556 | −// { | |
| 2557 | −// EM_error3( "[chuck](via FileIO): cannot flush on directory" ); | |
| 2558 | −// return; | |
| 2559 | −// } | |
| 2560 | −// m_io.flush(); | |
| 2561 | −//} | |
| 2562 | −// | |
| 2563 | −// | |
| 2564 | −// | |
| 2565 | −// | |
| 2566 | −////----------------------------------------------------------------------------- | |
| 2567 | −//// name: mode() | |
| 2568 | −//// desc: ... | |
| 2569 | −////----------------------------------------------------------------------------- | |
| 2570 | −//t_CKINT Chuck_IO_File::mode() | |
| 2571 | −//{ | |
| 2572 | −// // sanity | |
| 2573 | −// if ( m_dir ) | |
| 2574 | −// { | |
| 2575 | −// EM_error3( "[chuck](via FileIO): cannot get mode on directory" ); | |
| 2576 | −// return -1; | |
| 2577 | −// } | |
| 2578 | −// return m_iomode; | |
| 2579 | −//} | |
| 2580 | −// | |
| 2581 | −// | |
| 2582 | −// | |
| 2583 | −// | |
| 2584 | −////----------------------------------------------------------------------------- | |
| 2585 | −//// name: mode( t_CKINT flag ) | |
| 2586 | −//// desc: ... | |
| 2587 | −////----------------------------------------------------------------------------- | |
| 2588 | −//void Chuck_IO_File::mode( t_CKINT flag ) | |
| 2589 | −//{ | |
| 2590 | −// // sanity | |
| 2591 | −// if ( m_dir ) | |
| 2592 | −// { | |
| 2593 | −// EM_error3( "[chuck](via FileIO): cannot set mode on directory" ); | |
| 2594 | −// return; | |
| 2595 | −// } | |
| 2596 | −// if ( (flag != Chuck_IO::MODE_ASYNC) && (flag != Chuck_IO::MODE_SYNC) ) | |
| 2597 | −// { | |
| 2598 | −// EM_error3( "[chuck](via FileIO): invalid mode flag" ); | |
| 2599 | −// return; | |
| 2600 | −// } | |
| 2601 | −// | |
| 2602 | −// m_iomode = flag; | |
| 2603 | −//} | |
| 2604 | −// | |
| 2605 | −// | |
| 2606 | −// | |
| 2607 | −// | |
| 2608 | −////----------------------------------------------------------------------------- | |
| 2609 | −//// name: size() | |
| 2610 | −//// desc: Returns the size of the file in bytes, or -1 if no file is opened or | |
| 2611 | −//// if a directory is opened. | |
| 2612 | −////----------------------------------------------------------------------------- | |
| 2613 | −//t_CKINT Chuck_IO_File::size() | |
| 2614 | −//{ | |
| 2615 | −// if (!(m_io.is_open())) return -1; | |
| 2616 | −// if ( m_dir ) | |
| 2617 | −// { | |
| 2618 | −// EM_error3( "[chuck](via FileIO): cannot get size on a directory" ); | |
| 2619 | −// return -1; | |
| 2620 | −// } | |
| 2621 | −// | |
| 2622 | −// // no easy way to find file size in C++ | |
| 2623 | −// // have to seek to end, report position | |
| 2624 | −// FILE * stream = fopen( m_path.c_str(), "r" ); | |
| 2625 | −// fseek( stream, 0L, SEEK_END ); | |
| 2626 | −// int endPos = ftell( stream ); | |
| 2627 | −// fclose( stream ); | |
| 2628 | −// return endPos; | |
| 2629 | −//} | |
| 2630 | −// | |
| 2631 | −// | |
| 2632 | −// | |
| 2633 | −// | |
| 2634 | −////----------------------------------------------------------------------------- | |
| 2635 | −//// name: seek( t_CKINT pos ) | |
| 2636 | −//// desc: Seeks to the specified byte offset in the file. | |
| 2637 | −////----------------------------------------------------------------------------- | |
| 2638 | −//void Chuck_IO_File::seek( t_CKINT pos ) | |
| 2639 | −//{ | |
| 2640 | −// if ( !(m_io.is_open()) ) | |
| 2641 | −// { | |
| 2642 | −// EM_error3( "[chuck](via FileIO): cannot seek: no file is open" ); | |
| 2643 | −// return; | |
| 2644 | −// } | |
| 2645 | −// if ( m_dir ) | |
| 2646 | −// { | |
| 2647 | −// EM_error3( "[chuck](via FileIO): cannot seek on a directory" ); | |
| 2648 | −// return; | |
| 2649 | −// } | |
| 2650 | −// m_io.seekg( pos ); | |
| 2651 | −// m_io.seekp( pos ); | |
| 2652 | −//} | |
| 2653 | −// | |
| 2654 | −// | |
| 2655 | −// | |
| 2656 | −// | |
| 2657 | −////----------------------------------------------------------------------------- | |
| 2658 | −//// name: tell() | |
| 2659 | −//// desc: Returns the byte offset into the file, or -1 if no file is opened. | |
| 2660 | −////----------------------------------------------------------------------------- | |
| 2661 | −//t_CKINT Chuck_IO_File::tell() | |
| 2662 | −//{ | |
| 2663 | −// if (!(m_io.is_open())) | |
| 2664 | −// return -1; | |
| 2665 | −// if ( m_dir ) | |
| 2666 | −// { | |
| 2667 | −// EM_error3( "[chuck](via FileIO): cannot tell on directory" ); | |
| 2668 | −// return -1; | |
| 2669 | −// } | |
| 2670 | −// | |
| 2671 | −// return m_io.tellg(); | |
| 2672 | −//} | |
| 2673 | −// | |
| 2674 | −// | |
| 2675 | −// | |
| 2676 | −// | |
| 2677 | −////----------------------------------------------------------------------------- | |
| 2678 | −//// name: isDir() | |
| 2679 | −//// desc: ... | |
| 2680 | −////----------------------------------------------------------------------------- | |
| 2681 | −//t_CKINT Chuck_IO_File::isDir() | |
| 2682 | −//{ | |
| 2683 | −// return m_dir != NULL; | |
| 2684 | −//} | |
| 2685 | −// | |
| 2686 | −// | |
| 2687 | −// | |
| 2688 | −// | |
| 2689 | −////----------------------------------------------------------------------------- | |
| 2690 | −//// name: dirList() | |
| 2691 | −//// desc: ... | |
| 2692 | −////----------------------------------------------------------------------------- | |
| 2693 | −//Chuck_Array4 * Chuck_IO_File::dirList() | |
| 2694 | −//{ | |
| 2695 | −// // sanity | |
| 2696 | −// if ( !m_dir ) | |
| 2697 | −// { | |
| 2698 | −// EM_error3( "[chuck](via FileIO): cannot get list: no directory open" ); | |
| 2699 | −// Chuck_Array4 *ret = new Chuck_Array4( TRUE, 0 ); | |
| 2700 | −// initialize_object( ret, m_vmRef->env()->t_array ); | |
| 2701 | −// return ret; | |
| 2702 | −// } | |
| 2703 | −// | |
| 2704 | −// // fill vector with entry names | |
| 2705 | −// rewinddir( m_dir ); | |
| 2706 | −// std::vector<Chuck_String *> entrylist; | |
| 2707 | −// struct dirent *ent; | |
| 2708 | −// while( (ent = readdir( m_dir )) ) // fixed 1.3.0.0: removed warning | |
| 2709 | −// { | |
| 2710 | −// // pass NULL as shred ref | |
| 2711 | −// Chuck_String *s = (Chuck_String *)instantiate_and_initialize_object( m_vmRef->env()->t_string, NULL, m_vmRef ); | |
| 2712 | −// s->set( std::string( ent->d_name ) ); | |
| 2713 | −// if ( s->str() != ".." && s->str() != "." ) | |
| 2714 | −// { | |
| 2715 | −// // don't include .. and . in the list | |
| 2716 | −// entrylist.push_back( s ); | |
| 2717 | −// } | |
| 2718 | −// } | |
| 2719 | −// | |
| 2720 | −// // make array | |
| 2721 | −// Chuck_Array4 *array = new Chuck_Array4( true, entrylist.size() ); | |
| 2722 | −// initialize_object( array, m_vmRef->env()->t_array ); | |
| 2723 | −// for ( int i = 0; i < entrylist.size(); i++ ) | |
| 2724 | −// array->set( i, (t_CKUINT) entrylist[i] ); | |
| 2725 | −// return array; | |
| 2726 | −//} | |
| 2347 | +#ifndef __DISABLE_FILEIO__ | |
| 2348 | +//----------------------------------------------------------------------------- | |
| 2349 | +// name: Chuck_IO_File() | |
| 2350 | +// desc: constructor | |
| 2351 | +//----------------------------------------------------------------------------- | |
| 2352 | +Chuck_IO_File::Chuck_IO_File( Chuck_VM * vm ) | |
| 2353 | +{ | |
| 2354 | + m_vmRef = vm; | |
| 2355 | + // zero things out | |
| 2356 | + m_flags = 0; | |
| 2357 | + m_iomode = MODE_SYNC; | |
| 2358 | + m_path = ""; | |
| 2359 | + m_dir = NULL; | |
| 2360 | + m_dir_start = 0; | |
| 2361 | + m_asyncEvent = new Chuck_Event; | |
| 2362 | + initialize_object( m_asyncEvent, vm->env()->t_event ); | |
| 2363 | + m_thread = new XThread; | |
| 2364 | +} | |
| 2727 | 2365 | |
| 2728 | 2366 | |
| 2729 | 2367 | |
| 2730 | 2368 | |
| 2731 | 2369 | //----------------------------------------------------------------------------- |
| 2732 | −// name: read( t_CKINT length ) | |
| 2733 | −// desc: ... | |
| 2370 | +// name: ~Chuck_IO_File() | |
| 2371 | +// desc: destructor | |
| 2734 | 2372 | //----------------------------------------------------------------------------- |
| 2735 | −/*Chuck_String * Chuck_IO_File::read( t_CKINT length ) | |
| 2373 | +Chuck_IO_File::~Chuck_IO_File() | |
| 2736 | 2374 | { |
| 2737 | − // sanity | |
| 2738 | − if (!(m_io.is_open())) { | |
| 2739 | − EM_error3( "[chuck](via FileIO): cannot read: no file open" ); | |
| 2740 | − return new Chuck_String( "" ); | |
| 2375 | + // clean up | |
| 2376 | + this->close(); | |
| 2377 | + delete m_asyncEvent; | |
| 2378 | + delete m_thread; | |
| 2379 | +} | |
| 2380 | + | |
| 2381 | + | |
| 2382 | + | |
| 2383 | + | |
| 2384 | +//----------------------------------------------------------------------------- | |
| 2385 | +// name: open | |
| 2386 | +// desc: open file from disk | |
| 2387 | +//----------------------------------------------------------------------------- | |
| 2388 | +t_CKBOOL Chuck_IO_File::open( const string & path, t_CKINT flags ) | |
| 2389 | +{ | |
| 2390 | + // log | |
| 2391 | + EM_log( CK_LOG_INFO, "FileIO: opening file from disk..." ); | |
| 2392 | + EM_log( CK_LOG_INFO, "FileIO: path: %s", path.c_str() ); | |
| 2393 | + EM_pushlog(); | |
| 2394 | + | |
| 2395 | + // if no flag specified, make it READ by default | |
| 2396 | + if( !(flags & FLAG_READ_WRITE) && | |
| 2397 | + !(flags & FLAG_READONLY) && | |
| 2398 | + !(flags & FLAG_WRITEONLY) && | |
| 2399 | + !(flags & FLAG_APPEND) ) | |
| 2400 | + { | |
| 2401 | + flags |= FLAG_READONLY; | |
| 2741 | 2402 | } |
| 2742 | − | |
| 2743 | − if (m_io.fail()) { | |
| 2744 | − EM_error3( "[chuck](via FileIO): cannot read: I/O stream failed" ); | |
| 2745 | − return new Chuck_String( "" ); | |
| 2403 | + | |
| 2404 | + // if both read and write, enable read and write | |
| 2405 | + if( flags & FLAG_READONLY && flags & FLAG_WRITEONLY ) | |
| 2406 | + { | |
| 2407 | + flags ^= FLAG_READONLY; | |
| 2408 | + flags ^= FLAG_WRITEONLY; | |
| 2409 | + flags |= FLAG_READ_WRITE; | |
| 2746 | 2410 | } |
| 2747 | − | |
| 2748 | − if ( m_dir ) | |
| 2411 | + | |
| 2412 | + // check flags for errors | |
| 2413 | + if ((flags & TYPE_ASCII) && | |
| 2414 | + (flags & TYPE_BINARY)) | |
| 2749 | 2415 | { |
| 2750 | − EM_error3( "[chuck](via FileIO): cannot read on a directory" ); | |
| 2751 | − return new Chuck_String( "" ); | |
| 2416 | + EM_error3( "[chuck](via FileIO): cannot open file in both ASCII and binary mode" ); | |
| 2417 | + goto error; | |
| 2752 | 2418 | } |
| 2753 | − | |
| 2754 | − char buf[length+1]; | |
| 2755 | − m_io.read( buf, length ); | |
| 2756 | − buf[m_io.gcount()] = '\0'; | |
| 2757 | − string s( buf ); | |
| 2758 | − return new Chuck_String( s ); | |
| 2759 | −}*/ | |
| 2760 | 2419 | |
| 2420 | + if ((flags & FLAG_READ_WRITE) && | |
| 2421 | + (flags & FLAG_READONLY)) | |
| 2422 | + { | |
| 2423 | + EM_error3( "[chuck](via FileIO): conflicting flags: READ_WRITE and READ" ); | |
| 2424 | + goto error; | |
| 2425 | + } | |
| 2761 | 2426 | |
| 2427 | + if ((flags & FLAG_READ_WRITE) && | |
| 2428 | + (flags & FLAG_WRITEONLY)) | |
| 2429 | + { | |
| 2430 | + EM_error3( "[chuck](via FileIO): conflicting flags: READ_WRITE and WRITE" ); | |
| 2431 | + goto error; | |
| 2432 | + } | |
| 2762 | 2433 | |
| 2434 | + if ((flags & FLAG_READ_WRITE) && | |
| 2435 | + (flags & FLAG_APPEND)) | |
| 2436 | + { | |
| 2437 | + EM_error3( "[chuck](via FileIO): conflicting flags: READ_WRITE and APPEND" ); | |
| 2438 | + goto error; | |
| 2439 | + } | |
| 2763 | 2440 | |
| 2764 | −//----------------------------------------------------------------------------- | |
| 2765 | −// name: readLine() | |
| 2766 | −// desc: read line | |
| 2767 | −//----------------------------------------------------------------------------- | |
| 2768 | −//Chuck_String * Chuck_IO_File::readLine() | |
| 2769 | −//{ | |
| 2770 | −// // sanity | |
| 2771 | −// if (!(m_io.is_open())) { | |
| 2772 | −// EM_error3( "[chuck](via FileIO): cannot readLine: no file open" ); | |
| 2773 | −// return new Chuck_String( "" ); | |
| 2774 | −// } | |
| 2775 | −// | |
| 2776 | −// if (m_io.fail()) { | |
| 2777 | −// EM_error3( "[chuck](via FileIO): cannot readLine: I/O stream failed" ); | |
| 2778 | −// return new Chuck_String( "" ); | |
| 2779 | −// } | |
| 2780 | −// | |
| 2781 | −// if ( m_dir ) | |
| 2782 | −// { | |
| 2783 | −// EM_error3( "[chuck](via FileIO): cannot readLine on directory" ); | |
| 2784 | −// return new Chuck_String( "" ); | |
| 2785 | −// } | |
| 2786 | −// | |
| 2787 | −// string s; | |
| 2788 | −// getline( m_io, s ); | |
| 2789 | −// return new Chuck_String( s ); | |
| 2790 | −//} | |
| 2791 | −// | |
| 2792 | −// | |
| 2793 | −// | |
| 2794 | −// | |
| 2795 | −////----------------------------------------------------------------------------- | |
| 2796 | −//// name: readInt( t_CKINT flags ) | |
| 2797 | −//// desc: ... | |
| 2798 | −////----------------------------------------------------------------------------- | |
| 2799 | −//t_CKINT Chuck_IO_File::readInt( t_CKINT flags ) | |
| 2800 | −//{ | |
| 2801 | −// // sanity | |
| 2802 | −// if (!(m_io.is_open())) { | |
| 2803 | −// EM_error3( "[chuck](via FileIO): cannot readInt: no file open" ); | |
| 2804 | −// return 0; | |
| 2805 | −// } | |
| 2806 | −// | |
| 2807 | −// if (m_io.eof()) { | |
| 2808 | −// EM_error3( "[chuck](via FileIO): cannot readInt: EOF reached" ); | |
| 2809 | −// return 0; | |
| 2810 | −// } | |
| 2811 | −// | |
| 2812 | −// if ( m_dir ) | |
| 2813 | −// { | |
| 2814 | −// EM_error3( "[chuck](via FileIO): cannot read on directory" ); | |
| 2815 | −// return 0; | |
| 2816 | −// } | |
| 2817 | −// | |
| 2818 | −// if (m_io.fail()) { | |
| 2819 | −// EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" ); | |
| 2820 | −// return 0; | |
| 2821 | −// } | |
| 2822 | −// | |
| 2823 | −// if (m_flags & TYPE_ASCII) { | |
| 2824 | −// // ASCII | |
| 2825 | −// t_CKINT val = 0; | |
| 2826 | −// m_io >> val; | |
| 2827 | −// // if (m_io.fail()) | |
| 2828 | −// // EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" ); | |
| 2829 | −// return val; | |
| 2830 | −// | |
| 2831 | −// } else if (m_flags & TYPE_BINARY) { | |
| 2832 | −// // binary | |
| 2833 | −// if (flags & Chuck_IO::INT32) { | |
| 2834 | −// // 32-bit | |
| 2835 | −// t_CKINT i; | |
| 2836 | −// m_io.read( (char *)&i, 4 ); | |
| 2837 | −// if (m_io.gcount() != 4) | |
| 2838 | −// EM_error3( "[chuck](via FileIO): cannot readInt: not enough bytes left" ); | |
| 2839 | −// else if (m_io.fail()) | |
| 2840 | −// EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" ); | |
| 2841 | −// return i; | |
| 2842 | −// } else if (flags & Chuck_IO::INT16) { | |
| 2843 | −// // 16-bit | |
| 2844 | −// // int16_t i; | |
| 2845 | −// // issue: 64-bit | |
| 2846 | −// short i; | |
| 2847 | −// m_io.read( (char *)&i, 2 ); | |
| 2848 | −// if (m_io.gcount() != 2) | |
| 2849 | −// EM_error3( "[chuck](via FileIO): cannot readInt: not enough bytes left" ); | |
| 2850 | −// else if (m_io.fail()) | |
| 2851 | −// EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" ); | |
| 2852 | −// return (t_CKINT) i; | |
| 2853 | −// } else if (flags & Chuck_IO::INT8) { | |
| 2854 | −// // 8-bit | |
| 2855 | −// // int8_t i; | |
| 2856 | −// // issue: 64-bit | |
| 2857 | −// char i; | |
| 2858 | −// m_io.read( (char *)&i, 1 ); | |
| 2859 | −// if (m_io.gcount() != 1) | |
| 2860 | −// EM_error3( "[chuck](via FileIO): cannot readInt: not enough bytes left" ); | |
| 2861 | −// else if (m_io.fail()) | |
| 2862 | −// EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" ); | |
| 2863 | −// return (t_CKINT) i; | |
| 2864 | −// } else { | |
| 2865 | −// EM_error3( "[chuck](via FileIO): readInt error: invalid int size flag" ); | |
| 2866 | −// return 0; | |
| 2867 | −// } | |
| 2868 | −// | |
| 2869 | −// } else { | |
| 2870 | −// EM_error3( "[chuck](via FileIO): readInt error: invalid ASCII/binary flag" ); | |
| 2871 | −// return 0; | |
| 2872 | −// } | |
| 2873 | −//} | |
| 2874 | −// | |
| 2875 | −// | |
| 2876 | −// | |
| 2877 | −// | |
| 2878 | −////----------------------------------------------------------------------------- | |
| 2879 | −//// name: readFloat() | |
| 2880 | −//// desc: read next as (ascii) floating point value | |
| 2881 | −////----------------------------------------------------------------------------- | |
| 2882 | −//t_CKFLOAT Chuck_IO_File::readFloat() | |
| 2883 | −//{ | |
| 2884 | −// // sanity | |
| 2885 | −// if (!(m_io.is_open())) { | |
| 2886 | −// EM_error3( "[chuck](via FileIO): cannot readFloat: no file open" ); | |
| 2887 | −// return 0; | |
| 2888 | −// } | |
| 2889 | −// | |
| 2890 | −// if (m_io.eof()) { | |
| 2891 | −// EM_error3( "[chuck](via FileIO): cannot readFloat: EOF reached" ); | |
| 2892 | −// return 0; | |
| 2893 | −// } | |
| 2894 | −// | |
| 2895 | −// if (m_io.fail()) { | |
| 2896 | −// EM_error3( "[chuck](via FileIO): cannot readFloat: I/O stream failed" ); | |
| 2897 | −// return 0; | |
| 2898 | −// } | |
| 2899 | −// | |
| 2900 | −// if ( m_dir ) | |
| 2901 | −// { | |
| 2902 | −// EM_error3( "[chuck](via FileIO): cannot read a directory" ); | |
| 2903 | −// return 0; | |
| 2904 | −// } | |
| 2905 | −// | |
| 2906 | −// if (m_flags & TYPE_ASCII) { | |
| 2907 | −// // ASCII | |
| 2908 | −// t_CKFLOAT val = 0; | |
| 2909 | −// m_io >> val; | |
| 2910 | −// // if (m_io.fail()) | |
| 2911 | −// // EM_error3( "[chuck](via FileIO): cannot readFloat: I/O stream failed" ); | |
| 2912 | −// return val; | |
| 2913 | −// | |
| 2914 | −// } else if (m_flags & TYPE_BINARY) { | |
| 2915 | −// // binary | |
| 2916 | −// t_CKFLOAT i; | |
| 2917 | −// m_io.read( (char *)&i, sizeof(t_CKFLOAT) ); | |
| 2918 | −// if (m_io.gcount() != sizeof(t_CKFLOAT) ) | |
| 2919 | −// EM_error3( "[chuck](via FileIO): cannot readFloat: not enough bytes left" ); | |
| 2920 | −// else if (m_io.fail()) | |
| 2921 | −// EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" ); | |
| 2922 | −// return i; | |
| 2923 | −// | |
| 2924 | −// } else { | |
| 2925 | −// EM_error3( "[chuck](via FileIO): readFloat error: invalid ASCII/binary flag" ); | |
| 2926 | −// return 0; | |
| 2927 | −// } | |
| 2928 | −//} | |
| 2929 | −// | |
| 2930 | −// | |
| 2931 | −// | |
| 2932 | −// | |
| 2933 | −////----------------------------------------------------------------------------- | |
| 2934 | −//// name: readString() | |
| 2935 | −//// desc: ... | |
| 2936 | −////----------------------------------------------------------------------------- | |
| 2937 | −//t_CKBOOL Chuck_IO_File::readString( std::string & str ) | |
| 2938 | −//{ | |
| 2939 | −// // set | |
| 2940 | −// str = ""; | |
| 2941 | −// | |
| 2942 | −// // sanity | |
| 2943 | −// if (!(m_io.is_open())) { | |
| 2944 | −// EM_error3( "[chuck](via FileIO): cannot readString: no file open" ); | |
| 2945 | −// return FALSE; | |
| 2946 | −// } | |
| 2947 | −// | |
| 2948 | −// if (m_io.eof()) { | |
| 2949 | −// EM_error3( "[chuck](via FileIO): cannot readString: EOF reached" ); | |
| 2950 | −// return FALSE; | |
| 2951 | −// } | |
| 2952 | −// | |
| 2953 | −// if ( m_dir ) | |
| 2954 | −// { | |
| 2955 | −// EM_error3( "[chuck](via FileIO): cannot read on directory" ); | |
| 2956 | −// return FALSE; | |
| 2957 | −// } | |
| 2958 | −// | |
| 2959 | −// if (m_io.fail()) { | |
| 2960 | −// EM_error3( "[chuck](via FileIO): cannot readString: I/O stream failed" ); | |
| 2961 | −// return FALSE; | |
| 2962 | −// } | |
| 2963 | −// | |
| 2964 | −// if (m_flags & TYPE_ASCII) { | |
| 2965 | −// // ASCII | |
| 2966 | −// m_io >> str; | |
| 2967 | −// return TRUE; | |
| 2968 | −// } else if (m_flags & TYPE_BINARY) { | |
| 2969 | −// EM_error3( "[chuck](via FileIO): readString not supported for binary mode" ); | |
| 2970 | −// return FALSE; | |
| 2971 | −// } else { | |
| 2972 | −// EM_error3( "[chuck](via FileIO): readInt error: invalid ASCII/binary flag" ); | |
| 2973 | −// return FALSE; | |
| 2974 | −// } | |
| 2975 | −//} | |
| 2441 | + if ((flags & FLAG_WRITEONLY) && | |
| 2442 | + (flags & FLAG_READONLY)) | |
| 2443 | + { | |
| 2444 | + EM_error3( "[chuck](via FileIO): conflicting flags: WRITE and READ" ); | |
| 2445 | + goto error; | |
| 2446 | + } | |
| 2447 | + | |
| 2448 | + if ((flags & FLAG_APPEND) && | |
| 2449 | + (flags & FLAG_READONLY)) | |
| 2450 | + { | |
| 2451 | + EM_error3( "[chuck](via FileIO): conflicting flags: APPEND and FLAG_READ" ); | |
| 2452 | + goto error; | |
| 2453 | + } | |
| 2454 | + | |
| 2455 | + // set open flags | |
| 2456 | + ios_base::openmode mode; | |
| 2457 | + | |
| 2458 | + if (flags & FLAG_READ_WRITE) | |
| 2459 | + mode = ios_base::in | ios_base::out; | |
| 2460 | + else if (flags & FLAG_READONLY) | |
| 2461 | + mode = ios_base::in; | |
| 2462 | + else if (flags & FLAG_APPEND) | |
| 2463 | + mode = ios_base::out | ios_base::app; | |
| 2464 | + else if (flags & FLAG_WRITEONLY) | |
| 2465 | + mode = ios_base::out | ios_base::trunc; | |
| 2466 | + | |
| 2467 | + if (flags & TYPE_BINARY) | |
| 2468 | + mode |= ios_base::binary; | |
| 2469 | + | |
| 2470 | + // close first | |
| 2471 | + if (m_io.is_open()) | |
| 2472 | + this->close(); | |
| 2473 | + | |
| 2474 | + // try to open as a dir first (fixed 1.3.0.0 removed warning) | |
| 2475 | + if( (m_dir = opendir( path.c_str() )) ) | |
| 2476 | + { | |
| 2477 | + EM_poplog(); | |
| 2478 | + return TRUE; | |
| 2479 | + } | |
| 2480 | + | |
| 2481 | + // not a dir, create file if it does not exist unless flag is | |
| 2482 | + // readonly | |
| 2483 | + if ( !(flags & FLAG_READONLY) ) | |
| 2484 | + { | |
| 2485 | + m_io.open( path.c_str(), ios_base::in ); | |
| 2486 | + if ( m_io.fail() ) | |
| 2487 | + { | |
| 2488 | + m_io.clear(); | |
| 2489 | + m_io.open( path.c_str(), ios_base::out | ios_base::trunc ); | |
| 2490 | + m_io.close(); | |
| 2491 | + } | |
| 2492 | + else | |
| 2493 | + m_io.close(); | |
| 2494 | + } | |
| 2495 | + | |
| 2496 | + //open file | |
| 2497 | + m_io.open( path.c_str(), mode ); | |
| 2498 | + | |
| 2499 | + // seek to beginning if necessary | |
| 2500 | + if (flags & FLAG_READ_WRITE) | |
| 2501 | + { | |
| 2502 | + m_io.seekp(0); | |
| 2503 | + m_io.seekg(0); | |
| 2504 | + } | |
| 2505 | + | |
| 2506 | + /* ATODO: Ge's code | |
| 2507 | + // windows sucks for being creative in the wrong places | |
| 2508 | + #ifdef __PLATFORM_WIN32__ | |
| 2509 | + // if( flags ^ Chuck_IO::TRUNCATE && flags | Chuck_IO::READ ) nMode |= ios::nocreate; | |
| 2510 | + m_io.open( path.c_str(), nMode ); | |
| 2511 | + #else | |
| 2512 | + m_io.open( path.c_str(), (_Ios_Openmode)nMode ); | |
| 2513 | + #endif | |
| 2514 | + */ | |
| 2515 | + | |
| 2516 | + // check for error | |
| 2517 | + if( !(m_io.is_open()) ) | |
| 2518 | + { | |
| 2519 | + // EM_error3( "[chuck](via FileIO): cannot open file: '%s'", path.c_str() ); | |
| 2520 | + goto error; | |
| 2521 | + } | |
| 2522 | + | |
| 2523 | + // set path | |
| 2524 | + m_path = path; | |
| 2525 | + // set flags | |
| 2526 | + m_flags = flags; | |
| 2527 | + if (!(flags & TYPE_BINARY)) | |
| 2528 | + m_flags |= Chuck_IO_File::TYPE_ASCII; // ASCII is default | |
| 2529 | + // set mode | |
| 2530 | + m_iomode = MODE_SYNC; | |
| 2531 | + | |
| 2532 | + // pop | |
| 2533 | + EM_poplog(); | |
| 2534 | + | |
| 2535 | + return TRUE; | |
| 2536 | + | |
| 2537 | +error: | |
| 2538 | + | |
| 2539 | + // pop | |
| 2540 | + EM_poplog(); | |
| 2541 | + | |
| 2542 | + // reset | |
| 2543 | + m_path = ""; | |
| 2544 | + m_flags = 0; | |
| 2545 | + m_iomode = MODE_SYNC; | |
| 2546 | + m_io.clear(); | |
| 2547 | + m_io.close(); | |
| 2548 | + | |
| 2549 | + return FALSE; | |
| 2550 | +} | |
| 2551 | + | |
| 2552 | + | |
| 2553 | + | |
| 2554 | + | |
| 2555 | +//----------------------------------------------------------------------------- | |
| 2556 | +// name: close | |
| 2557 | +// desc: close file | |
| 2558 | +//----------------------------------------------------------------------------- | |
| 2559 | +void Chuck_IO_File::close() | |
| 2560 | +{ | |
| 2561 | + // log | |
| 2562 | + EM_log( CK_LOG_INFO, "FileIO: closing file '%s'...", m_path.c_str() ); | |
| 2563 | + // close it | |
| 2564 | + m_io.close(); | |
| 2565 | + m_flags = 0; | |
| 2566 | + m_path = ""; | |
| 2567 | + m_iomode = Chuck_IO::MODE_SYNC; | |
| 2568 | + if ( m_dir ) { | |
| 2569 | + closedir( m_dir ); | |
| 2570 | + m_dir = NULL; | |
| 2571 | + m_dir_start = 0; | |
| 2572 | + } | |
| 2573 | +} | |
| 2574 | + | |
| 2575 | + | |
| 2576 | + | |
| 2577 | + | |
| 2578 | +//----------------------------------------------------------------------------- | |
| 2579 | +// name: good() | |
| 2580 | +// desc: ... | |
| 2581 | +//----------------------------------------------------------------------------- | |
| 2582 | +t_CKBOOL Chuck_IO_File::good() | |
| 2583 | +{ | |
| 2584 | + return m_dir || m_io.is_open(); | |
| 2585 | + // return (!m_dir) && (!m_io.is_open()); | |
| 2586 | +} | |
| 2587 | + | |
| 2588 | + | |
| 2589 | + | |
| 2590 | + | |
| 2591 | +//----------------------------------------------------------------------------- | |
| 2592 | +// name: flush() | |
| 2593 | +// desc: ... | |
| 2594 | +//----------------------------------------------------------------------------- | |
| 2595 | +void Chuck_IO_File::flush() | |
| 2596 | +{ | |
| 2597 | + // sanity | |
| 2598 | + if ( m_dir ) | |
| 2599 | + { | |
| 2600 | + EM_error3( "[chuck](via FileIO): cannot flush on directory" ); | |
| 2601 | + return; | |
| 2602 | + } | |
| 2603 | + m_io.flush(); | |
| 2604 | +} | |
| 2605 | + | |
| 2606 | + | |
| 2607 | + | |
| 2608 | + | |
| 2609 | +//----------------------------------------------------------------------------- | |
| 2610 | +// name: mode() | |
| 2611 | +// desc: ... | |
| 2612 | +//----------------------------------------------------------------------------- | |
| 2613 | +t_CKINT Chuck_IO_File::mode() | |
| 2614 | +{ | |
| 2615 | + // sanity | |
| 2616 | + if ( m_dir ) | |
| 2617 | + { | |
| 2618 | + EM_error3( "[chuck](via FileIO): cannot get mode on directory" ); | |
| 2619 | + return -1; | |
| 2620 | + } | |
| 2621 | + return m_iomode; | |
| 2622 | +} | |
| 2623 | + | |
| 2624 | + | |
| 2625 | + | |
| 2626 | + | |
| 2627 | +//----------------------------------------------------------------------------- | |
| 2628 | +// name: mode( t_CKINT flag ) | |
| 2629 | +// desc: ... | |
| 2630 | +//----------------------------------------------------------------------------- | |
| 2631 | +void Chuck_IO_File::mode( t_CKINT flag ) | |
| 2632 | +{ | |
| 2633 | + // sanity | |
| 2634 | + if ( m_dir ) | |
| 2635 | + { | |
| 2636 | + EM_error3( "[chuck](via FileIO): cannot set mode on directory" ); | |
| 2637 | + return; | |
| 2638 | + } | |
| 2639 | + if ( (flag != Chuck_IO::MODE_ASYNC) && (flag != Chuck_IO::MODE_SYNC) ) | |
| 2640 | + { | |
| 2641 | + EM_error3( "[chuck](via FileIO): invalid mode flag" ); | |
| 2642 | + return; | |
| 2643 | + } | |
| 2644 | + | |
| 2645 | + m_iomode = flag; | |
| 2646 | +} | |
| 2647 | + | |
| 2648 | + | |
| 2649 | + | |
| 2650 | + | |
| 2651 | +//----------------------------------------------------------------------------- | |
| 2652 | +// name: size() | |
| 2653 | +// desc: Returns the size of the file in bytes, or -1 if no file is opened or | |
| 2654 | +// if a directory is opened. | |
| 2655 | +//----------------------------------------------------------------------------- | |
| 2656 | +t_CKINT Chuck_IO_File::size() | |
| 2657 | +{ | |
| 2658 | + if (!(m_io.is_open())) return -1; | |
| 2659 | + if ( m_dir ) | |
| 2660 | + { | |
| 2661 | + EM_error3( "[chuck](via FileIO): cannot get size on a directory" ); | |
| 2662 | + return -1; | |
| 2663 | + } | |
| 2664 | + | |
| 2665 | + // no easy way to find file size in C++ | |
| 2666 | + // have to seek to end, report position | |
| 2667 | + FILE * stream = fopen( m_path.c_str(), "r" ); | |
| 2668 | + fseek( stream, 0L, SEEK_END ); | |
| 2669 | + int endPos = ftell( stream ); | |
| 2670 | + fclose( stream ); | |
| 2671 | + return endPos; | |
| 2672 | +} | |
| 2673 | + | |
| 2674 | + | |
| 2675 | + | |
| 2676 | + | |
| 2677 | +//----------------------------------------------------------------------------- | |
| 2678 | +// name: seek( t_CKINT pos ) | |
| 2679 | +// desc: Seeks to the specified byte offset in the file. | |
| 2680 | +//----------------------------------------------------------------------------- | |
| 2681 | +void Chuck_IO_File::seek( t_CKINT pos ) | |
| 2682 | +{ | |
| 2683 | + if ( !(m_io.is_open()) ) | |
| 2684 | + { | |
| 2685 | + EM_error3( "[chuck](via FileIO): cannot seek: no file is open" ); | |
| 2686 | + return; | |
| 2687 | + } | |
| 2688 | + if ( m_dir ) | |
| 2689 | + { | |
| 2690 | + EM_error3( "[chuck](via FileIO): cannot seek on a directory" ); | |
| 2691 | + return; | |
| 2692 | + } | |
| 2693 | + m_io.seekg( pos ); | |
| 2694 | + m_io.seekp( pos ); | |
| 2695 | +} | |
| 2696 | + | |
| 2697 | + | |
| 2698 | + | |
| 2699 | + | |
| 2700 | +//----------------------------------------------------------------------------- | |
| 2701 | +// name: tell() | |
| 2702 | +// desc: Returns the byte offset into the file, or -1 if no file is opened. | |
| 2703 | +//----------------------------------------------------------------------------- | |
| 2704 | +t_CKINT Chuck_IO_File::tell() | |
| 2705 | +{ | |
| 2706 | + if (!(m_io.is_open())) | |
| 2707 | + return -1; | |
| 2708 | + if ( m_dir ) | |
| 2709 | + { | |
| 2710 | + EM_error3( "[chuck](via FileIO): cannot tell on directory" ); | |
| 2711 | + return -1; | |
| 2712 | + } | |
| 2713 | + | |
| 2714 | + return m_io.tellg(); | |
| 2715 | +} | |
| 2716 | + | |
| 2717 | + | |
| 2718 | + | |
| 2719 | + | |
| 2720 | +//----------------------------------------------------------------------------- | |
| 2721 | +// name: isDir() | |
| 2722 | +// desc: ... | |
| 2723 | +//----------------------------------------------------------------------------- | |
| 2724 | +t_CKINT Chuck_IO_File::isDir() | |
| 2725 | +{ | |
| 2726 | + return m_dir != NULL; | |
| 2727 | +} | |
| 2728 | + | |
| 2729 | + | |
| 2730 | + | |
| 2731 | + | |
| 2732 | +//----------------------------------------------------------------------------- | |
| 2733 | +// name: dirList() | |
| 2734 | +// desc: ... | |
| 2735 | +//----------------------------------------------------------------------------- | |
| 2736 | +Chuck_Array4 * Chuck_IO_File::dirList() | |
| 2737 | +{ | |
| 2738 | + // sanity | |
| 2739 | + if ( !m_dir ) | |
| 2740 | + { | |
| 2741 | + EM_error3( "[chuck](via FileIO): cannot get list: no directory open" ); | |
| 2742 | + Chuck_Array4 *ret = new Chuck_Array4( TRUE, 0 ); | |
| 2743 | + initialize_object( ret, m_vmRef->env()->t_array ); | |
| 2744 | + return ret; | |
| 2745 | + } | |
| 2746 | + | |
| 2747 | + // fill vector with entry names | |
| 2748 | + rewinddir( m_dir ); | |
| 2749 | + std::vector<Chuck_String *> entrylist; | |
| 2750 | + struct dirent *ent; | |
| 2751 | + while( (ent = readdir( m_dir )) ) // fixed 1.3.0.0: removed warning | |
| 2752 | + { | |
| 2753 | + // pass NULL as shred ref | |
| 2754 | + Chuck_String *s = (Chuck_String *)instantiate_and_initialize_object( m_vmRef->env()->t_string, NULL, m_vmRef ); | |
| 2755 | + s->set( std::string( ent->d_name ) ); | |
| 2756 | + if ( s->str() != ".." && s->str() != "." ) | |
| 2757 | + { | |
| 2758 | + // don't include .. and . in the list | |
| 2759 | + entrylist.push_back( s ); | |
| 2760 | + } | |
| 2761 | + } | |
| 2762 | + | |
| 2763 | + // make array | |
| 2764 | + Chuck_Array4 *array = new Chuck_Array4( true, entrylist.size() ); | |
| 2765 | + initialize_object( array, m_vmRef->env()->t_array ); | |
| 2766 | + for ( int i = 0; i < entrylist.size(); i++ ) | |
| 2767 | + array->set( i, (t_CKUINT) entrylist[i] ); | |
| 2768 | + return array; | |
| 2769 | +} | |
| 2770 | + | |
| 2771 | + | |
| 2772 | + | |
| 2773 | + | |
| 2774 | +//----------------------------------------------------------------------------- | |
| 2775 | +// name: read( t_CKINT length ) | |
| 2776 | +// desc: ... | |
| 2777 | +//----------------------------------------------------------------------------- | |
| 2778 | +/*Chuck_String * Chuck_IO_File::read( t_CKINT length ) | |
| 2779 | +{ | |
| 2780 | + // sanity | |
| 2781 | + if (!(m_io.is_open())) { | |
| 2782 | + EM_error3( "[chuck](via FileIO): cannot read: no file open" ); | |
| 2783 | + return new Chuck_String( "" ); | |
| 2784 | + } | |
| 2785 | + | |
| 2786 | + if (m_io.fail()) { | |
| 2787 | + EM_error3( "[chuck](via FileIO): cannot read: I/O stream failed" ); | |
| 2788 | + return new Chuck_String( "" ); | |
| 2789 | + } | |
| 2790 | + | |
| 2791 | + if ( m_dir ) | |
| 2792 | + { | |
| 2793 | + EM_error3( "[chuck](via FileIO): cannot read on a directory" ); | |
| 2794 | + return new Chuck_String( "" ); | |
| 2795 | + } | |
| 2796 | + | |
| 2797 | + char buf[length+1]; | |
| 2798 | + m_io.read( buf, length ); | |
| 2799 | + buf[m_io.gcount()] = '\0'; | |
| 2800 | + string s( buf ); | |
| 2801 | + return new Chuck_String( s ); | |
| 2802 | +}*/ | |
| 2803 | + | |
| 2804 | + | |
| 2805 | + | |
| 2806 | + | |
| 2807 | +//----------------------------------------------------------------------------- | |
| 2808 | +// name: readLine() | |
| 2809 | +// desc: read line | |
| 2810 | +//----------------------------------------------------------------------------- | |
| 2811 | +Chuck_String * Chuck_IO_File::readLine() | |
| 2812 | +{ | |
| 2813 | + // sanity | |
| 2814 | + if (!(m_io.is_open())) { | |
| 2815 | + EM_error3( "[chuck](via FileIO): cannot readLine: no file open" ); | |
| 2816 | + return new Chuck_String( "" ); | |
| 2817 | + } | |
| 2818 | + | |
| 2819 | + if (m_io.fail()) { | |
| 2820 | + EM_error3( "[chuck](via FileIO): cannot readLine: I/O stream failed" ); | |
| 2821 | + return new Chuck_String( "" ); | |
| 2822 | + } | |
| 2823 | + | |
| 2824 | + if ( m_dir ) | |
| 2825 | + { | |
| 2826 | + EM_error3( "[chuck](via FileIO): cannot readLine on directory" ); | |
| 2827 | + return new Chuck_String( "" ); | |
| 2828 | + } | |
| 2829 | + | |
| 2830 | + string s; | |
| 2831 | + getline( m_io, s ); | |
| 2832 | + return new Chuck_String( s ); | |
| 2833 | +} | |
| 2834 | + | |
| 2835 | + | |
| 2836 | + | |
| 2837 | + | |
| 2838 | +//----------------------------------------------------------------------------- | |
| 2839 | +// name: readInt( t_CKINT flags ) | |
| 2840 | +// desc: ... | |
| 2841 | +//----------------------------------------------------------------------------- | |
| 2842 | +t_CKINT Chuck_IO_File::readInt( t_CKINT flags ) | |
| 2843 | +{ | |
| 2844 | + // sanity | |
| 2845 | + if (!(m_io.is_open())) { | |
| 2846 | + EM_error3( "[chuck](via FileIO): cannot readInt: no file open" ); | |
| 2847 | + return 0; | |
| 2848 | + } | |
| 2849 | + | |
| 2850 | + if (m_io.eof()) { | |
| 2851 | + EM_error3( "[chuck](via FileIO): cannot readInt: EOF reached" ); | |
| 2852 | + return 0; | |
| 2853 | + } | |
| 2854 | + | |
| 2855 | + if ( m_dir ) | |
| 2856 | + { | |
| 2857 | + EM_error3( "[chuck](via FileIO): cannot read on directory" ); | |
| 2858 | + return 0; | |
| 2859 | + } | |
| 2860 | + | |
| 2861 | + if (m_io.fail()) { | |
| 2862 | + EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" ); | |
| 2863 | + return 0; | |
| 2864 | + } | |
| 2865 | + | |
| 2866 | + if (m_flags & TYPE_ASCII) { | |
| 2867 | + // ASCII | |
| 2868 | + t_CKINT val = 0; | |
| 2869 | + m_io >> val; | |
| 2870 | + // if (m_io.fail()) | |
| 2871 | + // EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" ); | |
| 2872 | + return val; | |
| 2873 | + | |
| 2874 | + } else if (m_flags & TYPE_BINARY) { | |
| 2875 | + // binary | |
| 2876 | + if (flags & Chuck_IO::INT32) { | |
| 2877 | + // 32-bit | |
| 2878 | + t_CKINT i; | |
| 2879 | + m_io.read( (char *)&i, 4 ); | |
| 2880 | + if (m_io.gcount() != 4) | |
| 2881 | + EM_error3( "[chuck](via FileIO): cannot readInt: not enough bytes left" ); | |
| 2882 | + else if (m_io.fail()) | |
| 2883 | + EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" ); | |
| 2884 | + return i; | |
| 2885 | + } else if (flags & Chuck_IO::INT16) { | |
| 2886 | + // 16-bit | |
| 2887 | + // int16_t i; | |
| 2888 | + // issue: 64-bit | |
| 2889 | + short i; | |
| 2890 | + m_io.read( (char *)&i, 2 ); | |
| 2891 | + if (m_io.gcount() != 2) | |
| 2892 | + EM_error3( "[chuck](via FileIO): cannot readInt: not enough bytes left" ); | |
| 2893 | + else if (m_io.fail()) | |
| 2894 | + EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" ); | |
| 2895 | + return (t_CKINT) i; | |
| 2896 | + } else if (flags & Chuck_IO::INT8) { | |
| 2897 | + // 8-bit | |
| 2898 | + // int8_t i; | |
| 2899 | + // issue: 64-bit | |
| 2900 | + char i; | |
| 2901 | + m_io.read( (char *)&i, 1 ); | |
| 2902 | + if (m_io.gcount() != 1) | |
| 2903 | + EM_error3( "[chuck](via FileIO): cannot readInt: not enough bytes left" ); | |
| 2904 | + else if (m_io.fail()) | |
| 2905 | + EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" ); | |
| 2906 | + return (t_CKINT) i; | |
| 2907 | + } else { | |
| 2908 | + EM_error3( "[chuck](via FileIO): readInt error: invalid int size flag" ); | |
| 2909 | + return 0; | |
| 2910 | + } | |
| 2911 | + | |
| 2912 | + } else { | |
| 2913 | + EM_error3( "[chuck](via FileIO): readInt error: invalid ASCII/binary flag" ); | |
| 2914 | + return 0; | |
| 2915 | + } | |
| 2916 | +} | |
| 2917 | + | |
| 2918 | + | |
| 2919 | + | |
| 2920 | + | |
| 2921 | +//----------------------------------------------------------------------------- | |
| 2922 | +// name: readFloat() | |
| 2923 | +// desc: read next as (ascii) floating point value | |
| 2924 | +//----------------------------------------------------------------------------- | |
| 2925 | +t_CKFLOAT Chuck_IO_File::readFloat() | |
| 2926 | +{ | |
| 2927 | + // sanity | |
| 2928 | + if (!(m_io.is_open())) { | |
| 2929 | + EM_error3( "[chuck](via FileIO): cannot readFloat: no file open" ); | |
| 2930 | + return 0; | |
| 2931 | + } | |
| 2932 | + | |
| 2933 | + if (m_io.eof()) { | |
| 2934 | + EM_error3( "[chuck](via FileIO): cannot readFloat: EOF reached" ); | |
| 2935 | + return 0; | |
| 2936 | + } | |
| 2937 | + | |
| 2938 | + if (m_io.fail()) { | |
| 2939 | + EM_error3( "[chuck](via FileIO): cannot readFloat: I/O stream failed" ); | |
| 2940 | + return 0; | |
| 2941 | + } | |
| 2942 | + | |
| 2943 | + if ( m_dir ) | |
| 2944 | + { | |
| 2945 | + EM_error3( "[chuck](via FileIO): cannot read a directory" ); | |
| 2946 | + return 0; | |
| 2947 | + } | |
| 2948 | + | |
| 2949 | + if (m_flags & TYPE_ASCII) { | |
| 2950 | + // ASCII | |
| 2951 | + t_CKFLOAT val = 0; | |
| 2952 | + m_io >> val; | |
| 2953 | + // if (m_io.fail()) | |
| 2954 | + // EM_error3( "[chuck](via FileIO): cannot readFloat: I/O stream failed" ); | |
| 2955 | + return val; | |
| 2956 | + | |
| 2957 | + } else if (m_flags & TYPE_BINARY) { | |
| 2958 | + // binary | |
| 2959 | + t_CKFLOAT i; | |
| 2960 | + m_io.read( (char *)&i, sizeof(t_CKFLOAT) ); | |
| 2961 | + if (m_io.gcount() != sizeof(t_CKFLOAT) ) | |
| 2962 | + EM_error3( "[chuck](via FileIO): cannot readFloat: not enough bytes left" ); | |
| 2963 | + else if (m_io.fail()) | |
| 2964 | + EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" ); | |
| 2965 | + return i; | |
| 2966 | + | |
| 2967 | + } else { | |
| 2968 | + EM_error3( "[chuck](via FileIO): readFloat error: invalid ASCII/binary flag" ); | |
| 2969 | + return 0; | |
| 2970 | + } | |
| 2971 | +} | |
| 2972 | + | |
| 2973 | + | |
| 2974 | + | |
| 2975 | + | |
| 2976 | +//----------------------------------------------------------------------------- | |
| 2977 | +// name: readString() | |
| 2978 | +// desc: ... | |
| 2979 | +//----------------------------------------------------------------------------- | |
| 2980 | +t_CKBOOL Chuck_IO_File::readString( std::string & str ) | |
| 2981 | +{ | |
| 2982 | + // set | |
| 2983 | + str = ""; | |
| 2984 | + | |
| 2985 | + // sanity | |
| 2986 | + if (!(m_io.is_open())) { | |
| 2987 | + EM_error3( "[chuck](via FileIO): cannot readString: no file open" ); | |
| 2988 | + return FALSE; | |
| 2989 | + } | |
| 2990 | + | |
| 2991 | + if (m_io.eof()) { | |
| 2992 | + EM_error3( "[chuck](via FileIO): cannot readString: EOF reached" ); | |
| 2993 | + return FALSE; | |
| 2994 | + } | |
| 2995 | + | |
| 2996 | + if ( m_dir ) | |
| 2997 | + { | |
| 2998 | + EM_error3( "[chuck](via FileIO): cannot read on directory" ); | |
| 2999 | + return FALSE; | |
| 3000 | + } | |
| 3001 | + | |
| 3002 | + if (m_io.fail()) { | |
| 3003 | + EM_error3( "[chuck](via FileIO): cannot readString: I/O stream failed" ); | |
| 3004 | + return FALSE; | |
| 3005 | + } | |
| 3006 | + | |
| 3007 | + if (m_flags & TYPE_ASCII) { | |
| 3008 | + // ASCII | |
| 3009 | + m_io >> str; | |
| 3010 | + return TRUE; | |
| 3011 | + } else if (m_flags & TYPE_BINARY) { | |
| 3012 | + EM_error3( "[chuck](via FileIO): readString not supported for binary mode" ); | |
| 3013 | + return FALSE; | |
| 3014 | + } else { | |
| 3015 | + EM_error3( "[chuck](via FileIO): readInt error: invalid ASCII/binary flag" ); | |
| 3016 | + return FALSE; | |
| 3017 | + } | |
| 3018 | +} | |
| 2976 | 3019 | |
| 2977 | 3020 | |
| 2978 | 3021 | |
| @@ -2981,13 +3024,13 @@ Chuck_IO::~Chuck_IO() · 2 unmodified lines | ||
| 2981 | 3024 | |
| 2982 | 3025 | THREAD_RETURN ( THREAD_TYPE Chuck_IO_File::read_thread ) ( void *data ) |
| 2983 | 3026 | { |
| 2984 | − // (ATODO: test this) | |
| 3027 | + (ATODO: test this) | |
| 2985 | 3028 | cerr << "In thread" << endl; |
| 2986 | 3029 | async_args *args = (async_args *)data; |
| 2987 | 3030 | Chuck_String *ret = (Chuck_String *)(args->RETURN); |
| 2988 | 3031 | ret = args->fileio_obj->read( args->intArg ); |
| 2989 | 3032 | cerr << "Called FileIO.read(int)" << endl; |
| 2990 | − args->fileio_obj->m_asyncEvent->broadcast(); // wake up | |
| 3033 | + args->fileio_obj->m_asyncEvent->broadcast(); wake up | |
| 2991 | 3034 | cerr << "Broadcasted on event" << endl; |
| 2992 | 3035 | cerr << "Sleeping" << endl; |
| 2993 | 3036 | sleep(5); |
| @@ -3000,18 +3043,18 @@ Chuck_IO::~Chuck_IO() · 6 unmodified lines | ||
| 3000 | 3043 | |
| 3001 | 3044 | THREAD_RETURN ( THREAD_TYPE Chuck_IO_File::readLine_thread ) ( void *data ) |
| 3002 | 3045 | { |
| 3003 | − // not yet implemented | |
| 3046 | + not yet implemented | |
| 3004 | 3047 | return NULL; |
| 3005 | 3048 | } |
| 3006 | 3049 | |
| 3007 | 3050 | THREAD_RETURN ( THREAD_TYPE Chuck_IO_File::readInt_thread ) ( void *data ) |
| 3008 | 3051 | { |
| 3009 | − // (ATODO: test this) | |
| 3052 | + (ATODO: test this) | |
| 3010 | 3053 | async_args *args = (async_args *)data; |
| 3011 | 3054 | Chuck_DL_Return *ret = (Chuck_DL_Return *)(args->RETURN); |
| 3012 | 3055 | ret->v_int = args->fileio_obj->readInt( args->intArg ); |
| 3013 | 3056 | cerr << "Called readInt, set ret->v_int to " << ret->v_int << endl; |
| 3014 | − args->fileio_obj->m_asyncEvent->broadcast(); // wake up | |
| 3057 | + args->fileio_obj->m_asyncEvent->broadcast(); wake up | |
| 3015 | 3058 | cerr << "Called broadcast" << endl; |
| 3016 | 3059 | delete args; |
| 3017 | 3060 | |
| @@ -3020,7 +3063,7 @@ Chuck_IO::~Chuck_IO() · 2 unmodified lines | ||
| 3020 | 3063 | |
| 3021 | 3064 | THREAD_RETURN ( THREAD_TYPE Chuck_IO_File::readFloat_thread ) ( void *data ) |
| 3022 | 3065 | { |
| 3023 | − // not yet implemented | |
| 3066 | + not yet implemented | |
| 3024 | 3067 | return NULL; |
| 3025 | 3068 | } |
| 3026 | 3069 | */ |
| @@ -3029,213 +3072,217 @@ Chuck_IO::~Chuck_IO() · 2 unmodified lines | ||
| 3029 | 3072 | // name: eof() |
| 3030 | 3073 | // desc: end of file? |
| 3031 | 3074 | //----------------------------------------------------------------------------- |
| 3032 | −//t_CKBOOL Chuck_IO_File::eof() | |
| 3033 | −//{ | |
| 3034 | −// if( !m_io.is_open() ) | |
| 3035 | −// { | |
| 3036 | −// // EM_error3( "[chuck](via FileIO): cannot check eof: no file open" ); | |
| 3037 | −// return TRUE; | |
| 3038 | −// } | |
| 3039 | −// if( m_dir ) | |
| 3040 | −// { | |
| 3041 | −// EM_error3( "[chuck](via FileIO): cannot check eof on directory" ); | |
| 3042 | −// return TRUE; | |
| 3043 | −// } | |
| 3044 | −// | |
| 3045 | −// return m_io.eof() || m_io.fail(); | |
| 3046 | −//} | |
| 3047 | −// | |
| 3048 | −// | |
| 3049 | −// | |
| 3050 | −// | |
| 3051 | −////----------------------------------------------------------------------------- | |
| 3052 | −//// name: write( const std::string & val ) | |
| 3053 | −//// desc: ... | |
| 3054 | −////----------------------------------------------------------------------------- | |
| 3055 | −//void Chuck_IO_File::write( const std::string & val ) | |
| 3056 | −//{ | |
| 3057 | −// // sanity | |
| 3058 | −// if (!(m_io.is_open())) { | |
| 3059 | −// EM_error3( "[chuck](via FileIO): cannot write: no file open" ); | |
| 3060 | −// return; | |
| 3061 | −// } | |
| 3062 | −// | |
| 3063 | −// if (m_io.fail()) { | |
| 3064 | −// EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" ); | |
| 3065 | −// return; | |
| 3066 | −// } | |
| 3067 | −// | |
| 3068 | −// if ( m_dir ) | |
| 3069 | −// { | |
| 3070 | −// EM_error3( "[chuck](via FileIO): cannot write to a directory" ); | |
| 3071 | −// return; | |
| 3072 | −// } | |
| 3073 | −// | |
| 3074 | −// m_io.write( val.c_str(), val.size() ); | |
| 3075 | −// | |
| 3076 | −// if (m_io.fail()) { // check both before and after write if stream is ok | |
| 3077 | −// EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" ); | |
| 3078 | −// } | |
| 3079 | −//} | |
| 3080 | −// | |
| 3081 | −// | |
| 3082 | −// | |
| 3083 | −// | |
| 3084 | −////----------------------------------------------------------------------------- | |
| 3085 | −//// name: write( t_CKINT val ) | |
| 3086 | −//// desc: ... | |
| 3087 | −////----------------------------------------------------------------------------- | |
| 3088 | −//void Chuck_IO_File::write( t_CKINT val ) | |
| 3089 | −//{ | |
| 3090 | −// // sanity | |
| 3091 | −// if (!(m_io.is_open())) { | |
| 3092 | −// EM_error3( "[chuck](via FileIO): cannot write: no file open" ); | |
| 3093 | −// return; | |
| 3094 | −// } | |
| 3095 | −// | |
| 3096 | −// if (m_io.fail()) { | |
| 3097 | −// EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" ); | |
| 3098 | −// return; | |
| 3099 | −// } | |
| 3100 | −// | |
| 3101 | −// if ( m_dir ) | |
| 3102 | −// { | |
| 3103 | −// EM_error3( "[chuck](via FileIO): cannot write on directory" ); | |
| 3104 | −// return; | |
| 3105 | −// } | |
| 3106 | −// | |
| 3107 | −// if (m_flags & TYPE_ASCII) { | |
| 3108 | −// m_io << val; | |
| 3109 | −// } else if (m_flags & TYPE_BINARY) { | |
| 3110 | −// m_io.write( (char *)&val, sizeof(t_CKINT) ); | |
| 3111 | −// } else { | |
| 3112 | −// EM_error3( "[chuck](via FileIO): write error: invalid ASCII/binary flag" ); | |
| 3113 | −// } | |
| 3114 | −// | |
| 3115 | −// if (m_io.fail()) { // check both before and after write if stream is ok | |
| 3116 | −// EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" ); | |
| 3117 | −// } | |
| 3118 | −//} | |
| 3119 | −// | |
| 3120 | −// | |
| 3121 | −// | |
| 3122 | −// | |
| 3123 | −////----------------------------------------------------------------------------- | |
| 3124 | −//// name: write( t_CKINT val ) | |
| 3125 | −//// desc: ... | |
| 3126 | −////----------------------------------------------------------------------------- | |
| 3127 | −//void Chuck_IO_File::write( t_CKINT val, t_CKINT flags ) | |
| 3128 | −//{ | |
| 3129 | −// // sanity | |
| 3130 | −// if (!(m_io.is_open())) { | |
| 3131 | −// EM_error3( "[chuck](via FileIO): cannot write: no file open" ); | |
| 3132 | −// return; | |
| 3133 | −// } | |
| 3134 | −// | |
| 3135 | −// if (m_io.fail()) { | |
| 3136 | −// EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" ); | |
| 3137 | −// return; | |
| 3138 | −// } | |
| 3139 | −// | |
| 3140 | −// if ( m_dir ) | |
| 3141 | −// { | |
| 3142 | −// EM_error3( "[chuck](via FileIO): cannot write on directory" ); | |
| 3143 | −// return; | |
| 3144 | −// } | |
| 3145 | −// | |
| 3146 | −// if (m_flags & TYPE_ASCII) { | |
| 3147 | −// m_io << val; | |
| 3148 | −// } else if (m_flags & TYPE_BINARY) { | |
| 3149 | −// int nBytes = 4; | |
| 3150 | −// if(flags & INT8) nBytes = 1; | |
| 3151 | −// else if(flags & INT16) nBytes = 2; | |
| 3152 | −// else if(flags & INT32) nBytes = 4; | |
| 3153 | −// m_io.write( (char *)&val, nBytes ); | |
| 3154 | −// } else { | |
| 3155 | −// EM_error3( "[chuck](via FileIO): write error: invalid ASCII/binary flag" ); | |
| 3156 | −// } | |
| 3157 | −// | |
| 3158 | −// if (m_io.fail()) { // check both before and after write if stream is ok | |
| 3159 | −// EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" ); | |
| 3160 | −// } | |
| 3161 | −//} | |
| 3162 | −// | |
| 3163 | −// | |
| 3164 | −// | |
| 3165 | −// | |
| 3166 | −////----------------------------------------------------------------------------- | |
| 3167 | −//// name: write( t_CKFLOAT val ) | |
| 3168 | −//// desc: ... | |
| 3169 | −////----------------------------------------------------------------------------- | |
| 3170 | −//void Chuck_IO_File::write( t_CKFLOAT val ) | |
| 3171 | −//{ | |
| 3172 | −// // sanity | |
| 3173 | −// if (!(m_io.is_open())) { | |
| 3174 | −// EM_error3( "[chuck](via FileIO): cannot write: no file open" ); | |
| 3175 | −// return; | |
| 3176 | −// } | |
| 3177 | −// | |
| 3178 | −// if (m_io.fail()) { | |
| 3179 | −// EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" ); | |
| 3180 | −// return; | |
| 3181 | −// } | |
| 3182 | −// | |
| 3183 | −// if ( m_dir ) | |
| 3184 | −// { | |
| 3185 | −// EM_error3( "[chuck](via FileIO): cannot write to a directory" ); | |
| 3186 | −// return; | |
| 3187 | −// } | |
| 3188 | −// | |
| 3189 | −// if (m_flags & TYPE_ASCII) { | |
| 3190 | −// m_io << val; | |
| 3191 | −// } else if (m_flags & TYPE_BINARY) { | |
| 3192 | −// m_io.write( (char *)&val, sizeof(t_CKFLOAT) ); | |
| 3193 | −// } else { | |
| 3194 | −// EM_error3( "[chuck](via FileIO): write error: invalid ASCII/binary flag" ); | |
| 3195 | −// } | |
| 3196 | −// | |
| 3197 | −// if (m_io.fail()) { // check both before and after write if stream is ok | |
| 3198 | −// EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" ); | |
| 3199 | −// } | |
| 3200 | −//} | |
| 3201 | −// | |
| 3202 | −// | |
| 3203 | −// | |
| 3204 | −// | |
| 3205 | −//// static helper functions for writing asynchronously | |
| 3206 | −//THREAD_RETURN ( THREAD_TYPE Chuck_IO_File::writeStr_thread ) ( void *data ) | |
| 3207 | −//{ | |
| 3208 | −// async_args *args = (async_args *)data; | |
| 3209 | −// args->fileio_obj->write ( args->stringArg ); | |
| 3210 | −// Chuck_Event *e = args->fileio_obj->m_asyncEvent; | |
| 3211 | −// delete args; | |
| 3212 | −// e->broadcast(); // wake up | |
| 3213 | −// e->broadcast_global(); | |
| 3214 | −// | |
| 3215 | −// return (THREAD_RETURN)0; | |
| 3216 | −//} | |
| 3217 | −// | |
| 3218 | −//THREAD_RETURN ( THREAD_TYPE Chuck_IO_File::writeInt_thread ) ( void *data ) | |
| 3219 | −//{ | |
| 3220 | −// async_args *args = (async_args *)data; | |
| 3221 | −// args->fileio_obj->write ( args->intArg ); | |
| 3222 | −// args->fileio_obj->m_asyncEvent->broadcast(); // wake up | |
| 3223 | −// args->fileio_obj->m_asyncEvent->broadcast_global(); | |
| 3224 | −// delete args; | |
| 3225 | −// | |
| 3226 | −// return (THREAD_RETURN)0; | |
| 3227 | −//} | |
| 3228 | −// | |
| 3229 | −//THREAD_RETURN ( THREAD_TYPE Chuck_IO_File::writeFloat_thread ) ( void *data ) | |
| 3230 | −//{ | |
| 3231 | −// async_args *args = (async_args *)data; | |
| 3232 | −// args->fileio_obj->write ( args->floatArg ); | |
| 3233 | −// args->fileio_obj->m_asyncEvent->broadcast(); // wake up | |
| 3234 | −// args->fileio_obj->m_asyncEvent->broadcast_global(); | |
| 3235 | −// delete args; | |
| 3236 | −// | |
| 3237 | −// return (THREAD_RETURN)0; | |
| 3238 | −//} | |
| 3075 | +t_CKBOOL Chuck_IO_File::eof() | |
| 3076 | +{ | |
| 3077 | + if( !m_io.is_open() ) | |
| 3078 | + { | |
| 3079 | + // EM_error3( "[chuck](via FileIO): cannot check eof: no file open" ); | |
| 3080 | + return TRUE; | |
| 3081 | + } | |
| 3082 | + if( m_dir ) | |
| 3083 | + { | |
| 3084 | + EM_error3( "[chuck](via FileIO): cannot check eof on directory" ); | |
| 3085 | + return TRUE; | |
| 3086 | + } | |
| 3087 | + | |
| 3088 | + return m_io.eof() || m_io.fail(); | |
| 3089 | +} | |
| 3090 | + | |
| 3091 | + | |
| 3092 | + | |
| 3093 | + | |
| 3094 | +//----------------------------------------------------------------------------- | |
| 3095 | +// name: write( const std::string & val ) | |
| 3096 | +// desc: ... | |
| 3097 | +//----------------------------------------------------------------------------- | |
| 3098 | +void Chuck_IO_File::write( const std::string & val ) | |
| 3099 | +{ | |
| 3100 | + // sanity | |
| 3101 | + if (!(m_io.is_open())) { | |
| 3102 | + EM_error3( "[chuck](via FileIO): cannot write: no file open" ); | |
| 3103 | + return; | |
| 3104 | + } | |
| 3105 | + | |
| 3106 | + if (m_io.fail()) { | |
| 3107 | + EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" ); | |
| 3108 | + return; | |
| 3109 | + } | |
| 3110 | + | |
| 3111 | + if ( m_dir ) | |
| 3112 | + { | |
| 3113 | + EM_error3( "[chuck](via FileIO): cannot write to a directory" ); | |
| 3114 | + return; | |
| 3115 | + } | |
| 3116 | + | |
| 3117 | + m_io.write( val.c_str(), val.size() ); | |
| 3118 | + | |
| 3119 | + if (m_io.fail()) { // check both before and after write if stream is ok | |
| 3120 | + EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" ); | |
| 3121 | + } | |
| 3122 | +} | |
| 3123 | + | |
| 3124 | + | |
| 3125 | + | |
| 3126 | + | |
| 3127 | +//----------------------------------------------------------------------------- | |
| 3128 | +// name: write( t_CKINT val ) | |
| 3129 | +// desc: ... | |
| 3130 | +//----------------------------------------------------------------------------- | |
| 3131 | +void Chuck_IO_File::write( t_CKINT val ) | |
| 3132 | +{ | |
| 3133 | + // sanity | |
| 3134 | + if (!(m_io.is_open())) { | |
| 3135 | + EM_error3( "[chuck](via FileIO): cannot write: no file open" ); | |
| 3136 | + return; | |
| 3137 | + } | |
| 3138 | + | |
| 3139 | + if (m_io.fail()) { | |
| 3140 | + EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" ); | |
| 3141 | + return; | |
| 3142 | + } | |
| 3143 | + | |
| 3144 | + if ( m_dir ) | |
| 3145 | + { | |
| 3146 | + EM_error3( "[chuck](via FileIO): cannot write on directory" ); | |
| 3147 | + return; | |
| 3148 | + } | |
| 3149 | + | |
| 3150 | + if (m_flags & TYPE_ASCII) { | |
| 3151 | + m_io << val; | |
| 3152 | + } else if (m_flags & TYPE_BINARY) { | |
| 3153 | + m_io.write( (char *)&val, sizeof(t_CKINT) ); | |
| 3154 | + } else { | |
| 3155 | + EM_error3( "[chuck](via FileIO): write error: invalid ASCII/binary flag" ); | |
| 3156 | + } | |
| 3157 | + | |
| 3158 | + if (m_io.fail()) { // check both before and after write if stream is ok | |
| 3159 | + EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" ); | |
| 3160 | + } | |
| 3161 | +} | |
| 3162 | + | |
| 3163 | + | |
| 3164 | + | |
| 3165 | + | |
| 3166 | +//----------------------------------------------------------------------------- | |
| 3167 | +// name: write( t_CKINT val ) | |
| 3168 | +// desc: ... | |
| 3169 | +//----------------------------------------------------------------------------- | |
| 3170 | +void Chuck_IO_File::write( t_CKINT val, t_CKINT flags ) | |
| 3171 | +{ | |
| 3172 | + // sanity | |
| 3173 | + if (!(m_io.is_open())) { | |
| 3174 | + EM_error3( "[chuck](via FileIO): cannot write: no file open" ); | |
| 3175 | + return; | |
| 3176 | + } | |
| 3177 | + | |
| 3178 | + if (m_io.fail()) { | |
| 3179 | + EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" ); | |
| 3180 | + return; | |
| 3181 | + } | |
| 3182 | + | |
| 3183 | + if ( m_dir ) | |
| 3184 | + { | |
| 3185 | + EM_error3( "[chuck](via FileIO): cannot write on directory" ); | |
| 3186 | + return; | |
| 3187 | + } | |
| 3188 | + | |
| 3189 | + if (m_flags & TYPE_ASCII) { | |
| 3190 | + m_io << val; | |
| 3191 | + } else if (m_flags & TYPE_BINARY) { | |
| 3192 | + int nBytes = 4; | |
| 3193 | + if(flags & INT8) nBytes = 1; | |
| 3194 | + else if(flags & INT16) nBytes = 2; | |
| 3195 | + else if(flags & INT32) nBytes = 4; | |
| 3196 | + m_io.write( (char *)&val, nBytes ); | |
| 3197 | + } else { | |
| 3198 | + EM_error3( "[chuck](via FileIO): write error: invalid ASCII/binary flag" ); | |
| 3199 | + } | |
| 3200 | + | |
| 3201 | + if (m_io.fail()) { // check both before and after write if stream is ok | |
| 3202 | + EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" ); | |
| 3203 | + } | |
| 3204 | +} | |
| 3205 | + | |
| 3206 | + | |
| 3207 | + | |
| 3208 | + | |
| 3209 | +//----------------------------------------------------------------------------- | |
| 3210 | +// name: write( t_CKFLOAT val ) | |
| 3211 | +// desc: ... | |
| 3212 | +//----------------------------------------------------------------------------- | |
| 3213 | +void Chuck_IO_File::write( t_CKFLOAT val ) | |
| 3214 | +{ | |
| 3215 | + // sanity | |
| 3216 | + if (!(m_io.is_open())) { | |
| 3217 | + EM_error3( "[chuck](via FileIO): cannot write: no file open" ); | |
| 3218 | + return; | |
| 3219 | + } | |
| 3220 | + | |
| 3221 | + if (m_io.fail()) { | |
| 3222 | + EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" ); | |
| 3223 | + return; | |
| 3224 | + } | |
| 3225 | + | |
| 3226 | + if ( m_dir ) | |
| 3227 | + { | |
| 3228 | + EM_error3( "[chuck](via FileIO): cannot write to a directory" ); | |
| 3229 | + return; | |
| 3230 | + } | |
| 3231 | + | |
| 3232 | + if (m_flags & TYPE_ASCII) { | |
| 3233 | + m_io << val; | |
| 3234 | + } else if (m_flags & TYPE_BINARY) { | |
| 3235 | + m_io.write( (char *)&val, sizeof(t_CKFLOAT) ); | |
| 3236 | + } else { | |
| 3237 | + EM_error3( "[chuck](via FileIO): write error: invalid ASCII/binary flag" ); | |
| 3238 | + } | |
| 3239 | + | |
| 3240 | + if (m_io.fail()) { // check both before and after write if stream is ok | |
| 3241 | + EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" ); | |
| 3242 | + } | |
| 3243 | +} | |
| 3244 | + | |
| 3245 | + | |
| 3246 | + | |
| 3247 | + | |
| 3248 | +// static helper functions for writing asynchronously | |
| 3249 | +THREAD_RETURN ( THREAD_TYPE Chuck_IO_File::writeStr_thread ) ( void *data ) | |
| 3250 | +{ | |
| 3251 | + async_args *args = (async_args *)data; | |
| 3252 | + args->fileio_obj->write ( args->stringArg ); | |
| 3253 | + Chuck_Event *e = args->fileio_obj->m_asyncEvent; | |
| 3254 | + delete args; | |
| 3255 | + e->broadcast(); // wake up | |
| 3256 | + e->broadcast_global(); | |
| 3257 | + | |
| 3258 | + return (THREAD_RETURN)0; | |
| 3259 | +} | |
| 3260 | + | |
| 3261 | +THREAD_RETURN ( THREAD_TYPE Chuck_IO_File::writeInt_thread ) ( void *data ) | |
| 3262 | +{ | |
| 3263 | + async_args *args = (async_args *)data; | |
| 3264 | + args->fileio_obj->write ( args->intArg ); | |
| 3265 | + args->fileio_obj->m_asyncEvent->broadcast(); // wake up | |
| 3266 | + args->fileio_obj->m_asyncEvent->broadcast_global(); | |
| 3267 | + delete args; | |
| 3268 | + | |
| 3269 | + return (THREAD_RETURN)0; | |
| 3270 | +} | |
| 3271 | + | |
| 3272 | +THREAD_RETURN ( THREAD_TYPE Chuck_IO_File::writeFloat_thread ) ( void *data ) | |
| 3273 | +{ | |
| 3274 | + async_args *args = (async_args *)data; | |
| 3275 | + args->fileio_obj->write ( args->floatArg ); | |
| 3276 | + args->fileio_obj->m_asyncEvent->broadcast(); // wake up | |
| 3277 | + args->fileio_obj->m_asyncEvent->broadcast_global(); | |
| 3278 | + delete args; | |
| 3279 | + | |
| 3280 | + return (THREAD_RETURN)0; | |
| 3281 | +} | |
| 3282 | +#endif // __DISABLE_FILEIO__ | |
| 3283 | + | |
| 3284 | + | |
| 3285 | + | |
| 3239 | 3286 | |
| 3240 | 3287 | Chuck_IO_Chout::Chuck_IO_Chout( Chuck_Carrier * carrier ) { |
| 3241 | 3288 | // store |