]> icculus.org git repositories - taylor/freespace2.git/blob - src/globalincs/version.cpp
use proper FS2 demo pilot files
[taylor/freespace2.git] / src / globalincs / version.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/GlobalIncs/version.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  *
16  * $Log$
17  * Revision 1.3  2002/06/09 04:41:17  relnev
18  * added copyright header
19  *
20  * Revision 1.2  2002/05/07 03:16:45  theoddone33
21  * The Great Newline Fix
22  *
23  * Revision 1.1.1.1  2002/05/03 03:28:09  root
24  * Initial import.
25  *
26  * 
27  * 4     8/09/99 2:21p Andsager
28  * Fix patching from multiplayer direct to launcher update tab.
29  * 
30  * 3     8/06/99 3:32p Andsager
31  * Handle update when no registry is set
32  * 
33  * 2     5/19/99 4:07p Dave
34  * Moved versioning code into a nice isolated common place. Fixed up
35  * updating code on the pxo screen. Fixed several stub problems.
36  * 
37  * 1     5/18/99 4:28p Dave
38  * 
39  * $NoKeywords: $
40  */
41
42 #include <stdio.h>
43 #include <string.h>
44 #include "version.h"
45 #include "osregistry.h"
46 #include "pstypes.h"
47
48 // ----------------------------------------------------------------------------------------------------------------
49 // VERSION DEFINES/VARS
50 //
51
52 // Defines
53 #define VER(major, minor, build) (100*100*major+100*minor+build)
54 #define MAX_LINE_LENGTH 512
55
56
57 // ----------------------------------------------------------------------------------------------------------------
58 // VERSION FUNCTIONS
59 //
60
61 // compare version against the passed version file
62 // returns -1 on error 
63 // 0 if we are an earlier version
64 // 1 if same version
65 // 2 if higher version
66 // fills in user version and latest version values if non-NULL
67 int version_compare(const char *filename, int *u_major, int *u_minor, int *u_build, int *l_major, int *l_minor, int *l_build)
68 {       
69         int usr_major, usr_minor, usr_build;
70         int latest_major, latest_minor, latest_build;
71
72         // open file and try backup, if needed
73         FILE *f = fopen(filename, "rt");
74         if (f == NULL) {
75                 return -1;              
76         }
77
78         // grab the last line in file which isn't empty and isn't a comment
79         char buffer[MAX_LINE_LENGTH+1], verbuffer[MAX_LINE_LENGTH+1];
80
81         SDL_strlcpy(verbuffer, "", SDL_arraysize(verbuffer));
82         SDL_strlcpy(buffer, "", SDL_arraysize(buffer));
83         while ( !feof(f) ) {
84                 // Read the line into a temporary buffer
85                 if ( fgets(buffer, MAX_LINE_LENGTH, f) == NULL ) {
86                         break;
87                 }
88
89                 // take the \n off the end of it
90                 if (strlen(buffer)>0 && buffer[strlen(buffer) - 1] == '\n')
91                         buffer[strlen(buffer) - 1] = 0;
92
93                 // If the line is empty, go get another one
94                 if (strlen(buffer) == 0) continue;
95
96                 // If the line is a comment, go get another one
97                 if (buffer[0] == VERSION_FILE_COMMENT_CHAR) continue;
98
99                 // Line is a good one, so save it...
100                 SDL_strlcpy(verbuffer, buffer, SDL_arraysize(verbuffer));
101         }
102         fclose(f);
103
104         // Make sure a version line was found
105         if (strlen(verbuffer) == 0) {
106                 // MessageBox(XSTR("Couldn't parse Version file!", 1205), XSTR("Error!", 1185), MB_OK|MB_ICONERROR);
107                 return -1;
108         }
109
110         // Get the most up to date Version number
111         latest_major = 0;
112         latest_minor = 0;
113         latest_build = 0;
114
115         if (sscanf(verbuffer, "%i %i %i", &latest_major, &latest_minor, &latest_build) != 3) {
116                 // MessageBox(XSTR("Couldn't parse Version file!", 1205), XSTR("Error!", 1185), MB_OK|MB_ICONERROR);
117                 return -1;
118         }
119
120         // retrieve the user's current version
121         usr_major = os_config_read_uint("Version", "Major", 0);
122         usr_minor = os_config_read_uint("Version", "Minor", 0);
123         usr_build = os_config_read_uint("Version", "Build", 0);
124         
125         // Make sure the user's Version was found!
126         if ( VER(usr_major, usr_minor, usr_build) == 0 ) {
127                 // MessageBox(XSTR("The Freespace 2 Auto-Update program could not find your current game Version in the system registry.\n\nThis should be corrected by starting up the game, exiting the game, and then running the Auto-Update program.", 1206), XSTR("Unable to Determine User's Version", 1207), MB_OK|MB_ICONERROR);
128                 return NO_VERSION_IN_REGISTRY;
129         }       
130
131         // stuff outgoing values
132         if(u_major != NULL){
133                 *u_major = usr_major;
134         }
135         if(u_minor != NULL){
136                 *u_minor = usr_minor;
137         }
138         if(u_build != NULL){
139                 *u_build = usr_build;
140         }
141         if(l_major != NULL){
142                 *l_major = latest_major;
143         }
144         if(l_minor != NULL){
145                 *l_minor = latest_minor;
146         }
147         if(l_build != NULL){
148                 *l_build = latest_build;
149         }
150
151         // check to see if the user's version is up to date
152         if (VER(usr_major, usr_minor, usr_build) < VER(latest_major, latest_minor, latest_build)) {             
153                 return 0;
154         }
155
156         // same version
157         return 1;
158 }
159