Convert Java to FWH 2011.08

Convert Java to FWH 2011.08

Postby cdmmaui » Sun Apr 22, 2012 4:54 pm

Hello,

I have two code snipets that I need converted to FWH, can someone help convert for me? I am happy to pay someone for their time. Please send me an e-mail at Darrell.Ortiz AT cdmsoft.com if you are interested.

Thank you,

// CODE 1-----------------------------------------------------------------------------
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.util.Date;

public class TcpClientUtil
{

private static final int BUFSIZE = 50;

Socket socket = null;
OutputStream out = null;
InputStream in = null;
BufferedOutputStream bufferedOutputStream=null;
BufferedInputStream bufferedInputStream =null;


String hostServer = "192.168.1.107"; //149.99.39.146
int hostPort = 9100;//50030
int socketTimeout = 10000;

boolean connected = false;

FileWriter s;


Charset charset = Charset.forName("ISO-8859-1");
CharsetDecoder decoder = charset.newDecoder();
CharsetEncoder encoder = charset.newEncoder();

public TcpClientUtil() {

}

public TcpClientUtil(String HostServer, int HostPort, int SocketTimeout){
hostServer = HostServer;
hostPort = HostPort;
socketTimeout = SocketTimeout;

}

public FileWriter getLogger() {
return s;
}

public void setLogger(FileWriter s) {
this.s = s;
}


public boolean ConnectSocket() {
//Create socket connection
try
{
if(socket == null)
{
socket = new Socket();
socket.connect(new InetSocketAddress(hostServer, hostPort), socketTimeout);


connected = true;
bufferedOutputStream = new BufferedOutputStream(socket.getOutputStream());
bufferedInputStream = new BufferedInputStream(socket.getInputStream());

}
}
catch (UnknownHostException e)
{
System.out.println("Unknown host: kq6py.eng");
//System.exit(1);
connected = false;

}
catch (IOException e)
{
System.out.println("No I/O");
// System.exit(1);
connected = false;

}

return connected;

}


private boolean DisconnectSocket(){
try
{
if(socket!=null)
{
bufferedInputStream.close();
socket.close();
connected = false;

}
}
catch (IOException e)
{
System.out.println("Could not close socket");
System.exit(-1);
connected = false;

}

return connected;
}


public String SendRequest(String dataPacket){

String result = "";
byte[] bsRequest = null;
byte[] bsResponse = new byte[BUFSIZE];
int rcvMsgSize;

try{


//Encode request
System.out.println(new Date().toString() + " : Start Encode Request.") ;
//s.write(new Date().toString() + " : Start Encode Request.\r\n");
bsRequest = dataPacket.getBytes();
System.out.println(new Date().toString() + " : Stop Encode Request.") ;
//s.write(new Date().toString() + " : Stop Encode Request.\r\n");


//Send Request
if(bsRequest != null){

System.out.println(new Date().toString() + " : Start Writing Request to socket.") ;
//s.write(new Date().toString() + " : Start Writing Request to socket.\r\n");

for (int u = 0; u < bsRequest.length; u++) {
try {
//out.write(bsRequest[u]);
bufferedOutputStream.write(bsRequest[u]);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
bufferedOutputStream.flush();
System.out.println("Sent data packet to printer");


boolean disconnected = DisconnectSocket();
System.out.println("Disconnected from printer");


}


}catch(Exception ex){

result = "ERROR : " + ex.getMessage();

}
return result;
}

private byte[] getDataPacket(String data){

byte[] b = null;

try{

String dataPacket = FileUtil.getContentsAsString("D:\\Argo Wireless\\Code\\sourcetag\\pocs\\TagPrintingPOC\\labels\\from Vijay\\Test.zpl");

b = dataPacket.getBytes();

}catch(Exception ex){

System.out.println(ex.getMessage());
}

return b;



}

private byte[] concat(byte[] bs1, byte[] bs2)
{
byte[] returnArray = new byte[bs1.length + bs2.length];
System.arraycopy(bs1, 0, returnArray, 0, bs1.length);
System.arraycopy(bs2, 0, returnArray, bs1.length, bs2.length);
return returnArray;
}

private String DecodeDataPacket(byte[] bs){

String result = null;
try{
result = new String(bs,"US-ASCII");
}catch(Exception ex){
result = "ERROR : " + ex.getMessage();
}
return result;
}
}

// CODE 2-----------------------------------------------------------------------------
private boolean printTags(String dataPacket, String hostName){

boolean result = false;

try{

//Connect to Server
TcpClientUtil printer = new TcpClientUtil(hostName,
Integer.parseInt(printerport),
Integer.parseInt(printerconnectiontimeout));

boolean connected = printer.ConnectSocket();

if(connected == true){
logger.info(new Date().toString() + " : Connected To Printer.\r\n");

String response = printer.SendRequest(dataPacket);

return true;


}

}catch(Exception ex){

logger.info(new Date().toString() + " : " + ex.getMessage());
return false;

}

return result;

}
*~*~*~*~*~*~*~*~*~*
Darrell Ortiz
CDM Software Solutions, Inc.
https://www.cdmsoft.com
User avatar
cdmmaui
 
Posts: 689
Joined: Fri Oct 28, 2005 9:53 am
Location: Houston ∙ Chicago ∙ Los Angeles ∙ Miami ∙ London ∙ Hong Kong

Re: Convert Java to FWH 2011.08

Postby ukoenig » Sun Apr 22, 2012 8:40 pm

I think straigt converting Java to xBase-syntax will be hard.
I found a Tool converting Java to C++.
C++ You can include in Your App.

Maybe it works for You.
You can try without risk, before buy.
The Demo accepts Lines enaugh, converting Your Function.

http://www.tangiblesoftwaresolutions.com/

Image

Result in C++ of Your Java-code :

Test.ccp

Code: Select all  Expand view

#include "Test.h"

TcpClientUtil::TcpClientUtil()
{

    InitializeInstanceFields();
}

TcpClientUtil::TcpClientUtil(const std::string &HostServer, int HostPort, int SocketTimeout)
{
InitializeInstanceFields();
hostServer = HostServer;
hostPort = HostPort;
socketTimeout = SocketTimeout;

}

FileWriter *TcpClientUtil::getLogger()
{
return s;
}

void TcpClientUtil::setLogger(FileWriter *s)
{
this->s = s;
}

bool TcpClientUtil::ConnectSocket()
{
//Create socket connection
try
{
if (socket == 0)
{
socket = new Socket();
socket->connect(new InetSocketAddress(hostServer, hostPort), socketTimeout);

connected = true;
bufferedOutputStream = new BufferedOutputStream(socket->getOutputStream());
bufferedInputStream = new BufferedInputStream(socket->getInputStream());

}
}
catch (UnknownHostException *e)
{
puts("Unknown host: kq6py.eng");
//System.exit(1);
connected = false;

}
catch (IOException *e)
{
puts("No I/O");
// System.exit(1);
connected = false;

}

return connected;

}

bool TcpClientUtil::DisconnectSocket()
{
try
{
if (socket != 0)
{
bufferedInputStream->close();
socket->close();
connected = false;

}
}
catch (IOException *e)
{
puts("Could not close socket");
exit(-1);
connected = false;

}

return connected;
}

std::string TcpClientUtil::SendRequest(const std::string &dataPacket)
{

std::string result = "";
//ORIGINAL LINE: byte[] bsRequest = nullptr;
//JAVA TO C++ CONVERTER WARNING: Since the array size is not known in this declaration, Java to C++ Converter has converted this array to a pointer.  You will need to call 'delete[]' where appropriate:
char *bsRequest = 0;
char bsResponse[BUFSIZE];
int rcvMsgSize;

try
{

//Encode request
//JAVA TO C++ CONVERTER TODO TASK: There is no native C++ equivalent to 'toString':
puts((Date()).toString() + " : Start Encode Request.");
//s.write(new Date().toString() + " : Start Encode Request.\r\n");
bsRequest = dataPacket.getBytes();
//JAVA TO C++ CONVERTER TODO TASK: There is no native C++ equivalent to 'toString':
puts((Date()).toString() + " : Stop Encode Request.");
//s.write(new Date().toString() + " : Stop Encode Request.\r\n");

//Send Request
if (bsRequest != 0)
{

//JAVA TO C++ CONVERTER TODO TASK: There is no native C++ equivalent to 'toString':
puts((Date()).toString() + " : Start Writing Request to socket.");
//s.write(new Date().toString() + " : Start Writing Request to socket.\r\n");

for (int u = 0; u < sizeof(bsRequest) / sizeof(bsRequest[0]); u++)
{
try
{
//out.write(bsRequest[u]);
bufferedOutputStream->write(bsRequest[u]);
}
catch (IOException *e)
{
// TODO Auto-generated catch block
e->printStackTrace();
}
}
bufferedOutputStream->flush();
puts("Sent data packet to printer");

bool disconnected = DisconnectSocket();
puts("Disconnected from printer");

}

}
catch (Exception *ex)
{

result = "ERROR : " + ex->getMessage();

}
return result;
}

char *TcpClientUtil::getDataPacket(const std::string &data)
{

//ORIGINAL LINE: byte[] b = nullptr;
//JAVA TO C++ CONVERTER WARNING: Since the array size is not known in this declaration, Java to C++ Converter has converted this array to a pointer.  You will need to call 'delete[]' where appropriate:
char *b = 0;

try
{

std::string dataPacket = FileUtil::getContentsAsString("D:\\Argo Wireless\\Code\\sourcetag\\pocs\\TagPrintingPOC\\labels\\from Vijay\\Test.zpl");

b = dataPacket.getBytes();

}
catch (Exception *ex)
{

puts(ex->getMessage());
}

return b;

}

char *TcpClientUtil::concat(char bs1[], char bs2[])
{
char returnArray[sizeof(bs1) / sizeof(bs1[0]) + sizeof(bs2) / sizeof(bs2[0])];
System::arraycopy(bs1, 0, returnArray, 0, sizeof(bs1) / sizeof(bs1[0]));
System::arraycopy(bs2, 0, returnArray, sizeof(bs1) / sizeof(bs1[0]), sizeof(bs2) / sizeof(bs2[0]));
return returnArray;
}

std::string TcpClientUtil::DecodeDataPacket(char bs[])
{

std::string result = "";
try
{
result = std::string(bs,"US-ASCII");
}
catch (Exception *ex)
{
result = "ERROR : " + ex->getMessage();
}
return result;
}

void TcpClientUtil::InitializeInstanceFields()
{
    delete socket;
    delete out;
    delete in_Renamed;
    delete bufferedOutputStream;
    delete bufferedInputStream;
    hostServer = "192.168.1.107";
    hostPort = 9100;
    socketTimeout = 10000;
    connected = false;
    charset = java::nio::charset::Charset::forName("ISO-8859-1");
    decoder = charset->newDecoder();
    encoder = charset->newEncoder();
}

bool <missing_class_definition>::result = false;
 


Test.h

Code: Select all  Expand view


#pragma once

#include <string>

class TcpClientUtil
{

private:
static const int BUFSIZE = 50;

public:
Socket *socket;
OutputStream *out;
InputStream *in_Renamed;
BufferedOutputStream *bufferedOutputStream;
BufferedInputStream *bufferedInputStream;

std::string hostServer; //149.99.39.146
int hostPort; //50030
int socketTimeout;

bool connected;

FileWriter *s;

Charset *charset;
CharsetDecoder *decoder;
CharsetEncoder *encoder;

TcpClientUtil();

TcpClientUtil(const std::string &HostServer, int HostPort, int SocketTimeout);

virtual FileWriter *getLogger();

virtual void setLogger(FileWriter *s);

virtual bool ConnectSocket();

private:
bool DisconnectSocket();

public:
virtual std::string SendRequest(const std::string &dataPacket);

private:
char *getDataPacket(const std::string &data);

char *concat(char bs1[], char bs2[]);

std::string DecodeDataPacket(char bs[]);

private:
    void InitializeInstanceFields();
};

// CODE 2-----------------------------------------------------------------------------
//JAVA TO C++ CONVERTER TODO TASK: The following line could not be converted:
private boolean printTags(String dataPacket, String hostName)
{

public:
bool result;

try
{

//Connect to Server
TcpClientUtil *printer = new TcpClientUtil(hostName, static_cast<int>(printerport), static_cast<int>(printerconnectiontimeout));

bool connected = printer->ConnectSocket();

if (connected == true)
{
//JAVA TO C++ CONVERTER TODO TASK: There is no native C++ equivalent to 'toString':
logger::info((Date()).toString() + " : Connected To Printer.\r\n");

std::string response = printer->SendRequest(dataPacket);

return true;

}

}
catch (Exception *ex)
{

//JAVA TO C++ CONVERTER TODO TASK: There is no native C++ equivalent to 'toString':
logger::info((Date()).toString() + " : " + ex->getMessage());
return false;

}

return result;

}
 


Best Regards
Uwe :lol:
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
User avatar
ukoenig
 
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Otto and 86 guests