1 /********************************************************************************
2 * *
3 * M e m o r y M a p T e s t *
4 * *
5 *********************************************************************************
6 * Copyright (C) 2004,2006 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * $Id: memmap.cpp,v 1.5 2006/01/22 17:59:02 fox Exp $ *
9 ********************************************************************************/
10 #include "fx.h"
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include "FXMemMap.h"
15
16 /*
17 Notes:
18
19 - Test FXMemMap capabilities.
20
21 */
22
23 /*******************************************************************************/
24
25 enum ACTION {
26 TEST_NONE,
27 TEST_READ,
28 TEST_WRITE
29 };
30
31
printusage()32 void printusage(){
33 fprintf(stderr,"Usage: \n\n");
34 fprintf(stderr," memmap read <length> <filename>\n\n");
35 fprintf(stderr," or\n\n");
36 fprintf(stderr," memmap write <length> <filename>\n");
37 }
38
39
40 // Start the whole thing
main(int argc,char ** argv)41 int main(int argc,char** argv){
42 FXString filename=FXString::null;
43 ACTION action=TEST_READ;
44 long index;
45 long length=0;
46 long offset=0;
47 char* base;
48
49 // What to test
50 FXMemMap map;
51
52 // Show how to use
53 if(argc<4){
54 printusage();
55 exit(0);
56 }
57
58 // Get action
59 if(strcmp(argv[1],"read")==0)
60 action=TEST_READ;
61 else if(strcmp(argv[1],"write")==0)
62 action=TEST_WRITE;
63 else{
64 printusage();
65 exit(0);
66 }
67
68 // Get size
69 if(sscanf(argv[2],"%ld",&length)<1){
70 printusage();
71 exit(0);
72 }
73
74 // Get filename
75 filename=argv[3];
76
77 // Show process id
78 fprintf(stderr,"Process id=%d\n",fxgetpid());
79
80 // Test reading
81 if(action==TEST_READ){
82
83 // Wait for return
84 fprintf(stderr,"Press return to map:");
85 getchar();
86
87 // Map
88 base=(char*)map.mapFile(filename,offset,length,FXMemMap::READ,FXMemMap::SHAR);
89 if(base==NULL){
90 fprintf(stderr,"mapFile returned NULL\n");
91 exit(1);
92 }
93
94 fprintf(stderr,"actual length = %ld\n",map.length());
95
96 // Touch it
97 for(index=0; index<map.length(); index++){
98 if(base[index]!='Z') fprintf(stderr,"base[%ld]=%d\n",index,base[index]);
99 }
100
101 // Wait for return
102 fprintf(stderr,"Press return to unmap:");
103 getchar();
104
105 // Unmap
106 map.unmap();
107
108 // Wait for return
109 fprintf(stderr,"Press return to quit:");
110 getchar();
111 }
112
113 // Test writing
114 else if(action==TEST_WRITE){
115
116 // Wait for return
117 fprintf(stderr,"Press return to map:");
118 getchar();
119
120 // Map
121 base=(char*)map.mapFile(filename,offset,length,FXMemMap::READ|FXMemMap::WRITE|FXMemMap::TRUNC,FXMemMap::SHAR);
122 if(base==NULL){
123 fprintf(stderr,"mapFile returned NULL\n");
124 exit(1);
125 }
126
127 fprintf(stderr,"actual length = %ld\n",map.length());
128
129 // Write it
130 for(index=0; index<map.length(); index++){
131 base[index]='Z';
132 }
133
134 // Wait for return
135 fprintf(stderr,"Press return to unmap:");
136 getchar();
137
138 // Unmap
139 map.unmap();
140
141 // Wait for return
142 fprintf(stderr,"Press return to quit:");
143 getchar();
144 }
145
146 return 1;
147 }
148
149
150