Java to C++

Hello there

Can someone assist to translate the following code snippets to C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
private void checkFile(String userid)
*** *** *** throws RecoverableConnectionException {
*** *** // Connecting to the peer 
*** *** // Check if the sec file exist and it hasn't been changed by the external program.
*** *** if (!new File(SEC_FILE).exists()) {
*** *** *** if (cat.isDebugEnabled()) {
*** *** *** *** cat.debug("Security File exists: "
*** *** *** *** *** *** + new File(SEC_FILE).exists()
*** *** *** *** *** *** + " - logging in");
*** *** *** }
*** *** *** cat.debug("Starting login process for: " + userid);
*** *** *** logon();
*** *** *** cat.debug("Logon complete for: " + userid);
*** *** } else {
*** *** *** if (cat.isDebugEnabled()) {
*** *** *** *** cat.debug("Security File exists: "
*** *** *** *** *** *** + new File(SEC_FILE).exists()
*** *** *** *** *** *** + " - connecting to server");
*** *** *** }

*** *** *** // check checksum
*** *** *** md5sumCheck();

*** *** *** cat.debug("Starting connection for: " + userid);
*** *** *** connect();
*** *** *** cat.debug("Connection complete for: " + userid);
*** *** }
*** }


And file handling like this;

1
2
3
4
5
6
7
8
9
10
 // Set userid
 userid = userID.value;

 // Set securitykey
 securitykey = secKey.value;

 // Store securitykey
 printWriter = new java.io.PrintWriter(new java.io.FileWriter(SEC_KEY, false), true);
 printWriter.println(userid);
 printWriter.println(securitykey);


Kindly advice
Sam
What's with all the crazy asterisks?
my bad. Here it is.
private void checkFile(String userid)
throws RecoverableConnectionException {
// Connecting to the peer
// Check if the sec file exist and it hasn't been changed by the external program.
if (!new File(SEC_FILE).exists()) {
if (cat.isDebugEnabled()) {
cat.debug("Security File exists: "
+ new File(SEC_FILE).exists()
+ " - logging in");
}
cat.debug("Starting login process for: " + userid);
logon();
cat.debug("Logon complete for: " + userid);
} else {
if (cat.isDebugEnabled()) {
cat.debug("Security File exists: "
+ new File(SEC_FILE).exists()
+ " - connecting to server");
}

// check checksum
md5sumCheck();

cat.debug("Starting connection for: " + userid);
connect();
cat.debug("Connection complete for: " + userid);
}
}
Something like this maybe. But it really depends a lot on everything else. Its difficult to just convert small code sections like this because the languages are quite different.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <fstream>
#include <iostream>

struct logger
{
	bool isDebugEnabled() { return true; }
	void debug(const std::string& msg)
	{
		std::cout << msg << std::endl;
	}
} cat;

const char* SEC_FILE = "input.txt";

void logon() {}

void md5sumCheck() {}

void connect() {}

void checkFile(const std::string& userid)
{
	// Connecting to the peer
	// Check if the sec file exist and it hasn't been changed by the external program.

	if(std::ifstream(SEC_FILE).is_open())
	{
		if (cat.isDebugEnabled())
		{
			cat.debug("Security File exists: true - logging in");
		}
		cat.debug("Starting login process for: " + userid);
		logon();
		cat.debug("Logon complete for: " + userid);
	}
	else
	{
		if (cat.isDebugEnabled())
		{
			cat.debug("Security File exists: false - connecting to server");
		}

		// check checksum
		md5sumCheck();

		cat.debug("Starting connection for: " + userid);
		connect();
		cat.debug("Connection complete for: " + userid);
	}
}
Topic archived. No new replies allowed.