// $Id: Mem_Map_Test.cpp 80826 2008-03-04 14:51:23Z wotte $ // ============================================================================ // // = LIBRARY // tests // // = FILENAME // Mem_Map_Test.cpp // // = DESCRIPTION // This test illustrates the use of ACE_Mem_Map to reverse a // file. The test first creates a dummy file for testing, then // reverses the file and then reverses it again to get back the // original file. // // = AUTHOR // Prashant Jain // // ============================================================================ #include "test_config.h" #include "ace/Mem_Map.h" #include "ace/ACE.h" #include "ace/OS_NS_string.h" #include "ace/OS_NS_unistd.h" #include "ace/OS_NS_fcntl.h" #include "ace/OS_Memory.h" ACE_RCSID(tests, Mem_Map_Test, "Mem_Map_Test.cpp,v 4.36 2003/11/01 11:15:25 dhinton Exp") #if !defined (ACE_LACKS_MMAP) static const char ACE_ALPHABET[] = "abcdefghijklmnopqrstuvwxyz"; static const int LINE_LENGTH = 10; static const int NUM_LINES = 15; static void reverse_file (ACE_HANDLE file_handle, char *array, size_t size) { int count = 0; // LynxOS 3.0.0/PowerPC needs the volatile qualifier, with -O2 // optimization enabled and without ACE_HAS_INLINE. volatile size_t i = size; --i; if (array[i] == '/0') array[i] = '/n'; while (i-- > 0) { if (array[i] == '/n') { ACE_OS::write (file_handle, array + i + 1, count); ACE_OS::write (file_handle, ACE_TEXT ("/n"), 1); count = 0; } else count++; } ACE_OS::write (file_handle, array, count+1); } static int create_test_file (ACE_TCHAR *filename, int line_length, int num_lines) { char *mybuf = 0; ACE_NEW_RETURN (mybuf, char[line_length + 1], -1); const char *c = ACE_ALPHABET; const char *d = c; #if defined (__QNXNTO__) || (defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x640)) // For NTO has to applied to open the file, as Mem_Map can map only shared memory ACE_Mem_Map mmap_4_open; mmap_4_open.open (filename, O_RDWR | O_CREAT | O_TRUNC, ACE_DEFAULT_FILE_PERMS); ACE_HANDLE file_handle = mmap_4_open.handle(); #else ACE_HANDLE file_handle = ACE_OS::open (filename, O_RDWR | O_CREAT | O_TRUNC, ACE_DEFAULT_FILE_PERMS); #endif if (file_handle == ACE_INVALID_HANDLE) { delete [] mybuf; ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("Open failed for %s/n"), filename), -1); } for (int j = 0; j < num_lines; j++) { for (int i = 0; i < line_length; i++) { mybuf[i] = *c; c++; } mybuf[line_length] = '/0'; c = ++d; if (ACE_OS::write (file_handle, mybuf, line_length) != line_length) { delete [] mybuf; ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p (%d)
测试语法高亮
最新推荐文章于 2025-12-31 16:07:14 发布
1316

被折叠的 条评论
为什么被折叠?



