]> icculus.org git repositories - taylor/freespace2.git/blob - src/globalincs/version.cpp
Initial revision
[taylor/freespace2.git] / src / globalincs / version.cpp
1 /*
2  * $Logfile: /Freespace2/code/GlobalIncs/version.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  *
8  * $Log$
9  * Revision 1.1  2002/05/03 03:28:09  root
10  * Initial revision
11  *
12  * 
13  * 4     8/09/99 2:21p Andsager
14  * Fix patching from multiplayer direct to launcher update tab.
15  * 
16  * 3     8/06/99 3:32p Andsager
17  * Handle update when no registry is set
18  * 
19  * 2     5/19/99 4:07p Dave
20  * Moved versioning code into a nice isolated common place. Fixed up
21  * updating code on the pxo screen. Fixed several stub problems.
22  * 
23  * 1     5/18/99 4:28p Dave
24  * 
25  * $NoKeywords: $
26  */
27
28 #include <stdio.h>
29 #include <string.h>
30 #include "version.h"
31 #include "osregistry.h"
32
33 // ----------------------------------------------------------------------------------------------------------------
34 // VERSION DEFINES/VARS
35 //
36
37 // Defines
38 #define VER(major, minor, build) (100*100*major+100*minor+build)
39 #define MAX_LINE_LENGTH 512
40
41
42 // ----------------------------------------------------------------------------------------------------------------
43 // VERSION FUNCTIONS
44 //
45
46 // compare version against the passed version file
47 // returns -1 on error 
48 // 0 if we are an earlier version
49 // 1 if same version
50 // 2 if higher version
51 // fills in user version and latest version values if non-NULL
52 int version_compare(char *filename, int *u_major, int *u_minor, int *u_build, int *l_major, int *l_minor, int *l_build)
53 {       
54         int usr_major, usr_minor, usr_build;
55         int latest_major, latest_minor, latest_build;
56
57         // open file and try backup, if needed
58         FILE *f = fopen(filename, "rt");
59         if (f == NULL) {
60                 return -1;              
61         }
62
63         // grab the last line in file which isn't empty and isn't a comment
64         char buffer[MAX_LINE_LENGTH+1], verbuffer[MAX_LINE_LENGTH+1];
65
66         strcpy(verbuffer,"");
67         strcpy(buffer,"");
68         while ( !feof(f) ) {
69                 // Read the line into a temporary buffer
70                 fgets(buffer, MAX_LINE_LENGTH, f);
71
72                 // take the \n off the end of it
73                 if (strlen(buffer)>0 && buffer[strlen(buffer) - 1] == '\n')
74                         buffer[strlen(buffer) - 1] = 0;
75
76                 // If the line is empty, go get another one
77                 if (strlen(buffer) == 0) continue;
78
79                 // If the line is a comment, go get another one
80                 if (buffer[0] == VERSION_FILE_COMMENT_CHAR) continue;
81
82                 // Line is a good one, so save it...
83                 strcpy(verbuffer, buffer);
84         }
85         fclose(f);
86
87         // Make sure a version line was found
88         if (strlen(verbuffer) == 0) {
89                 // MessageBox(XSTR("Couldn't parse Version file!", 1205), XSTR("Error!", 1185), MB_OK|MB_ICONERROR);
90                 return -1;
91         }
92
93         // Get the most up to date Version number
94         latest_major = 0;
95         latest_minor = 0;
96         latest_build = 0;
97
98         if (sscanf(verbuffer, "%i %i %i", &latest_major, &latest_minor, &latest_build) != 3) {
99                 // MessageBox(XSTR("Couldn't parse Version file!", 1205), XSTR("Error!", 1185), MB_OK|MB_ICONERROR);
100                 return -1;
101         }
102
103         // retrieve the user's current version
104         usr_major = os_config_read_uint("Version", "Major", 0);
105         usr_minor = os_config_read_uint("Version", "Minor", 0);
106         usr_build = os_config_read_uint("Version", "Build", 0);
107         
108         // Make sure the user's Version was found!
109         if ( VER(usr_major, usr_minor, usr_build) == 0 ) {
110                 // 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);
111                 return NO_VERSION_IN_REGISTRY;
112         }       
113
114         // stuff outgoing values
115         if(u_major != NULL){
116                 *u_major = usr_major;
117         }
118         if(u_minor != NULL){
119                 *u_minor = usr_minor;
120         }
121         if(u_build != NULL){
122                 *u_build = usr_build;
123         }
124         if(l_major != NULL){
125                 *l_major = latest_major;
126         }
127         if(l_minor != NULL){
128                 *l_minor = latest_minor;
129         }
130         if(l_build != NULL){
131                 *l_build = latest_build;
132         }
133
134         // check to see if the user's version is up to date
135         if (VER(usr_major, usr_minor, usr_build) < VER(latest_major, latest_minor, latest_build)) {             
136                 return 0;
137         }
138
139         // same version
140         return 1;
141 }