1 /* $Id: RCFile.cpp,v 1.13 2003/07/28 16:55:04 nan Exp $ */
2 
3 // Copyright (C) 2001, 2003  ���� �ȹ�(Kanna Yoshihiro)
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 
19 #include "ttinc.h"
20 #include "RCFile.h"
21 
22 RCFile* RCFile::m_rcFile = NULL;
23 
RCFile()24 RCFile::RCFile() {
25   isTexture = true;
26   fullScreen = false;
27   gmode = GMODE_FULL;
28   myModel = MODEL_TRANSPARENT;
29   gameLevel = LEVEL_EASY;
30   gameMode = GAME_21PTS;
31   sndMode = SOUND_SDL;
32 
33   protocol = IPv4;
34 
35   serverName[0] = '\0';
36   nickname[0] = '\0';
37   message[0] = '\0';
38 
39   csmash_port = CSMASH_PORT;
40 
41 }
42 
43 RCFile*
GetRCFile()44 RCFile::GetRCFile() {
45   if ( !RCFile::m_rcFile )
46     RCFile::m_rcFile = new RCFile();
47 
48   return m_rcFile;
49 }
50 
51 bool
ReadRCFile()52 RCFile::ReadRCFile() {
53   FILE *fp = OpenRCFile( "r" );
54   char buf[1024];
55   char *p;
56 
57   while ( fgets( buf, 1024, fp ) ) {
58     if ( strncmp( buf, "fullscreen=", 11 ) == 0 ) {
59       if ( buf[11] == '1' )
60 	fullScreen = true;
61       else
62 	fullScreen = false;
63     } else if ( strncmp( buf, "graphics=", 9 ) == 0 ) {
64       if ( strncmp( &buf[9], "simple", 6 ) == 0 ) {
65 	gmode = GMODE_SIMPLE;
66 	isTexture = false;
67       } else if ( strncmp( &buf[9], "2D", 2 ) == 0 ) {
68 	gmode = GMODE_2D;
69       }
70     } else if ( strncmp( buf, "server=", 7 ) == 0 ) {
71       strncpy( serverName , &buf[7], 256 );
72       if ( (p = strchr( serverName, '\r' )) )
73 	*p = '\0';
74       else if ( (p = strchr( serverName, '\n' )) )
75 	*p = '\0';
76     } else if ( strncmp( buf, "nickname=", 9 ) == 0 ) {
77       strncpy( nickname , &buf[9], 32 );
78       if ( (p = strchr( nickname, '\r' )) )
79 	*p = '\0';
80       else if ( (p = strchr( nickname, '\n' )) )
81 	*p = '\0';
82     } else if ( strncmp( buf, "message=", 8 ) == 0 ) {
83       strncpy( message, &buf[8], 64 );
84       if ( (p = strchr( message, '\r' )) )
85 	*p = '\0';
86       else if ( (p = strchr( message, '\n' )) )
87 	*p = '\0';
88     } else if ( strncmp( buf, "mymodel=", 8 ) == 0 ) {
89       myModel = buf[8]-'0';
90       if ( myModel > MODEL_ARMONLY || myModel < MODEL_TRANSPARENT )
91 	myModel = MODEL_TRANSPARENT;
92     } else if ( strncmp( buf, "gamelevel=", 10 ) == 0 ) {
93       gameLevel = buf[10]-'0';
94       if ( gameLevel > LEVEL_TSUBORISH || gameLevel < LEVEL_EASY )
95 	gameLevel = LEVEL_EASY;
96     } else if ( strncmp( buf, "gamemode=", 9 ) == 0 ) {
97       gameMode = buf[9]-'0';
98       if ( gameMode > GAME_21PTS || gameMode < GAME_5PTS )
99 	gameMode = GAME_21PTS;
100     } else if ( strncmp( buf, "soundmode=", 10 ) == 0 ) {
101       sndMode = buf[10]-'0';
102       if ( sndMode > SOUND_SDL || sndMode < SOUND_NONE )
103 	sndMode = SOUND_NONE;
104     } else if ( strncmp( buf, "protocol=", 9 ) == 0 ) {
105       protocol = buf[9]-'0';
106       if ( protocol > IPv6 || protocol < IPv4 )
107 	protocol = IPv4;
108     }
109   }
110 
111   fclose( fp );
112 
113   return true;
114 }
115 
116 bool
WriteRCFile()117 RCFile::WriteRCFile() {
118   FILE *fp = OpenRCFile( "w" );
119 
120   fprintf( fp, "fullscreen=%d\n", fullScreen ? 1 : 0 );
121 
122   fprintf( fp, "graphics=" );
123   if ( gmode == GMODE_SIMPLE )
124     fprintf( fp, "simple\n" );
125   else if ( gmode == GMODE_2D )
126     fprintf( fp, "2D\n" );
127   else
128     fprintf( fp, "full\n" );
129 
130   fprintf( fp, "server=%s\n", serverName );
131   fprintf( fp, "nickname=%s\n", nickname );
132   fprintf( fp, "message=%s\n", message );
133 
134   fprintf( fp, "mymodel=%d\n", myModel );
135   fprintf( fp, "gamelevel=%d\n", gameLevel );
136   fprintf( fp, "gamemode=%d\n", gameMode );
137   fprintf( fp, "soundmode=%d\n", sndMode );
138   fprintf( fp, "protocol=%d\n", protocol );
139 
140   fclose( fp );
141 
142   return true;
143 }
144 
145 FILE *
OpenRCFile(char * mode)146 RCFile::OpenRCFile( char *mode ) {
147   char *csmashrc;
148 
149   csmashrc = getenv( "CSMASH_RC" );
150 
151   if ( csmashrc == NULL ) {
152     if ( getenv( "HOME" ) == NULL ) {
153 #ifdef WIN32
154       csmashrc = new char[256+12];
155       GetWindowsDirectory( csmashrc, 256 );
156       strcat( csmashrc, "\\csmash.ini" );
157 #else
158       fprintf( stderr, _("No home directory.\n") );
159       exit(1);
160 #endif
161     } else {
162 #ifdef WIN32
163       csmashrc = new char[strlen(getenv("HOME"))+12];
164       sprintf( csmashrc, "%s\\csmash.ini", getenv("HOME") );
165 #else
166       csmashrc = new char[strlen(getenv("HOME"))+12];
167       sprintf( csmashrc, "%s/.csmashrc", getenv("HOME") );
168 #endif
169     }
170   }
171 
172   FILE *fp = fopen( csmashrc, mode );
173   if ( fp == NULL ) {
174     fp = fopen( csmashrc, "w" );	// touch
175   }
176 
177   if ( fp == NULL ) {
178     fprintf( stderr, _("Cannot open rc file %s.\n"), csmashrc );
179     exit(1);
180   }
181 
182   return fp;
183 }
184