History

undo test commenting out code with preprocessor directives

Jack Atherton committed 7 years ago

Commit b91dd1e1 parent db1a1dd26 files · +7454 −7195Browse files

Showing the changes to src/core/chuck_oo.cpp (1 of 26 files). Show all

Unified · Split

src/core/chuck_oo.cpp+943−896
@@ -56,12 +56,14 @@ const t_CKINT Chuck_IO::INT16 = 0x2; · 55 unmodified lines
56const t_CKINT Chuck_IO::INT8 = 0x4;56const t_CKINT Chuck_IO::INT8 = 0x4;
57const t_CKINT Chuck_IO::MODE_SYNC = 0;57const t_CKINT Chuck_IO::MODE_SYNC = 0;
58const t_CKINT Chuck_IO::MODE_ASYNC = 1;58const t_CKINT Chuck_IO::MODE_ASYNC = 1;
59//const t_CKINT Chuck_IO_File::FLAG_READ_WRITE = 0x8;59#ifndef __DISABLE_FILEIO__
60//const t_CKINT Chuck_IO_File::FLAG_READONLY = 0x10;60const t_CKINT Chuck_IO_File::FLAG_READ_WRITE = 0x8;
61//const t_CKINT Chuck_IO_File::FLAG_WRITEONLY = 0x20;61const t_CKINT Chuck_IO_File::FLAG_READONLY = 0x10;
62//const t_CKINT Chuck_IO_File::FLAG_APPEND = 0x40;62const t_CKINT Chuck_IO_File::FLAG_WRITEONLY = 0x20;
63//const t_CKINT Chuck_IO_File::TYPE_ASCII = 0x80;63const t_CKINT Chuck_IO_File::FLAG_APPEND = 0x40;
64//const t_CKINT Chuck_IO_File::TYPE_BINARY = 0x100;64const t_CKINT Chuck_IO_File::TYPE_ASCII = 0x80;
65const 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//-----------------------------------------------------------------------------
1968void Chuck_Event::signal()1970void Chuck_Event::signal()
1969{1971{
1970// m_queue_lock.acquire();1972 #ifndef __DISABLE_THREADS__
1973 m_queue_lock.acquire();
1974 #endif
1971 if( !m_queue.empty() )1975 if( !m_queue.empty() )
1972 {1976 {
1973 // get the shred on top of the queue1977 // get the shred on top of the queue
@@ -1975,7 +1979,9 @@ void Chuck_Event::signal() · 1 unmodified line
1975 // pop the top1979 // pop the top
1976 m_queue.pop();1980 m_queue.pop();
1977 // release it!1981 // release it!
1978// m_queue_lock.release();1982 #ifndef __DISABLE_THREADS__
1983 m_queue_lock.release();
1984 #endif
1979 // REFACTOR-2017: BUG-FIX1985 // REFACTOR-2017: BUG-FIX
1980 // release the extra ref we added when we started waiting for this event1986 // release the extra ref we added when we started waiting for this event
1981 SAFE_RELEASE( shred->event );1987 SAFE_RELEASE( shred->event );
@@ -1991,7 +1997,9 @@ void Chuck_Event::signal() · 9 unmodified lines
1991 }1997 }
1992 else1998 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 t_CKBOOL removed = FALSE;2016 t_CKBOOL removed = FALSE;
2009 2017
2010 // lock2018 // lock
2011// m_queue_lock.acquire();2019 #ifndef __DISABLE_THREADS__
2020 m_queue_lock.acquire();
2021 #endif
2012 // while something in queue2022 // while something in queue
2013 while( !m_queue.empty() )2023 while( !m_queue.empty() )
2014 {2024 {
@@ -2038,7 +2048,9 @@ t_CKBOOL Chuck_Event::remove( Chuck_VM_Shred * shred ) · 23 unmodified lines
2038 // copy temp back to queue2048 // copy temp back to queue
2039 m_queue = temp;2049 m_queue = temp;
2040 // release lock2050 // release lock
2041// m_queue_lock.release();2051 #ifndef __DISABLE_THREADS__
2052 m_queue_lock.release();
2053 #endif
2042 2054
2043 return removed;2055 return removed;
2044}2056}
@@ -2077,7 +2089,9 @@ t_CKBOOL Chuck_Event::remove_listen( void (* cb)(void) ) · 32 unmodified lines
2077 t_CKBOOL removed = FALSE;2089 t_CKBOOL removed = FALSE;
2078 2090
2079 // lock2091 // lock
2080// m_queue_lock.acquire();2092 #ifndef __DISABLE_THREADS__
2093 m_queue_lock.acquire();
2094 #endif
2081 // while something in queue2095 // while something in queue
2082 while( !m_global_queue.empty() )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 // copy temp back to queue2114 // copy temp back to queue
2101 m_global_queue = temp;2115 m_global_queue = temp;
2102 // release lock2116 // release lock
2103// m_queue_lock.release();2117 #ifndef __DISABLE_THREADS__
2118 m_queue_lock.release();
2119 #endif
2104 2120
2105 return removed;2121 return removed;
2106}2122}
@@ -2114,7 +2130,9 @@ t_CKBOOL Chuck_Event::remove_listen( void (* cb)(void) ) · 7 unmodified lines
2114//-----------------------------------------------------------------------------2130//-----------------------------------------------------------------------------
2115void Chuck_Event::signal_global()2131void 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 if( !m_global_queue.empty() )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//-----------------------------------------------------------------------------
2147void Chuck_Event::broadcast_global()2167void Chuck_Event::broadcast_global()
2148{2168{
2149// m_queue_lock.acquire();2169 #ifndef __DISABLE_THREADS__
2170 m_queue_lock.acquire();
2171 #endif
2150 std::queue< Chuck_Global_Event_Listener > call_again;2172 std::queue< Chuck_Global_Event_Listener > call_again;
2151 2173
2152 while( !m_global_queue.empty() )2174 while( !m_global_queue.empty() )
@@ -2169,8 +2191,10 @@ void Chuck_Event::broadcast_global() · 16 unmodified lines
2169 2191
2170 // for those that should be called again, store them again2192 // for those that should be called again, store them again
2171 m_global_queue = call_again;2193 m_global_queue = call_again;
2172 2194
2173// m_queue_lock.release();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
2185void Chuck_Event::queue_broadcast( CBufferSimple * event_buffer )2209void Chuck_Event::queue_broadcast( CBufferSimple * event_buffer )
2186{2210{
2187 // TODO: handle multiple VM2211 // TODO: handle multiple VM
2188// m_queue_lock.acquire();2212 #ifndef __DISABLE_THREADS__
2213 m_queue_lock.acquire();
2214 #endif
2189 if( !m_queue.empty() )2215 if( !m_queue.empty() )
2190 {2216 {
2191 // get shred (only to get the VM ref)2217 // get shred (only to get the VM ref)
2192 Chuck_VM_Shred * shred = m_queue.front();2218 Chuck_VM_Shred * shred = m_queue.front();
2193 // release lock2219 // release lock
2194// m_queue_lock.release();2220 #ifndef __DISABLE_THREADS__
2221 m_queue_lock.release();
2222 #endif
2195 // queue the event on the vm (added 1.3.0.0: event_buffer)2223 // queue the event on the vm (added 1.3.0.0: event_buffer)
2196 shred->vm_ref->queue_event( this, 1, event_buffer );2224 shred->vm_ref->queue_event( this, 1, event_buffer );
2197 }2225 }
2198 else2226 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
2211void Chuck_Event::broadcast()2241void Chuck_Event::broadcast()
2212{2242{
2213 // lock queue2243 // lock queue
2214// m_queue_lock.acquire();2244 #ifndef __DISABLE_THREADS__
2245 m_queue_lock.acquire();
2246 #endif
2215 // while not empty2247 // while not empty
2216 while( !m_queue.empty() )2248 while( !m_queue.empty() )
2217 {2249 {
2218 // release first2250 // release first
2219// m_queue_lock.release();2251 #ifndef __DISABLE_THREADS__
2252 m_queue_lock.release();
2253 #endif
2220 // signal the next shred2254 // signal the next shred
2221 this->signal();2255 this->signal();
2222 // lock again2256 // lock again
2223// m_queue_lock.acquire();2257 #ifndef __DISABLE_THREADS__
2258 m_queue_lock.acquire();
2259 #endif
2224 }2260 }
2225 // release2261 // 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 shred->is_running = FALSE;2293 shred->is_running = FALSE;
2256 2294
2257 // add to waiting list2295 // add to waiting list
2258// m_queue_lock.acquire();2296 #ifndef __DISABLE_THREADS__
2297 m_queue_lock.acquire();
2298 #endif
2259 m_queue.push( shred );2299 m_queue.push( shred );
2260// m_queue_lock.release();2300 #ifndef __DISABLE_THREADS__
2301 m_queue_lock.release();
2302 #endif
2261 2303
2262 // add event to shred2304 // add event to shred
2263 assert( shred->event == NULL );2305 assert( shred->event == NULL );
@@ -2302,677 +2344,678 @@ Chuck_IO::~Chuck_IO() · 38 unmodified lines
2302 2344
2303 2345
2304 2346
2305////-----------------------------------------------------------------------------2347#ifndef __DISABLE_FILEIO__
2306//// name: Chuck_IO_File()2348//-----------------------------------------------------------------------------
2307//// desc: constructor2349// name: Chuck_IO_File()
2308////-----------------------------------------------------------------------------2350// desc: constructor
2309//Chuck_IO_File::Chuck_IO_File( Chuck_VM * vm )2351//-----------------------------------------------------------------------------
2310//{2352Chuck_IO_File::Chuck_IO_File( Chuck_VM * vm )
2311// m_vmRef = vm;2353{
2312// // zero things out2354 m_vmRef = vm;
2313// m_flags = 0;2355 // zero things out
2314// m_iomode = MODE_SYNC;2356 m_flags = 0;
2315// m_path = "";2357 m_iomode = MODE_SYNC;
2316// m_dir = NULL;2358 m_path = "";
2317// m_dir_start = 0;2359 m_dir = NULL;
2318// m_asyncEvent = new Chuck_Event;2360 m_dir_start = 0;
2319// initialize_object( m_asyncEvent, vm->env()->t_event );2361 m_asyncEvent = new Chuck_Event;
2320// m_thread = new XThread;2362 initialize_object( m_asyncEvent, vm->env()->t_event );
2321//}2363 m_thread = new XThread;
2322//2364}
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//}
2727 2365
2728 2366
2729 2367
2730 2368
2731//-----------------------------------------------------------------------------2369//-----------------------------------------------------------------------------
2732// name: read( t_CKINT length )2370// name: ~Chuck_IO_File()
2733// desc: ...2371// desc: destructor
2734//-----------------------------------------------------------------------------2372//-----------------------------------------------------------------------------
2735/*Chuck_String * Chuck_IO_File::read( t_CKINT length )2373Chuck_IO_File::~Chuck_IO_File()
2736{2374{
2737 // sanity2375 // clean up
2738 if (!(m_io.is_open())) {2376 this->close();
2739 EM_error3( "[chuck](via FileIO): cannot read: no file open" );2377 delete m_asyncEvent;
2740 return new Chuck_String( "" );2378 delete m_thread;
2379}
2380
2381
2382
2383
2384//-----------------------------------------------------------------------------
2385// name: open
2386// desc: open file from disk
2387//-----------------------------------------------------------------------------
2388t_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 2403
2743 if (m_io.fail()) {2404 // if both read and write, enable read and write
2744 EM_error3( "[chuck](via FileIO): cannot read: I/O stream failed" );2405 if( flags & FLAG_READONLY && flags & FLAG_WRITEONLY )
2745 return new Chuck_String( "" );2406 {
2407 flags ^= FLAG_READONLY;
2408 flags ^= FLAG_WRITEONLY;
2409 flags |= FLAG_READ_WRITE;
2746 }2410 }
2747 2411
2748 if ( m_dir )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" );2416 EM_error3( "[chuck](via FileIO): cannot open file in both ASCII and binary mode" );
2751 return new Chuck_String( "" );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//-----------------------------------------------------------------------------2441 if ((flags & FLAG_WRITEONLY) &&
2765// name: readLine()2442 (flags & FLAG_READONLY))
2766// desc: read line2443 {
2767//-----------------------------------------------------------------------------2444 EM_error3( "[chuck](via FileIO): conflicting flags: WRITE and READ" );
2768//Chuck_String * Chuck_IO_File::readLine()2445 goto error;
2769//{2446 }
2770// // sanity2447
2771// if (!(m_io.is_open())) {2448 if ((flags & FLAG_APPEND) &&
2772// EM_error3( "[chuck](via FileIO): cannot readLine: no file open" );2449 (flags & FLAG_READONLY))
2773// return new Chuck_String( "" );2450 {
2774// }2451 EM_error3( "[chuck](via FileIO): conflicting flags: APPEND and FLAG_READ" );
2775//2452 goto error;
2776// if (m_io.fail()) {2453 }
2777// EM_error3( "[chuck](via FileIO): cannot readLine: I/O stream failed" );2454
2778// return new Chuck_String( "" );2455 // set open flags
2779// }2456 ios_base::openmode mode;
2780//2457
2781// if ( m_dir )2458 if (flags & FLAG_READ_WRITE)
2782// {2459 mode = ios_base::in | ios_base::out;
2783// EM_error3( "[chuck](via FileIO): cannot readLine on directory" );2460 else if (flags & FLAG_READONLY)
2784// return new Chuck_String( "" );2461 mode = ios_base::in;
2785// }2462 else if (flags & FLAG_APPEND)
2786//2463 mode = ios_base::out | ios_base::app;
2787// string s;2464 else if (flags & FLAG_WRITEONLY)
2788// getline( m_io, s );2465 mode = ios_base::out | ios_base::trunc;
2789// return new Chuck_String( s );2466
2790//}2467 if (flags & TYPE_BINARY)
2791//2468 mode |= ios_base::binary;
2792//2469
2793//2470 // close first
2794//2471 if (m_io.is_open())
2795////-----------------------------------------------------------------------------2472 this->close();
2796//// name: readInt( t_CKINT flags )2473
2797//// desc: ...2474 // try to open as a dir first (fixed 1.3.0.0 removed warning)
2798////-----------------------------------------------------------------------------2475 if( (m_dir = opendir( path.c_str() )) )
2799//t_CKINT Chuck_IO_File::readInt( t_CKINT flags )2476 {
2800//{2477 EM_poplog();
2801// // sanity2478 return TRUE;
2802// if (!(m_io.is_open())) {2479 }
2803// EM_error3( "[chuck](via FileIO): cannot readInt: no file open" );2480
2804// return 0;2481 // not a dir, create file if it does not exist unless flag is
2805// }2482 // readonly
2806//2483 if ( !(flags & FLAG_READONLY) )
2807// if (m_io.eof()) {2484 {
2808// EM_error3( "[chuck](via FileIO): cannot readInt: EOF reached" );2485 m_io.open( path.c_str(), ios_base::in );
2809// return 0;2486 if ( m_io.fail() )
2810// }2487 {
2811//2488 m_io.clear();
2812// if ( m_dir )2489 m_io.open( path.c_str(), ios_base::out | ios_base::trunc );
2813// {2490 m_io.close();
2814// EM_error3( "[chuck](via FileIO): cannot read on directory" );2491 }
2815// return 0;2492 else
2816// }2493 m_io.close();
2817//2494 }
2818// if (m_io.fail()) {2495
2819// EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" );2496 //open file
2820// return 0;2497 m_io.open( path.c_str(), mode );
2821// }2498
2822//2499 // seek to beginning if necessary
2823// if (m_flags & TYPE_ASCII) {2500 if (flags & FLAG_READ_WRITE)
2824// // ASCII2501 {
2825// t_CKINT val = 0;2502 m_io.seekp(0);
2826// m_io >> val;2503 m_io.seekg(0);
2827// // if (m_io.fail())2504 }
2828// // EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" );2505
2829// return val;2506 /* ATODO: Ge's code
2830//2507 // windows sucks for being creative in the wrong places
2831// } else if (m_flags & TYPE_BINARY) {2508 #ifdef __PLATFORM_WIN32__
2832// // binary2509 // if( flags ^ Chuck_IO::TRUNCATE && flags | Chuck_IO::READ ) nMode |= ios::nocreate;
2833// if (flags & Chuck_IO::INT32) {2510 m_io.open( path.c_str(), nMode );
2834// // 32-bit2511 #else
2835// t_CKINT i;2512 m_io.open( path.c_str(), (_Ios_Openmode)nMode );
2836// m_io.read( (char *)&i, 4 );2513 #endif
2837// if (m_io.gcount() != 4)2514 */
2838// EM_error3( "[chuck](via FileIO): cannot readInt: not enough bytes left" );2515
2839// else if (m_io.fail())2516 // check for error
2840// EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" );2517 if( !(m_io.is_open()) )
2841// return i;2518 {
2842// } else if (flags & Chuck_IO::INT16) {2519 // EM_error3( "[chuck](via FileIO): cannot open file: '%s'", path.c_str() );
2843// // 16-bit2520 goto error;
2844// // int16_t i;2521 }
2845// // issue: 64-bit2522
2846// short i;2523 // set path
2847// m_io.read( (char *)&i, 2 );2524 m_path = path;
2848// if (m_io.gcount() != 2)2525 // set flags
2849// EM_error3( "[chuck](via FileIO): cannot readInt: not enough bytes left" );2526 m_flags = flags;
2850// else if (m_io.fail())2527 if (!(flags & TYPE_BINARY))
2851// EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" );2528 m_flags |= Chuck_IO_File::TYPE_ASCII; // ASCII is default
2852// return (t_CKINT) i;2529 // set mode
2853// } else if (flags & Chuck_IO::INT8) {2530 m_iomode = MODE_SYNC;
2854// // 8-bit2531
2855// // int8_t i;2532 // pop
2856// // issue: 64-bit2533 EM_poplog();
2857// char i;2534
2858// m_io.read( (char *)&i, 1 );2535 return TRUE;
2859// if (m_io.gcount() != 1)2536
2860// EM_error3( "[chuck](via FileIO): cannot readInt: not enough bytes left" );2537error:
2861// else if (m_io.fail())2538
2862// EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" );2539 // pop
2863// return (t_CKINT) i;2540 EM_poplog();
2864// } else {2541
2865// EM_error3( "[chuck](via FileIO): readInt error: invalid int size flag" );2542 // reset
2866// return 0;2543 m_path = "";
2867// }2544 m_flags = 0;
2868//2545 m_iomode = MODE_SYNC;
2869// } else {2546 m_io.clear();
2870// EM_error3( "[chuck](via FileIO): readInt error: invalid ASCII/binary flag" );2547 m_io.close();
2871// return 0;2548
2872// }2549 return FALSE;
2873//}2550}
2874//2551
2875//2552
2876//2553
2877//2554
2878////-----------------------------------------------------------------------------2555//-----------------------------------------------------------------------------
2879//// name: readFloat()2556// name: close
2880//// desc: read next as (ascii) floating point value2557// desc: close file
2881////-----------------------------------------------------------------------------2558//-----------------------------------------------------------------------------
2882//t_CKFLOAT Chuck_IO_File::readFloat()2559void Chuck_IO_File::close()
2883//{2560{
2884// // sanity2561 // log
2885// if (!(m_io.is_open())) {2562 EM_log( CK_LOG_INFO, "FileIO: closing file '%s'...", m_path.c_str() );
2886// EM_error3( "[chuck](via FileIO): cannot readFloat: no file open" );2563 // close it
2887// return 0;2564 m_io.close();
2888// }2565 m_flags = 0;
2889//2566 m_path = "";
2890// if (m_io.eof()) {2567 m_iomode = Chuck_IO::MODE_SYNC;
2891// EM_error3( "[chuck](via FileIO): cannot readFloat: EOF reached" );2568 if ( m_dir ) {
2892// return 0;2569 closedir( m_dir );
2893// }2570 m_dir = NULL;
2894//2571 m_dir_start = 0;
2895// if (m_io.fail()) {2572 }
2896// EM_error3( "[chuck](via FileIO): cannot readFloat: I/O stream failed" );2573}
2897// return 0;2574
2898// }2575
2899//2576
2900// if ( m_dir )2577
2901// {2578//-----------------------------------------------------------------------------
2902// EM_error3( "[chuck](via FileIO): cannot read a directory" );2579// name: good()
2903// return 0;2580// desc: ...
2904// }2581//-----------------------------------------------------------------------------
2905//2582t_CKBOOL Chuck_IO_File::good()
2906// if (m_flags & TYPE_ASCII) {2583{
2907// // ASCII2584 return m_dir || m_io.is_open();
2908// t_CKFLOAT val = 0;2585 // return (!m_dir) && (!m_io.is_open());
2909// m_io >> val;2586}
2910// // if (m_io.fail())2587
2911// // EM_error3( "[chuck](via FileIO): cannot readFloat: I/O stream failed" );2588
2912// return val;2589
2913//2590
2914// } else if (m_flags & TYPE_BINARY) {2591//-----------------------------------------------------------------------------
2915// // binary2592// name: flush()
2916// t_CKFLOAT i;2593// desc: ...
2917// m_io.read( (char *)&i, sizeof(t_CKFLOAT) );2594//-----------------------------------------------------------------------------
2918// if (m_io.gcount() != sizeof(t_CKFLOAT) )2595void Chuck_IO_File::flush()
2919// EM_error3( "[chuck](via FileIO): cannot readFloat: not enough bytes left" );2596{
2920// else if (m_io.fail())2597 // sanity
2921// EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" );2598 if ( m_dir )
2922// return i;2599 {
2923//2600 EM_error3( "[chuck](via FileIO): cannot flush on directory" );
2924// } else {2601 return;
2925// EM_error3( "[chuck](via FileIO): readFloat error: invalid ASCII/binary flag" );2602 }
2926// return 0;2603 m_io.flush();
2927// }2604}
2928//}2605
2929//2606
2930//2607
2931//2608
2932//2609//-----------------------------------------------------------------------------
2933////-----------------------------------------------------------------------------2610// name: mode()
2934//// name: readString()2611// desc: ...
2935//// desc: ...2612//-----------------------------------------------------------------------------
2936////-----------------------------------------------------------------------------2613t_CKINT Chuck_IO_File::mode()
2937//t_CKBOOL Chuck_IO_File::readString( std::string & str )2614{
2938//{2615 // sanity
2939// // set2616 if ( m_dir )
2940// str = "";2617 {
2941//2618 EM_error3( "[chuck](via FileIO): cannot get mode on directory" );
2942// // sanity2619 return -1;
2943// if (!(m_io.is_open())) {2620 }
2944// EM_error3( "[chuck](via FileIO): cannot readString: no file open" );2621 return m_iomode;
2945// return FALSE;2622}
2946// }2623
2947//2624
2948// if (m_io.eof()) {2625
2949// EM_error3( "[chuck](via FileIO): cannot readString: EOF reached" );2626
2950// return FALSE;2627//-----------------------------------------------------------------------------
2951// }2628// name: mode( t_CKINT flag )
2952//2629// desc: ...
2953// if ( m_dir )2630//-----------------------------------------------------------------------------
2954// {2631void Chuck_IO_File::mode( t_CKINT flag )
2955// EM_error3( "[chuck](via FileIO): cannot read on directory" );2632{
2956// return FALSE;2633 // sanity
2957// }2634 if ( m_dir )
2958//2635 {
2959// if (m_io.fail()) {2636 EM_error3( "[chuck](via FileIO): cannot set mode on directory" );
2960// EM_error3( "[chuck](via FileIO): cannot readString: I/O stream failed" );2637 return;
2961// return FALSE;2638 }
2962// }2639 if ( (flag != Chuck_IO::MODE_ASYNC) && (flag != Chuck_IO::MODE_SYNC) )
2963//2640 {
2964// if (m_flags & TYPE_ASCII) {2641 EM_error3( "[chuck](via FileIO): invalid mode flag" );
2965// // ASCII2642 return;
2966// m_io >> str;2643 }
2967// return TRUE;2644
2968// } else if (m_flags & TYPE_BINARY) {2645 m_iomode = flag;
2969// EM_error3( "[chuck](via FileIO): readString not supported for binary mode" );2646}
2970// return FALSE;2647
2971// } else {2648
2972// EM_error3( "[chuck](via FileIO): readInt error: invalid ASCII/binary flag" );2649
2973// return FALSE;2650
2974// }2651//-----------------------------------------------------------------------------
2975//}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//-----------------------------------------------------------------------------
2656t_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//-----------------------------------------------------------------------------
2681void 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//-----------------------------------------------------------------------------
2704t_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//-----------------------------------------------------------------------------
2724t_CKINT Chuck_IO_File::isDir()
2725{
2726 return m_dir != NULL;
2727}
2728
2729
2730
2731
2732//-----------------------------------------------------------------------------
2733// name: dirList()
2734// desc: ...
2735//-----------------------------------------------------------------------------
2736Chuck_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//-----------------------------------------------------------------------------
2811Chuck_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//-----------------------------------------------------------------------------
2842t_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//-----------------------------------------------------------------------------
2925t_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//-----------------------------------------------------------------------------
2980t_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 THREAD_RETURN ( THREAD_TYPE Chuck_IO_File::read_thread ) ( void *data )3025 THREAD_RETURN ( THREAD_TYPE Chuck_IO_File::read_thread ) ( void *data )
2983 {3026 {
2984 // (ATODO: test this)3027 (ATODO: test this)
2985 cerr << "In thread" << endl;3028 cerr << "In thread" << endl;
2986 async_args *args = (async_args *)data;3029 async_args *args = (async_args *)data;
2987 Chuck_String *ret = (Chuck_String *)(args->RETURN);3030 Chuck_String *ret = (Chuck_String *)(args->RETURN);
2988 ret = args->fileio_obj->read( args->intArg );3031 ret = args->fileio_obj->read( args->intArg );
2989 cerr << "Called FileIO.read(int)" << endl;3032 cerr << "Called FileIO.read(int)" << endl;
2990 args->fileio_obj->m_asyncEvent->broadcast(); // wake up3033 args->fileio_obj->m_asyncEvent->broadcast(); wake up
2991 cerr << "Broadcasted on event" << endl;3034 cerr << "Broadcasted on event" << endl;
2992 cerr << "Sleeping" << endl;3035 cerr << "Sleeping" << endl;
2993 sleep(5);3036 sleep(5);
@@ -3000,18 +3043,18 @@ Chuck_IO::~Chuck_IO() · 6 unmodified lines
3000 3043
3001 THREAD_RETURN ( THREAD_TYPE Chuck_IO_File::readLine_thread ) ( void *data )3044 THREAD_RETURN ( THREAD_TYPE Chuck_IO_File::readLine_thread ) ( void *data )
3002 {3045 {
3003 // not yet implemented3046 not yet implemented
3004 return NULL;3047 return NULL;
3005 }3048 }
3006 3049
3007 THREAD_RETURN ( THREAD_TYPE Chuck_IO_File::readInt_thread ) ( void *data )3050 THREAD_RETURN ( THREAD_TYPE Chuck_IO_File::readInt_thread ) ( void *data )
3008 {3051 {
3009 // (ATODO: test this)3052 (ATODO: test this)
3010 async_args *args = (async_args *)data;3053 async_args *args = (async_args *)data;
3011 Chuck_DL_Return *ret = (Chuck_DL_Return *)(args->RETURN);3054 Chuck_DL_Return *ret = (Chuck_DL_Return *)(args->RETURN);
3012 ret->v_int = args->fileio_obj->readInt( args->intArg );3055 ret->v_int = args->fileio_obj->readInt( args->intArg );
3013 cerr << "Called readInt, set ret->v_int to " << ret->v_int << endl;3056 cerr << "Called readInt, set ret->v_int to " << ret->v_int << endl;
3014 args->fileio_obj->m_asyncEvent->broadcast(); // wake up3057 args->fileio_obj->m_asyncEvent->broadcast(); wake up
3015 cerr << "Called broadcast" << endl;3058 cerr << "Called broadcast" << endl;
3016 delete args;3059 delete args;
3017 3060
@@ -3020,7 +3063,7 @@ Chuck_IO::~Chuck_IO() · 2 unmodified lines
3020 3063
3021 THREAD_RETURN ( THREAD_TYPE Chuck_IO_File::readFloat_thread ) ( void *data )3064 THREAD_RETURN ( THREAD_TYPE Chuck_IO_File::readFloat_thread ) ( void *data )
3022 {3065 {
3023 // not yet implemented3066 not yet implemented
3024 return NULL;3067 return NULL;
3025 }3068 }
3026 */3069 */
@@ -3029,213 +3072,217 @@ Chuck_IO::~Chuck_IO() · 2 unmodified lines
3029// name: eof()3072// name: eof()
3030// desc: end of file?3073// desc: end of file?
3031//-----------------------------------------------------------------------------3074//-----------------------------------------------------------------------------
3032//t_CKBOOL Chuck_IO_File::eof()3075t_CKBOOL Chuck_IO_File::eof()
3033//{3076{
3034// if( !m_io.is_open() )3077 if( !m_io.is_open() )
3035// {3078 {
3036// // EM_error3( "[chuck](via FileIO): cannot check eof: no file open" );3079 // EM_error3( "[chuck](via FileIO): cannot check eof: no file open" );
3037// return TRUE;3080 return TRUE;
3038// }3081 }
3039// if( m_dir )3082 if( m_dir )
3040// {3083 {
3041// EM_error3( "[chuck](via FileIO): cannot check eof on directory" );3084 EM_error3( "[chuck](via FileIO): cannot check eof on directory" );
3042// return TRUE;3085 return TRUE;
3043// }3086 }
3044//3087
3045// return m_io.eof() || m_io.fail();3088 return m_io.eof() || m_io.fail();
3046//}3089}
3047//3090
3048//3091
3049//3092
3050//3093
3051////-----------------------------------------------------------------------------3094//-----------------------------------------------------------------------------
3052//// name: write( const std::string & val )3095// name: write( const std::string & val )
3053//// desc: ...3096// desc: ...
3054////-----------------------------------------------------------------------------3097//-----------------------------------------------------------------------------
3055//void Chuck_IO_File::write( const std::string & val )3098void Chuck_IO_File::write( const std::string & val )
3056//{3099{
3057// // sanity3100 // sanity
3058// if (!(m_io.is_open())) {3101 if (!(m_io.is_open())) {
3059// EM_error3( "[chuck](via FileIO): cannot write: no file open" );3102 EM_error3( "[chuck](via FileIO): cannot write: no file open" );
3060// return;3103 return;
3061// }3104 }
3062//3105
3063// if (m_io.fail()) {3106 if (m_io.fail()) {
3064// EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );3107 EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );
3065// return;3108 return;
3066// }3109 }
3067//3110
3068// if ( m_dir )3111 if ( m_dir )
3069// {3112 {
3070// EM_error3( "[chuck](via FileIO): cannot write to a directory" );3113 EM_error3( "[chuck](via FileIO): cannot write to a directory" );
3071// return;3114 return;
3072// }3115 }
3073//3116
3074// m_io.write( val.c_str(), val.size() );3117 m_io.write( val.c_str(), val.size() );
3075//3118
3076// if (m_io.fail()) { // check both before and after write if stream is ok3119 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" );3120 EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );
3078// }3121 }
3079//}3122}
3080//3123
3081//3124
3082//3125
3083//3126
3084////-----------------------------------------------------------------------------3127//-----------------------------------------------------------------------------
3085//// name: write( t_CKINT val )3128// name: write( t_CKINT val )
3086//// desc: ...3129// desc: ...
3087////-----------------------------------------------------------------------------3130//-----------------------------------------------------------------------------
3088//void Chuck_IO_File::write( t_CKINT val )3131void Chuck_IO_File::write( t_CKINT val )
3089//{3132{
3090// // sanity3133 // sanity
3091// if (!(m_io.is_open())) {3134 if (!(m_io.is_open())) {
3092// EM_error3( "[chuck](via FileIO): cannot write: no file open" );3135 EM_error3( "[chuck](via FileIO): cannot write: no file open" );
3093// return;3136 return;
3094// }3137 }
3095//3138
3096// if (m_io.fail()) {3139 if (m_io.fail()) {
3097// EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );3140 EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );
3098// return;3141 return;
3099// }3142 }
3100//3143
3101// if ( m_dir )3144 if ( m_dir )
3102// {3145 {
3103// EM_error3( "[chuck](via FileIO): cannot write on directory" );3146 EM_error3( "[chuck](via FileIO): cannot write on directory" );
3104// return;3147 return;
3105// }3148 }
3106//3149
3107// if (m_flags & TYPE_ASCII) {3150 if (m_flags & TYPE_ASCII) {
3108// m_io << val;3151 m_io << val;
3109// } else if (m_flags & TYPE_BINARY) {3152 } else if (m_flags & TYPE_BINARY) {
3110// m_io.write( (char *)&val, sizeof(t_CKINT) );3153 m_io.write( (char *)&val, sizeof(t_CKINT) );
3111// } else {3154 } else {
3112// EM_error3( "[chuck](via FileIO): write error: invalid ASCII/binary flag" );3155 EM_error3( "[chuck](via FileIO): write error: invalid ASCII/binary flag" );
3113// }3156 }
3114//3157
3115// if (m_io.fail()) { // check both before and after write if stream is ok3158 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" );3159 EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );
3117// }3160 }
3118//}3161}
3119//3162
3120//3163
3121//3164
3122//3165
3123////-----------------------------------------------------------------------------3166//-----------------------------------------------------------------------------
3124//// name: write( t_CKINT val )3167// name: write( t_CKINT val )
3125//// desc: ...3168// desc: ...
3126////-----------------------------------------------------------------------------3169//-----------------------------------------------------------------------------
3127//void Chuck_IO_File::write( t_CKINT val, t_CKINT flags )3170void Chuck_IO_File::write( t_CKINT val, t_CKINT flags )
3128//{3171{
3129// // sanity3172 // sanity
3130// if (!(m_io.is_open())) {3173 if (!(m_io.is_open())) {
3131// EM_error3( "[chuck](via FileIO): cannot write: no file open" );3174 EM_error3( "[chuck](via FileIO): cannot write: no file open" );
3132// return;3175 return;
3133// }3176 }
3134//3177
3135// if (m_io.fail()) {3178 if (m_io.fail()) {
3136// EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );3179 EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );
3137// return;3180 return;
3138// }3181 }
3139//3182
3140// if ( m_dir )3183 if ( m_dir )
3141// {3184 {
3142// EM_error3( "[chuck](via FileIO): cannot write on directory" );3185 EM_error3( "[chuck](via FileIO): cannot write on directory" );
3143// return;3186 return;
3144// }3187 }
3145//3188
3146// if (m_flags & TYPE_ASCII) {3189 if (m_flags & TYPE_ASCII) {
3147// m_io << val;3190 m_io << val;
3148// } else if (m_flags & TYPE_BINARY) {3191 } else if (m_flags & TYPE_BINARY) {
3149// int nBytes = 4;3192 int nBytes = 4;
3150// if(flags & INT8) nBytes = 1;3193 if(flags & INT8) nBytes = 1;
3151// else if(flags & INT16) nBytes = 2;3194 else if(flags & INT16) nBytes = 2;
3152// else if(flags & INT32) nBytes = 4;3195 else if(flags & INT32) nBytes = 4;
3153// m_io.write( (char *)&val, nBytes );3196 m_io.write( (char *)&val, nBytes );
3154// } else {3197 } else {
3155// EM_error3( "[chuck](via FileIO): write error: invalid ASCII/binary flag" );3198 EM_error3( "[chuck](via FileIO): write error: invalid ASCII/binary flag" );
3156// }3199 }
3157//3200
3158// if (m_io.fail()) { // check both before and after write if stream is ok3201 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" );3202 EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );
3160// }3203 }
3161//}3204}
3162//3205
3163//3206
3164//3207
3165//3208
3166////-----------------------------------------------------------------------------3209//-----------------------------------------------------------------------------
3167//// name: write( t_CKFLOAT val )3210// name: write( t_CKFLOAT val )
3168//// desc: ...3211// desc: ...
3169////-----------------------------------------------------------------------------3212//-----------------------------------------------------------------------------
3170//void Chuck_IO_File::write( t_CKFLOAT val )3213void Chuck_IO_File::write( t_CKFLOAT val )
3171//{3214{
3172// // sanity3215 // sanity
3173// if (!(m_io.is_open())) {3216 if (!(m_io.is_open())) {
3174// EM_error3( "[chuck](via FileIO): cannot write: no file open" );3217 EM_error3( "[chuck](via FileIO): cannot write: no file open" );
3175// return;3218 return;
3176// }3219 }
3177//3220
3178// if (m_io.fail()) {3221 if (m_io.fail()) {
3179// EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );3222 EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );
3180// return;3223 return;
3181// }3224 }
3182//3225
3183// if ( m_dir )3226 if ( m_dir )
3184// {3227 {
3185// EM_error3( "[chuck](via FileIO): cannot write to a directory" );3228 EM_error3( "[chuck](via FileIO): cannot write to a directory" );
3186// return;3229 return;
3187// }3230 }
3188//3231
3189// if (m_flags & TYPE_ASCII) {3232 if (m_flags & TYPE_ASCII) {
3190// m_io << val;3233 m_io << val;
3191// } else if (m_flags & TYPE_BINARY) {3234 } else if (m_flags & TYPE_BINARY) {
3192// m_io.write( (char *)&val, sizeof(t_CKFLOAT) );3235 m_io.write( (char *)&val, sizeof(t_CKFLOAT) );
3193// } else {3236 } else {
3194// EM_error3( "[chuck](via FileIO): write error: invalid ASCII/binary flag" );3237 EM_error3( "[chuck](via FileIO): write error: invalid ASCII/binary flag" );
3195// }3238 }
3196//3239
3197// if (m_io.fail()) { // check both before and after write if stream is ok3240 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" );3241 EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );
3199// }3242 }
3200//}3243}
3201//3244
3202//3245
3203//3246
3204//3247
3205//// static helper functions for writing asynchronously3248// static helper functions for writing asynchronously
3206//THREAD_RETURN ( THREAD_TYPE Chuck_IO_File::writeStr_thread ) ( void *data )3249THREAD_RETURN ( THREAD_TYPE Chuck_IO_File::writeStr_thread ) ( void *data )
3207//{3250{
3208// async_args *args = (async_args *)data;3251 async_args *args = (async_args *)data;
3209// args->fileio_obj->write ( args->stringArg );3252 args->fileio_obj->write ( args->stringArg );
3210// Chuck_Event *e = args->fileio_obj->m_asyncEvent;3253 Chuck_Event *e = args->fileio_obj->m_asyncEvent;
3211// delete args;3254 delete args;
3212// e->broadcast(); // wake up3255 e->broadcast(); // wake up
3213// e->broadcast_global();3256 e->broadcast_global();
3214//3257
3215// return (THREAD_RETURN)0;3258 return (THREAD_RETURN)0;
3216//}3259}
3217//3260
3218//THREAD_RETURN ( THREAD_TYPE Chuck_IO_File::writeInt_thread ) ( void *data )3261THREAD_RETURN ( THREAD_TYPE Chuck_IO_File::writeInt_thread ) ( void *data )
3219//{3262{
3220// async_args *args = (async_args *)data;3263 async_args *args = (async_args *)data;
3221// args->fileio_obj->write ( args->intArg );3264 args->fileio_obj->write ( args->intArg );
3222// args->fileio_obj->m_asyncEvent->broadcast(); // wake up3265 args->fileio_obj->m_asyncEvent->broadcast(); // wake up
3223// args->fileio_obj->m_asyncEvent->broadcast_global();3266 args->fileio_obj->m_asyncEvent->broadcast_global();
3224// delete args;3267 delete args;
3225//3268
3226// return (THREAD_RETURN)0;3269 return (THREAD_RETURN)0;
3227//}3270}
3228//3271
3229//THREAD_RETURN ( THREAD_TYPE Chuck_IO_File::writeFloat_thread ) ( void *data )3272THREAD_RETURN ( THREAD_TYPE Chuck_IO_File::writeFloat_thread ) ( void *data )
3230//{3273{
3231// async_args *args = (async_args *)data;3274 async_args *args = (async_args *)data;
3232// args->fileio_obj->write ( args->floatArg );3275 args->fileio_obj->write ( args->floatArg );
3233// args->fileio_obj->m_asyncEvent->broadcast(); // wake up3276 args->fileio_obj->m_asyncEvent->broadcast(); // wake up
3234// args->fileio_obj->m_asyncEvent->broadcast_global();3277 args->fileio_obj->m_asyncEvent->broadcast_global();
3235// delete args;3278 delete args;
3236//3279
3237// return (THREAD_RETURN)0;3280 return (THREAD_RETURN)0;
3238//}3281}
3282#endif // __DISABLE_FILEIO__
3283
3284
3285
3239 3286
3240Chuck_IO_Chout::Chuck_IO_Chout( Chuck_Carrier * carrier ) {3287Chuck_IO_Chout::Chuck_IO_Chout( Chuck_Carrier * carrier ) {
3241 // store3288 // store

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t