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