Style cleanup patch by Vasilis Kaoutsis. Small changes by myself.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@22629 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold
2007-10-20 20:40:09 +00:00
parent cb93a65449
commit 9d3f15c60e
10 changed files with 1623 additions and 1713 deletions
File diff suppressed because it is too large Load Diff
+89 -103
View File
@@ -1,20 +1,22 @@
/* CodyCam.h */
#ifndef CODYCAM_H
#define CODYCAM_H
#include <Box.h>
#include <Menu.h>
#include <string.h>
#include <Window.h>
#include <CheckBox.h>
#include <MenuField.h>
#include <StringView.h>
#include <Application.h>
#include <TextControl.h>
#include "Settings.h"
#include"VideoConsumer.h"
#include "VideoConsumer.h"
#include <string.h>
#include <Application.h>
#include <Box.h>
#include <CheckBox.h>
#include <Menu.h>
#include <MenuField.h>
#include <StringView.h>
#include <TextControl.h>
#include <Window.h>
class BMediaRoster;
@@ -56,7 +58,8 @@ enum {
msg_control_win = 'ctlw'
};
const char *kCaptureRate[] = {
const char* kCaptureRate[] = {
"Every 15 seconds",
"Every 30 seconds",
"Every minute",
@@ -73,113 +76,96 @@ const char *kCaptureRate[] = {
0
};
class CodyCam : public BApplication {
public:
CodyCam();
virtual ~CodyCam();
void ReadyToRun();
virtual bool QuitRequested();
virtual void MessageReceived(
BMessage *message);
CodyCam();
virtual ~CodyCam();
void ReadyToRun();
virtual bool QuitRequested();
virtual void MessageReceived(BMessage* message);
private:
status_t SetUpNodes();
void TearDownNodes();
BMediaRoster * fMediaRoster;
status_t _SetUpNodes();
void _TearDownNodes();
media_node fTimeSourceNode;
media_node fProducerNode;
VideoConsumer * fVideoConsumer;
media_output fProducerOut;
media_input fConsumerIn;
BWindow * fWindow;
port_id fPort;
BWindow *mVideoControlWindow;
BMediaRoster* fMediaRoster;
media_node fTimeSourceNode;
media_node fProducerNode;
VideoConsumer* fVideoConsumer;
media_output fProducerOut;
media_input fConsumerIn;
BWindow* fWindow;
port_id fPort;
BWindow* fVideoControlWindow;
};
class VideoWindow : public BWindow
{
public:
VideoWindow (
BRect frame,
const char * title,
window_type type,
uint32 flags,
port_id * consumerport);
~VideoWindow();
virtual bool QuitRequested();
virtual void MessageReceived(
BMessage *message);
class VideoWindow : public BWindow {
public:
VideoWindow(BRect frame, const char* title, window_type type,
uint32 flags, port_id* consumerport);
~VideoWindow();
void ApplyControls();
BView * VideoView();
BStringView * StatusLine();
private:
void BuildCaptureControls(
BView *theView);
virtual bool QuitRequested();
virtual void MessageReceived(BMessage* message);
void SetUpSettings(
const char *filename,
const char *dirname);
void QuitSettings();
void ApplyControls();
private:
media_node * fProducer;
port_id * fPortPtr;
BView * fView;
BView * fVideoView;
BTextControl * fFileName;
BBox * fCaptureSetupBox;
BMenu * fCaptureRateMenu;
BMenuField * fCaptureRateSelector;
BMenu * fImageFormatMenu;
BMenuField * fImageFormatSelector;
BBox * fFtpSetupBox;
BTextControl * fServerName;
BTextControl * fLoginId;
BTextControl * fPassword;
BTextControl * fDirectory;
BCheckBox * fPassiveFtp;
BBox * fStatusBox;
BStringView * fStatusLine;
ftp_msg_info fFtpInfo;
BView* VideoView();
BStringView* StatusLine();
Settings * fSettings;
StringValueSetting * fServerSetting;
StringValueSetting * fLoginSetting;
StringValueSetting * fPasswordSetting;
StringValueSetting * fDirectorySetting;
BooleanValueSetting * fPassiveFtpSetting;
StringValueSetting * fFilenameSetting;
EnumeratedStringValueSetting *fCaptureRateSetting;
private:
void _BuildCaptureControls(BView* theView);
void _SetUpSettings(const char* filename, const char* dirname);
void _QuitSettings();
private:
media_node* fProducer;
port_id* fPortPtr;
BView* fView;
BView* fVideoView;
BTextControl* fFileName;
BBox* fCaptureSetupBox;
BMenu* fCaptureRateMenu;
BMenuField* fCaptureRateSelector;
BMenu* fImageFormatMenu;
BMenuField* fImageFormatSelector;
BBox* fFtpSetupBox;
BTextControl* fServerName;
BTextControl* fLoginId;
BTextControl* fPassword;
BTextControl* fDirectory;
BCheckBox* fPassiveFtp;
BBox* fStatusBox;
BStringView* fStatusLine;
ftp_msg_info fFtpInfo;
Settings* fSettings;
StringValueSetting* fServerSetting;
StringValueSetting* fLoginSetting;
StringValueSetting* fPasswordSetting;
StringValueSetting* fDirectorySetting;
BooleanValueSetting* fPassiveFtpSetting;
StringValueSetting* fFilenameSetting;
EnumeratedStringValueSetting* fCaptureRateSetting;
};
class ControlWindow : public BWindow {
public:
ControlWindow(const BRect & frame, BView * controls, media_node node);
void MessageReceived(BMessage * message);
public:
ControlWindow(const BRect& frame, BView* controls, media_node node);
void MessageReceived(BMessage* message);
bool QuitRequested();
private:
BView * fView;
media_node fNode;
private:
BView* fView;
media_node fNode;
};
#endif
#endif // CODYCAM_H
+276 -372
View File
@@ -1,38 +1,36 @@
#include "FtpClient.h"
FtpClient::FtpClient()
:
fState(0),
fControl(NULL),
fData(NULL)
{
m_control = 0;
m_state = 0;
m_data = 0;
}
FtpClient::~FtpClient()
{
delete m_control;
delete m_data;
delete fControl;
delete fData;
}
bool FtpClient::cd(const string &dir)
bool
FtpClient::ChangeDir(const string& dir)
{
bool rc = false;
int code, codetype;
string cmd = "CWD ", replystr;
int code, codeType;
string cmd = "CWD ", replyString;
cmd += dir;
if(dir.length() == 0)
if (dir.length() == 0)
cmd += '/';
if(p_sendRequest(cmd) == true)
{
if(p_getReply(replystr, code, codetype) == true)
{
if(codetype == 2)
if (_SendRequest(cmd) == true) {
if (_GetReply(replyString, code, codeType) == true) {
if (codeType == 2)
rc = true;
}
}
@@ -40,44 +38,36 @@ bool FtpClient::cd(const string &dir)
}
bool FtpClient::ls(string &listing)
bool
FtpClient::ListDirContents(string& listing)
{
bool rc = false;
string cmd, replystr;
int code, codetype, numread;
string cmd, replyString;
int code, codeType, numRead;
char buf[513];
cmd = "TYPE A";
if(p_sendRequest(cmd))
p_getReply(replystr, code, codetype);
if (_SendRequest(cmd))
_GetReply(replyString, code, codeType);
if(p_openDataConnection())
{
if (_OpenDataConnection()) {
cmd = "LIST";
if(p_sendRequest(cmd))
{
if(p_getReply(replystr, code, codetype))
{
if(codetype <= 2)
{
if(p_acceptDataConnection())
{
numread = 1;
while(numread > 0)
{
if (_SendRequest(cmd)) {
if (_GetReply(replyString, code, codeType)) {
if (codeType <= 2) {
if (_AcceptDataConnection()) {
numRead = 1;
while (numRead > 0) {
memset(buf, 0, sizeof(buf));
numread = m_data->Receive(buf, sizeof(buf) - 1);
numRead = fData->Receive(buf, sizeof(buf) - 1);
listing += buf;
printf(buf);
}
if(p_getReply(replystr, code, codetype))
{
if(codetype <= 2)
{
if (_GetReply(replyString, code, codeType)) {
if (codeType <= 2)
rc = true;
}
}
}
}
@@ -85,31 +75,28 @@ bool FtpClient::ls(string &listing)
}
}
delete m_data;
m_data = 0;
delete fData;
fData = 0;
return rc;
}
bool FtpClient::pwd(string &dir)
bool
FtpClient::PrintWorkingDir(string& dir)
{
bool rc = false;
int code, codetype;
string cmd = "PWD", replystr;
int code, codeType;
string cmd = "PWD", replyString;
long i;
if(p_sendRequest(cmd) == true)
{
if(p_getReply(replystr, code, codetype) == true)
{
if(codetype == 2)
{
i = replystr.find('"');
if(i != -1)
{
if (_SendRequest(cmd) == true) {
if (_GetReply(replyString, code, codeType) == true) {
if (codeType == 2) {
i = replyString.find('"');
if (i != -1) {
i++;
dir = replystr.substr(i, replystr.find('"') - i);
dir = replyString.substr(i, replyString.find('"') - i);
rc = true;
}
}
@@ -120,129 +107,110 @@ bool FtpClient::pwd(string &dir)
}
bool FtpClient::connect(const string &server, const string &login, const string &passwd)
bool
FtpClient::Connect(const string& server, const string& login, const string& passwd)
{
bool rc = false;
int code, codetype;
string cmd, replystr;
int code, codeType;
string cmd, replyString;
BNetAddress addr;
delete m_control;
delete m_data;
delete fControl;
delete fData;
m_control = new BNetEndpoint;
if(m_control->InitCheck() != B_NO_ERROR)
fControl = new BNetEndpoint;
if (fControl->InitCheck() != B_NO_ERROR)
return false;
addr.SetTo(server.c_str(), "tcp", "ftp");
if(m_control->Connect(addr) == B_NO_ERROR)
{
//
if (fControl->Connect(addr) == B_NO_ERROR) {
// read the welcome message, do the login
//
if(p_getReply(replystr, code, codetype))
{
if(code != 421 && codetype != 5)
{
cmd = "USER "; cmd += login;
p_sendRequest(cmd);
if(p_getReply(replystr, code, codetype))
{
switch(code)
{
if (_GetReply(replyString, code, codeType)) {
if (code != 421 && codeType != 5) {
cmd = "USER ";
cmd += login;
_SendRequest(cmd);
if (_GetReply(replyString, code, codeType)) {
switch (code) {
case 230:
case 202:
rc = true;
break;
break;
case 331: // password needed
cmd = "PASS "; cmd += passwd;
p_sendRequest(cmd);
if(p_getReply(replystr, code, codetype))
{
if(codetype == 2)
{
cmd = "PASS ";
cmd += passwd;
_SendRequest(cmd);
if (_GetReply(replyString, code, codeType)) {
if (codeType == 2)
rc = true;
}
}
break;
break;
default:
break;
break;
}
}
}
}
}
if(rc == true)
{
p_setState(ftp_connected);
} else {
delete m_control;
m_control = 0;
if (rc == true)
_SetState(ftp_connected);
else {
delete fControl;
fControl = 0;
}
return rc;
}
bool FtpClient::putFile(const string &local, const string &remote, ftp_mode mode)
bool
FtpClient::PutFile(const string& local, const string& remote, ftp_mode mode)
{
bool rc = false;
string cmd, replystr;
int code, codetype, rlen, slen, i;
string cmd, replyString;
int code, codeType, rlen, slen, i;
BFile infile(local.c_str(), B_READ_ONLY);
char buf[8192], sbuf[16384], *stmp;
if(infile.InitCheck() != B_NO_ERROR)
if (infile.InitCheck() != B_NO_ERROR)
return false;
if(mode == binary_mode)
if (mode == binary_mode)
cmd = "TYPE I";
else
cmd = "TYPE A";
if(p_sendRequest(cmd))
p_getReply(replystr, code, codetype);
if (_SendRequest(cmd))
_GetReply(replyString, code, codeType);
try
{
if(p_openDataConnection())
{
try {
if (_OpenDataConnection()) {
cmd = "STOR ";
cmd += remote;
if(p_sendRequest(cmd))
{
if(p_getReply(replystr, code, codetype))
{
if(codetype <= 2)
{
if(p_acceptDataConnection())
{
if (_SendRequest(cmd)) {
if (_GetReply(replyString, code, codeType)) {
if (codeType <= 2) {
if (_AcceptDataConnection()) {
rlen = 1;
while(rlen > 0)
{
while (rlen > 0) {
memset(buf, 0, sizeof(buf));
memset(sbuf, 0, sizeof(sbuf));
rlen = infile.Read((void *) buf, sizeof(buf));
rlen = infile.Read((void*)buf, sizeof(buf));
slen = rlen;
stmp = buf;
if(mode == ascii_mode)
{
if (mode == ascii_mode) {
stmp = sbuf;
slen = 0;
for(i=0;i<rlen;i++)
{
if(buf[i] == '\n')
{
for (i = 0; i < rlen; i++) {
if (buf[i] == '\n') {
*stmp = '\r';
stmp++;
slen++;
@@ -253,9 +221,8 @@ bool FtpClient::putFile(const string &local, const string &remote, ftp_mode mode
}
stmp = sbuf;
}
if(slen > 0)
{
if(m_data->Send(stmp, slen) < 0)
if (slen > 0) {
if (fData->Send(stmp, slen) < 0)
throw "bail";
}
}
@@ -267,143 +234,119 @@ bool FtpClient::putFile(const string &local, const string &remote, ftp_mode mode
}
}
}
catch(const char *errstr)
{
catch(const char* errorString)
{
}
delete fData;
fData = 0;
if (rc) {
_GetReply(replyString, code, codeType);
rc = codeType <= 2;
}
delete m_data;
m_data = 0;
if(rc == true)
{
p_getReply(replystr, code, codetype);
rc = (bool) codetype <= 2;
}
return rc;
}
bool FtpClient::getFile(const string &remote, const string &local, ftp_mode mode)
bool
FtpClient::GetFile(const string& remote, const string& local, ftp_mode mode)
{
bool rc = false;
string cmd, replystr;
int code, codetype, rlen, slen, i;
string cmd, replyString;
int code, codeType, rlen, slen, i;
BFile outfile(local.c_str(), B_READ_WRITE | B_CREATE_FILE);
char buf[8192], sbuf[16384], *stmp;
bool writeerr = false;
if(outfile.InitCheck() != B_NO_ERROR)
bool writeError = false;
if (outfile.InitCheck() != B_NO_ERROR)
return false;
if(mode == binary_mode)
if (mode == binary_mode)
cmd = "TYPE I";
else
cmd = "TYPE A";
if(p_sendRequest(cmd))
p_getReply(replystr, code, codetype);
if(p_openDataConnection())
{
if (_SendRequest(cmd))
_GetReply(replyString, code, codeType);
if (_OpenDataConnection()) {
cmd = "RETR ";
cmd += remote;
if(p_sendRequest(cmd))
{
if(p_getReply(replystr, code, codetype))
{
if(codetype <= 2)
{
if(p_acceptDataConnection())
{
if (_SendRequest(cmd)) {
if (_GetReply(replyString, code, codeType)) {
if (codeType <= 2) {
if (_AcceptDataConnection()) {
rlen = 1;
rc = true;
while(rlen > 0)
{
while (rlen > 0) {
memset(buf, 0, sizeof(buf));
memset(sbuf, 0, sizeof(sbuf));
rlen = m_data->Receive(buf, sizeof(buf));
rlen = fData->Receive(buf, sizeof(buf));
if(rlen > 0)
{
if (rlen > 0) {
slen = rlen;
stmp = buf;
if(mode == ascii_mode)
{
if (mode == ascii_mode) {
stmp = sbuf;
slen = 0;
for(i=0;i<rlen;i++)
{
if(buf[i] == '\r')
{
for (i = 0; i < rlen; i++) {
if (buf[i] == '\r')
i++;
}
*stmp = buf[i];
stmp++;
slen++;
}
stmp = sbuf;
}
if(slen > 0)
{
if(outfile.Write(stmp, slen) < 0)
{
writeerr = true;
}
if (slen > 0) {
if (outfile.Write(stmp, slen) < 0)
writeError = true;
}
}
}
}
}
}
}
}
delete fData;
fData = 0;
delete m_data;
m_data = 0;
if(rc == true)
{
p_getReply(replystr, code, codetype);
rc = (bool) ((codetype <= 2) && (writeerr == false));
}
if (rc) {
_GetReply(replyString, code, codeType);
rc = (codeType <= 2 && writeError == false);
}
return rc;
}
//
// Note: this only works for local remote moves, cross filesystem moves
// will not work
//
bool FtpClient::moveFile(const string &oldpath, const string &newpath)
bool
FtpClient::MoveFile(const string& oldPath, const string& newPath)
{
bool rc = false;
string from = "RNFR ";
string to = "RNTO ";
string replystr;
int code, codetype;
string replyString;
int code, codeType;
from += oldpath;
to += newpath;
if(p_sendRequest(from))
{
if(p_getReply(replystr, code, codetype))
{
if(codetype == 3)
{
if(p_sendRequest(to))
{
if(p_getReply(replystr, code, codetype))
{
if(codetype == 2)
from += oldPath;
to += newPath;
if (_SendRequest(from)) {
if (_GetReply(replyString, code, codeType)) {
if (codeType == 3) {
if (_SendRequest(to)) {
if (_GetReply(replyString, code, codeType)) {
if(codeType == 2)
rc = true;
}
}
@@ -414,62 +357,60 @@ bool FtpClient::moveFile(const string &oldpath, const string &newpath)
}
void FtpClient::setPassive(bool on)
void
FtpClient::SetPassive(bool on)
{
if(on)
p_setState(ftp_passive);
if (on)
_SetState(ftp_passive);
else
p_clearState(ftp_passive);
_ClearState(ftp_passive);
}
bool FtpClient::p_testState(unsigned long state)
bool
FtpClient::_TestState(unsigned long state)
{
return (bool) ((m_state & state) != 0);
return ((fState & state) != 0);
}
void FtpClient::p_setState(unsigned long state)
void
FtpClient::_SetState(unsigned long state)
{
m_state |= state;
fState |= state;
}
void FtpClient::p_clearState(unsigned long state)
void
FtpClient::_ClearState(unsigned long state)
{
m_state &= ~state;
fState &= ~state;
}
bool FtpClient::p_sendRequest(const string &cmd)
bool
FtpClient::_SendRequest(const string& cmd)
{
bool rc = false;
string ccmd = cmd;
if(m_control != 0)
{
if(cmd.find("PASS") != string::npos)
if (fControl != 0) {
if (cmd.find("PASS") != string::npos)
printf("PASS <suppressed> (real password sent)\n");
else
printf("%s\n", ccmd.c_str());
ccmd += "\r\n";
if(m_control->Send(ccmd.c_str(), ccmd.length()) >= 0)
{
if (fControl->Send(ccmd.c_str(), ccmd.length()) >= 0)
rc = true;
}
}
return rc;
}
bool FtpClient::p_getReplyLine(string &line)
bool
FtpClient::_GetReplyLine(string& line)
{
bool rc = false;
int c = 0;
@@ -477,38 +418,33 @@ bool FtpClient::p_getReplyLine(string &line)
line = ""; // Thanks to Stephen van Egmond for catching a bug here
if(m_control != 0)
{
if (fControl != 0) {
rc = true;
while(done == false && (m_control->Receive(&c, 1) > 0))
{
if(c == EOF || c == xEOF || c == '\n')
{
while (done == false && fControl->Receive(&c, 1) > 0) {
if (c == EOF || c == xEOF || c == '\n') {
done = true;
} else {
if(c == IAC)
{
m_control->Receive(&c, 1);
switch(c)
{
if (c == IAC) {
fControl->Receive(&c, 1);
switch (c) {
unsigned char treply[3];
case WILL:
case WONT:
m_control->Receive(&c, 1);
fControl->Receive(&c, 1);
treply[0] = IAC;
treply[1] = DONT;
treply[2] = c;
m_control->Send(treply, 3);
fControl->Send(treply, 3);
break;
case DO:
case DONT:
m_control->Receive(&c, 1);
m_control->Receive(&c, 1);
fControl->Receive(&c, 1);
fControl->Receive(&c, 1);
treply[0] = IAC;
treply[1] = WONT;
treply[2] = c;
m_control->Send(treply, 3);
fControl->Send(treply, 3);
break;
case EOF:
@@ -521,10 +457,8 @@ bool FtpClient::p_getReplyLine(string &line)
break;
}
} else {
//
// normal char
//
if(c != '\r')
if (c != '\r')
line += c;
}
}
@@ -535,10 +469,11 @@ bool FtpClient::p_getReplyLine(string &line)
}
bool FtpClient::p_getReply(string &outstr, int &outcode, int &codetype)
bool
FtpClient::_GetReply(string& outString, int& outCode, int& codeType)
{
bool rc = false;
string line, tempstr;
string line, tempString;
//
// comment from the ncftp source:
@@ -556,149 +491,125 @@ bool FtpClient::p_getReply(string &outstr, int &outcode, int &codetype)
* 234 A line beginning with numbers
* 123 The last line
*/
if((rc = p_getReplyLine(line)) == true)
{
outstr = line;
outstr += '\n';
printf(outstr.c_str());
tempstr = line.substr(0, 3);
outcode = atoi(tempstr.c_str());
if ((rc = _GetReplyLine(line)) == true) {
outString = line;
outString += '\n';
printf(outString.c_str());
tempString = line.substr(0, 3);
outCode = atoi(tempString.c_str());
if(line[3] == '-')
{
while((rc = p_getReplyLine(line)) == true)
{
outstr += line;
outstr += '\n';
printf(outstr.c_str());
//
if (line[3] == '-') {
while ((rc = _GetReplyLine(line)) == true) {
outString += line;
outString += '\n';
printf(outString.c_str());
// we're done with nnn when we get to a "nnn blahblahblah"
//
if((line.find(tempstr) == 0) && line[3] == ' ')
if ((line.find(tempString) == 0) && line[3] == ' ')
break;
}
}
}
if(rc == false && outcode != 421)
{
outstr += "Remote host has closed the connection.\n";
outcode = 421;
if (!rc && outCode != 421) {
outString += "Remote host has closed the connection.\n";
outCode = 421;
}
if(outcode == 421)
{
delete m_control;
m_control = 0;
p_clearState(ftp_connected);
if (outCode == 421) {
delete fControl;
fControl = 0;
_ClearState(ftp_connected);
}
codetype = outcode / 100;
codeType = outCode / 100;
return rc;
}
bool FtpClient::p_openDataConnection()
bool
FtpClient::_OpenDataConnection()
{
string host, cmd, repstr;
string host, cmd, replyString;
unsigned short port;
BNetAddress addr;
int i, code, codetype;
int i, code, codeType;
bool rc = false;
struct sockaddr_in sa;
delete m_data;
m_data = 0;
delete fData;
fData = 0;
m_data = new BNetEndpoint;
fData = new BNetEndpoint;
if(p_testState(ftp_passive))
{
//
if (_TestState(ftp_passive)) {
// Here we send a "pasv" command and connect to the remote server
// on the port it sends back to us
//
cmd = "PASV";
if(p_sendRequest(cmd))
{
if(p_getReply(repstr, code, codetype))
{
if(codetype == 2)
{
if (_SendRequest(cmd)) {
if (_GetReply(replyString, code, codeType)) {
if (codeType == 2) {
// It should give us something like:
// "227 Entering Passive Mode (192,168,1,1,10,187)"
int paddr[6];
unsigned char ucaddr[6];
i = repstr.find('(');
i = replyString.find('(');
i++;
repstr = repstr.substr(i, repstr.find(')') - i);
if (sscanf(repstr.c_str(), "%d,%d,%d,%d,%d,%d",
replyString = replyString.substr(i, replyString.find(')') - i);
if (sscanf(replyString.c_str(), "%d,%d,%d,%d,%d,%d",
&paddr[0], &paddr[1], &paddr[2], &paddr[3],
&paddr[4], &paddr[5]) != 6)
{
//
// cannot do passive. Do a little harmless rercursion here
//
p_clearState(ftp_passive);
return p_openDataConnection();
&paddr[4], &paddr[5]) != 6) {
// cannot do passive. Do a little harmless rercursion here
_ClearState(ftp_passive);
return _OpenDataConnection();
}
for(i=0;i<6;i++)
{
ucaddr[i] = (unsigned char) (paddr[i] & 0xff);
}
for (i = 0; i < 6; i++)
ucaddr[i] = (unsigned char)(paddr[i] & 0xff);
memcpy(&sa.sin_addr, &ucaddr[0], (size_t) 4);
memcpy(&sa.sin_port, &ucaddr[4], (size_t) 2);
addr.SetTo(sa);
if(m_data->Connect(addr) == B_NO_ERROR)
{
if (fData->Connect(addr) == B_NO_ERROR)
rc = true;
}
}
}
} else {
//
// cannot do passive. Do a little harmless rercursion here
//
p_clearState(ftp_passive);
rc = p_openDataConnection();
_ClearState(ftp_passive);
rc = _OpenDataConnection();
}
} else {
//
// Here we bind to a local port and send a PORT command
//
if(m_data->Bind() == B_NO_ERROR)
{
if (fData->Bind() == B_NO_ERROR) {
char buf[255];
m_data->Listen();
addr = m_data->LocalAddr();
fData->Listen();
addr = fData->LocalAddr();
addr.GetAddr(buf, &port);
host = buf;
i=0;
while(i >= 0)
{
i = 0;
while (i >= 0) {
i = host.find('.', i);
if(i >= 0)
if (i >= 0)
host[i] = ',';
}
sprintf(buf, ",%d,%d", (port & 0xff00) >> 8, port & 0x00ff);
cmd = "PORT ";
cmd += host; cmd += buf;
p_sendRequest(cmd);
p_getReply(repstr, code, codetype);
//
cmd += host;
cmd += buf;
_SendRequest(cmd);
_GetReply(replyString, code, codeType);
// PORT failure is in the 500-range
//
if(codetype == 2)
if (codeType == 2)
rc = true;
}
}
@@ -707,32 +618,25 @@ bool FtpClient::p_openDataConnection()
}
bool FtpClient::p_acceptDataConnection()
bool
FtpClient::_AcceptDataConnection()
{
BNetEndpoint *ep;
BNetEndpoint* endPoint;
bool rc = false;
if(p_testState(ftp_passive) == false)
{
if(m_data != 0)
{
ep = m_data->Accept();
if(ep != 0)
{
delete m_data;
m_data = ep;
if (_TestState(ftp_passive) == false) {
if (fData != 0) {
endPoint = fData->Accept();
if (endPoint != 0) {
delete fData;
fData = endPoint;
rc = true;
}
}
} else {
}
else
rc = true;
}
return rc;
}
+60 -48
View File
@@ -1,54 +1,15 @@
#include <string>
#include <File.h>
#ifndef FTP_CLIENT_H
#define FTP_CLIENT_H
#include <stdio.h>
#include <string>
#include <File.h>
#include <NetworkKit.h>
using std::string;
class FtpClient
{
public:
FtpClient();
~FtpClient();
enum ftp_mode
{
binary_mode,
ascii_mode
};
bool connect(const string &server, const string &login, const string &passwd);
bool putFile(const string &local, const string &remote, ftp_mode mode = binary_mode);
bool getFile(const string &remote, const string &local, ftp_mode mode = binary_mode);
bool moveFile(const string &oldpath, const string &newpath);
bool cd(const string &dir);
bool pwd(string &dir);
bool ls(string &listing);
void setPassive(bool on);
protected:
enum {
ftp_complete = 1UL,
ftp_connected = 2,
ftp_passive = 4
};
unsigned long m_state;
bool p_testState(unsigned long state);
void p_setState(unsigned long state);
void p_clearState(unsigned long state);
bool p_sendRequest(const string &cmd);
bool p_getReply(string &outstr, int &outcode, int &codetype);
bool p_getReplyLine(string &line);
bool p_openDataConnection();
bool p_acceptDataConnection();
BNetEndpoint *m_control;
BNetEndpoint *m_data;
};
/*
* Definitions for the TELNET protocol. Snarfed from the BSD source.
@@ -58,5 +19,56 @@ protected:
#define DO 253
#define WONT 252
#define WILL 251
#define xEOF 236
#define xEOF 236
class FtpClient {
public:
FtpClient();
~FtpClient();
enum ftp_mode {
binary_mode,
ascii_mode
};
bool Connect(const string& server, const string& login,
const string& passwd);
bool PutFile(const string& local, const string& remote,
ftp_mode mode = binary_mode);
bool GetFile(const string& remote, const string& local,
ftp_mode mode = binary_mode);
bool MoveFile(const string& oldPath, const string& newPath);
bool ChangeDir(const string& dir);
bool PrintWorkingDir(string& dir);
bool ListDirContents(string& listing);
void SetPassive(bool on);
protected:
enum {
ftp_complete = 1UL,
ftp_connected = 2,
ftp_passive = 4
};
bool _TestState(unsigned long state);
void _SetState(unsigned long state);
void _ClearState(unsigned long state);
bool _SendRequest(const string& cmd);
bool _GetReply(string& outString, int& outCode, int& codeType);
bool _GetReplyLine(string& line);
bool _OpenDataConnection();
bool _AcceptDataConnection();
unsigned long fState;
BNetEndpoint* fControl;
BNetEndpoint* fData;
};
#endif // FTP_CLIENT_H
+105 -78
View File
@@ -1,90 +1,101 @@
#include <Debug.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "Settings.h"
Settings *settings = NULL;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// generic setting handler classes
#include <Debug.h>
StringValueSetting::StringValueSetting(const char *name, const char *defaultValue,
const char *valueExpectedErrorString, const char *wrongValueErrorString)
: SettingsArgvDispatcher(name),
defaultValue(defaultValue),
valueExpectedErrorString(valueExpectedErrorString),
wrongValueErrorString(wrongValueErrorString),
value(strdup(defaultValue))
Settings* settings = NULL;
StringValueSetting::StringValueSetting(const char* name, const char* defaultValue,
const char* valueExpectedErrorString, const char* wrongValueErrorString)
: SettingsArgvDispatcher(name),
fDefaultValue(defaultValue),
fValueExpectedErrorString(valueExpectedErrorString),
fWrongValueErrorString(wrongValueErrorString),
fValue(strdup(defaultValue))
{
}
StringValueSetting::~StringValueSetting()
{
free(value);
free(fValue);
}
void
StringValueSetting::ValueChanged(const char *newValue)
StringValueSetting::ValueChanged(const char* newValue)
{
if (newValue == value)
if (newValue == fValue)
// guard against self assingment
return;
free(value);
value = strdup(newValue);
free(fValue);
fValue = strdup(newValue);
}
const char *
const char*
StringValueSetting::Value() const
{
return value;
return fValue;
}
void
StringValueSetting::SaveSettingValue(Settings *settings)
StringValueSetting::SaveSettingValue(Settings* settings)
{
printf("-------StringValueSetting::SaveSettingValue %s %s\n", Name(), value);
settings->Write("\"%s\"", value);
printf("-------StringValueSetting::SaveSettingValue %s %s\n", Name(), fValue);
settings->Write("\"%s\"", fValue);
}
bool
StringValueSetting::NeedsSaving() const
{
// needs saving if different than default
return strcmp(value, defaultValue) != 0;
return strcmp(fValue, fDefaultValue) != 0;
}
const char *
const char*
StringValueSetting::Handle(const char *const *argv)
{
if (!*++argv)
return valueExpectedErrorString;
return fValueExpectedErrorString;
ValueChanged(*argv);
return 0;
}
EnumeratedStringValueSetting::EnumeratedStringValueSetting(const char *name,
const char *defaultValue, const char *const *values, const char *valueExpectedErrorString,
const char *wrongValueErrorString)
: StringValueSetting(name, defaultValue, valueExpectedErrorString, wrongValueErrorString),
values(values)
// #pragma mark -
EnumeratedStringValueSetting::EnumeratedStringValueSetting(const char* name,
const char* defaultValue, const char *const *values,
const char* valueExpectedErrorString,
const char* wrongValueErrorString)
: StringValueSetting(name, defaultValue, valueExpectedErrorString, wrongValueErrorString),
fValues(values)
{
}
void
EnumeratedStringValueSetting::ValueChanged(const char *newValue)
EnumeratedStringValueSetting::ValueChanged(const char* newValue)
{
#if DEBUG
// must be one of the enumerated values
bool found = false;
for (int32 index = 0; ; index++) {
if (!values[index])
if (!fValues[index])
break;
if (strcmp(values[index], newValue) != 0)
if (strcmp(fValues[index], newValue) != 0)
continue;
found = true;
break;
@@ -94,120 +105,136 @@ EnumeratedStringValueSetting::ValueChanged(const char *newValue)
StringValueSetting::ValueChanged(newValue);
}
const char *
const char*
EnumeratedStringValueSetting::Handle(const char *const *argv)
{
if (!*++argv)
return valueExpectedErrorString;
return fValueExpectedErrorString;
printf("-----EnumeratedStringValueSetting::Handle %s %s\n", *(argv-1), *argv);
printf("-----EnumeratedStringValueSetting::Handle %s %s\n", *(argv-1), *argv);
bool found = false;
for (int32 index = 0; ; index++) {
if (!values[index])
if (!fValues[index])
break;
if (strcmp(values[index], *argv) != 0)
if (strcmp(fValues[index], *argv) != 0)
continue;
found = true;
break;
}
if (!found)
return wrongValueErrorString;
return fWrongValueErrorString;
ValueChanged(*argv);
return 0;
}
ScalarValueSetting::ScalarValueSetting(const char *name, int32 defaultValue,
const char *valueExpectedErrorString, const char *wrongValueErrorString,
// #pragma mark -
ScalarValueSetting::ScalarValueSetting(const char* name, int32 defaultValue,
const char* valueExpectedErrorString, const char* wrongValueErrorString,
int32 min, int32 max)
: SettingsArgvDispatcher(name),
defaultValue(defaultValue),
value(defaultValue),
max(max),
min(min),
valueExpectedErrorString(valueExpectedErrorString),
wrongValueErrorString(wrongValueErrorString)
: SettingsArgvDispatcher(name),
fDefaultValue(defaultValue),
fValue(defaultValue),
fMax(max),
fMin(min),
fValueExpectedErrorString(valueExpectedErrorString),
fWrongValueErrorString(wrongValueErrorString)
{
}
void
ScalarValueSetting::ValueChanged(int32 newValue)
{
ASSERT(newValue > min);
ASSERT(newValue < max);
value = newValue;
ASSERT(newValue > fMin);
ASSERT(newValue < fMax);
fValue = newValue;
}
int32
ScalarValueSetting::Value() const
{
return value;
return fValue;
}
void
ScalarValueSetting::GetValueAsString(char *buffer) const
ScalarValueSetting::GetValueAsString(char* buffer) const
{
sprintf(buffer, "%ld", value);
sprintf(buffer, "%ld", fValue);
}
const char *
const char*
ScalarValueSetting::Handle(const char *const *argv)
{
if (!*++argv)
return valueExpectedErrorString;
return fValueExpectedErrorString;
int32 newValue = atoi(*argv);
if (newValue < min || newValue > max)
return wrongValueErrorString;
value = newValue;
if (newValue < fMin || newValue > fMax)
return fWrongValueErrorString;
fValue = newValue;
return 0;
}
void
ScalarValueSetting::SaveSettingValue(Settings *settings)
ScalarValueSetting::SaveSettingValue(Settings* settings)
{
settings->Write("%d", value);
settings->Write("%d", fValue);
}
bool
ScalarValueSetting::NeedsSaving() const
{
return value != defaultValue;
return fValue != fDefaultValue;
}
BooleanValueSetting::BooleanValueSetting(const char *name, bool defaultValue )
: ScalarValueSetting(name, defaultValue, 0, 0)
// #pragma mark -
BooleanValueSetting::BooleanValueSetting(const char* name, bool defaultValue)
: ScalarValueSetting(name, defaultValue, 0, 0)
{
}
bool
BooleanValueSetting::Value() const
{
return value;
return fValue;
}
const char *
const char*
BooleanValueSetting::Handle(const char *const *argv)
{
if (!*++argv)
return "or or off expected";
return "on or off expected";
if (strcmp(*argv, "on") == 0)
value = true;
fValue = true;
else if (strcmp(*argv, "off") == 0)
value = false;
fValue = false;
else
return "or or off expected";
return "on or off expected";
return 0;
}
void
BooleanValueSetting::SaveSettingValue(Settings *settings)
{
settings->Write(value ? "on" : "off");
}
void
BooleanValueSetting::SaveSettingValue(Settings* settings)
{
settings->Write(fValue ? "on" : "off");
}
+43 -41
View File
@@ -1,84 +1,86 @@
#ifndef __SETTINGS__
#define __SETTINGS__
#ifndef SETTINGS_H
#define SETTINGS_H
#include "SettingsHandler.h"
void SetUpSettings(char *filename);
void SetUpSettings(char* filename);
void QuitSettings();
class StringValueSetting : public SettingsArgvDispatcher {
// simple string setting
public:
StringValueSetting(const char *name, const char *defaultValue,
const char *valueExpectedErrorString,
const char *wrongValueErrorString);
StringValueSetting(const char* name, const char* defaultValue,
const char* valueExpectedErrorString,
const char* wrongValueErrorString);
virtual ~StringValueSetting();
void ValueChanged(const char *newValue);
const char *Value() const;
virtual const char *Handle(const char *const *argv);
void ValueChanged(const char* newValue);
const char* Value() const;
virtual const char* Handle(const char *const *argv);
protected:
virtual void SaveSettingValue(Settings *);
virtual void SaveSettingValue(Settings*);
virtual bool NeedsSaving() const;
const char *defaultValue;
const char *valueExpectedErrorString;
const char *wrongValueErrorString;
char *value;
const char* fDefaultValue;
const char* fValueExpectedErrorString;
const char* fWrongValueErrorString;
char* fValue;
};
class EnumeratedStringValueSetting : public StringValueSetting {
// string setting, values that do not match string enumeration
// are rejected
public:
EnumeratedStringValueSetting(const char *name, const char *defaultValue,
const char *const *values, const char *valueExpectedErrorString,
const char *wrongValueErrorString);
public:
EnumeratedStringValueSetting(const char* name, const char* defaultValue,
const char *const *values, const char* valueExpectedErrorString,
const char* wrongValueErrorString);
void ValueChanged(const char *newValue);
virtual const char *Handle(const char *const *argv);
void ValueChanged(const char* newValue);
virtual const char* Handle(const char *const *argv);
protected:
const char *const *values;
char *value;
protected:
const char *const *fValues;
char* fValue;
};
class ScalarValueSetting : public SettingsArgvDispatcher {
// simple int32 setting
public:
ScalarValueSetting(const char *name, int32 defaultValue,
const char *valueExpectedErrorString, const char *wrongValueErrorString,
ScalarValueSetting(const char* name, int32 defaultValue,
const char* valueExpectedErrorString, const char* wrongValueErrorString,
int32 min = LONG_MIN, int32 max = LONG_MAX);
void ValueChanged(int32 newValue);
int32 Value() const;
void GetValueAsString(char *) const;
virtual const char *Handle(const char *const *argv);
void GetValueAsString(char*) const;
virtual const char* Handle(const char *const *argv);
protected:
virtual void SaveSettingValue(Settings *);
virtual void SaveSettingValue(Settings*);
virtual bool NeedsSaving() const;
int32 defaultValue;
int32 value;
int32 max;
int32 min;
const char *valueExpectedErrorString;
const char *wrongValueErrorString;
int32 fDefaultValue;
int32 fValue;
int32 fMax;
int32 fMin;
const char* fValueExpectedErrorString;
const char* fWrongValueErrorString;
};
class BooleanValueSetting : public ScalarValueSetting {
// on-off setting
public:
BooleanValueSetting(const char *name, bool defaultValue);
public:
BooleanValueSetting(const char* name, bool defaultValue);
bool Value() const;
virtual const char *Handle(const char *const *argv);
bool Value() const;
virtual const char* Handle(const char *const *argv);
protected:
virtual void SaveSettingValue(Settings *);
protected:
virtual void SaveSettingValue(Settings *);
};
#endif
#endif // SETTINGS_H
+203 -154
View File
@@ -1,73 +1,101 @@
#include <Directory.h>
#include <Entry.h>
#include <FindDirectory.h>
#include <File.h>
#include <Path.h>
#include <StopWatch.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <Debug.h>
#include "SettingsHandler.h"
ArgvParser::ArgvParser(const char *name)
: file(0),
buffer(0),
pos(-1),
argc(0),
currentArgv(0),
currentArgsPos(-1),
sawBackslash(false),
eatComment(false),
inDoubleQuote(false),
inSingleQuote(false),
lineNo(0),
fileName(name)
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Debug.h>
#include <Directory.h>
#include <Entry.h>
#include <File.h>
#include <FindDirectory.h>
#include <Path.h>
#include <StopWatch.h>
#if 0
static int
Compare(const SettingsArgvDispatcher* p1, const SettingsArgvDispatcher* p2)
{
file = fopen(fileName, "r");
if (!file) {
PRINT(("Error opening %s\n", fileName));
return strcmp(p1->Name(), p2->Name());
}
#endif
#if 0
static int
CompareByNameOne(const SettingsArgvDispatcher* item1,
const SettingsArgvDispatcher* item2)
{
return strcmp(item1->Name(), item2->Name());
}
#endif
/*! \class ArgvParser
ArgvParser class opens a text file and passes the context in argv
format to a specified handler
*/
ArgvParser::ArgvParser(const char* name)
:
fFile(0),
fBuffer(0),
fPos(-1),
fArgc(0),
fCurrentArgv(0),
fCurrentArgsPos(-1),
fSawBackslash(false),
fEatComment(false),
fInDoubleQuote(false),
fInSingleQuote(false),
fLineNo(0),
fFileName(name)
{
fFile = fopen(fFileName, "r");
if (!fFile) {
PRINT(("Error opening %s\n", fFileName));
return;
}
buffer = new char [kBufferSize];
currentArgv = new char * [1024];
fBuffer = new char [kBufferSize];
fCurrentArgv = new char* [1024];
}
ArgvParser::~ArgvParser()
{
delete [] buffer;
delete[] fBuffer;
MakeArgvEmpty();
delete [] currentArgv;
delete [] fCurrentArgv;
if (file)
fclose(file);
if (fFile)
fclose(fFile);
}
void
ArgvParser::MakeArgvEmpty()
{
// done with current argv, free it up
for (int32 index = 0; index < argc; index++)
delete currentArgv[index];
for (int32 index = 0; index < fArgc; index++)
delete fCurrentArgv[index];
argc = 0;
fArgc = 0;
}
status_t
ArgvParser::SendArgv(ArgvHandler argvHandlerFunc, void *passThru)
ArgvParser::SendArgv(ArgvHandler argvHandlerFunc, void* passThru)
{
if (argc) {
if (fArgc) {
NextArgv();
currentArgv[argc] = 0;
const char *result = (argvHandlerFunc)(argc, currentArgv, passThru);
fCurrentArgv[fArgc] = 0;
const char *result = (argvHandlerFunc)(fArgc, fCurrentArgv, passThru);
if (result)
printf("File %s; Line %ld # %s", fileName, lineNo, result);
printf("File %s; Line %ld # %s", fFileName, fLineNo, result);
MakeArgvEmpty();
if (result)
return B_ERROR;
@@ -76,54 +104,60 @@ ArgvParser::SendArgv(ArgvHandler argvHandlerFunc, void *passThru)
return B_NO_ERROR;
}
void
ArgvParser::NextArgv()
{
if (sawBackslash) {
currentArgs[++currentArgsPos] = '\\';
sawBackslash = false;
if (fSawBackslash) {
fCurrentArgs[++fCurrentArgsPos] = '\\';
fSawBackslash = false;
}
currentArgs[++currentArgsPos] = '\0';
// terminate current arg pos
fCurrentArgs[++fCurrentArgsPos] = '\0';
// terminate current arg pos
// copy it as a string to the current argv slot
currentArgv[argc] = new char [strlen(currentArgs) + 1];
strcpy(currentArgv[argc], currentArgs);
currentArgsPos = -1;
argc++;
fCurrentArgv[fArgc] = new char [strlen(fCurrentArgs) + 1];
strcpy(fCurrentArgv[fArgc], fCurrentArgs);
fCurrentArgsPos = -1;
fArgc++;
}
void
ArgvParser::NextArgvIfNotEmpty()
{
if (!sawBackslash && currentArgsPos < 0)
if (!fSawBackslash && fCurrentArgsPos < 0)
return;
NextArgv();
}
char
ArgvParser::GetCh()
{
if (pos < 0 || buffer[pos] == 0) {
if (file == 0)
if (fPos < 0 || fBuffer[fPos] == 0) {
if (fFile == 0)
return EOF;
if (fgets(buffer, kBufferSize, file) == 0)
if (fgets(fBuffer, kBufferSize, fFile) == 0)
return EOF;
pos = 0;
fPos = 0;
}
return buffer[pos++];
return fBuffer[fPos++];
}
status_t
ArgvParser::EachArgv(const char *name, ArgvHandler argvHandlerFunc, void *passThru)
ArgvParser::EachArgv(const char* name, ArgvHandler argvHandlerFunc, void* passThru)
{
ArgvParser parser(name);
return parser.EachArgvPrivate(name, argvHandlerFunc, passThru);
}
status_t
ArgvParser::EachArgvPrivate(const char *name, ArgvHandler argvHandlerFunc, void *passThru)
ArgvParser::EachArgvPrivate(const char* name, ArgvHandler argvHandlerFunc,
void* passThru)
{
status_t result;
@@ -131,7 +165,7 @@ ArgvParser::EachArgvPrivate(const char *name, ArgvHandler argvHandlerFunc, void
char ch = GetCh();
if (ch == EOF) {
// done with file
if (inDoubleQuote || inSingleQuote) {
if (fInDoubleQuote || fInSingleQuote) {
printf("File %s # unterminated quote at end of file\n", name);
result = B_ERROR;
break;
@@ -142,15 +176,15 @@ ArgvParser::EachArgvPrivate(const char *name, ArgvHandler argvHandlerFunc, void
if (ch == '\n' || ch == '\r') {
// handle new line
eatComment = false;
if (!sawBackslash && (inDoubleQuote || inSingleQuote)) {
printf("File %s ; Line %ld # unterminated quote\n", name, lineNo);
fEatComment = false;
if (!fSawBackslash && (fInDoubleQuote || fInSingleQuote)) {
printf("File %s ; Line %ld # unterminated quote\n", name, fLineNo);
result = B_ERROR;
break;
}
lineNo++;
if (sawBackslash) {
sawBackslash = false;
fLineNo++;
if (fSawBackslash) {
fSawBackslash = false;
continue;
}
// end of line, flush all argv
@@ -160,11 +194,11 @@ ArgvParser::EachArgvPrivate(const char *name, ArgvHandler argvHandlerFunc, void
continue;
}
if (eatComment)
if (fEatComment)
continue;
if (!sawBackslash) {
if (!inDoubleQuote && !inSingleQuote) {
if (!fSawBackslash) {
if (!fInDoubleQuote && !fInSingleQuote) {
if (ch == ';') {
// semicolon is a command separator, pass on the whole argv
result = SendArgv(argvHandlerFunc, passThru);
@@ -173,47 +207,52 @@ ArgvParser::EachArgvPrivate(const char *name, ArgvHandler argvHandlerFunc, void
continue;
} else if (ch == '#') {
// ignore everything on this line after this character
eatComment = true;
fEatComment = true;
continue;
} else if (ch == ' ' || ch == '\t') {
// space or tab separates the individual arg strings
NextArgvIfNotEmpty();
continue;
} else if (!sawBackslash && ch == '\\') {
} else if (!fSawBackslash && ch == '\\') {
// the next character is escaped
sawBackslash = true;
fSawBackslash = true;
continue;
}
}
if (!inSingleQuote && ch == '"') {
if (!fInSingleQuote && ch == '"') {
// enter/exit double quote handling
inDoubleQuote = !inDoubleQuote;
fInDoubleQuote = !fInDoubleQuote;
continue;
}
if (!inDoubleQuote && ch == '\'') {
if (!fInDoubleQuote && ch == '\'') {
// enter/exit single quote handling
inSingleQuote = !inSingleQuote;
fInSingleQuote = !fInSingleQuote;
continue;
}
} else {
// we just pass through the escape sequence as is
currentArgs[++currentArgsPos] = '\\';
sawBackslash = false;
fCurrentArgs[++fCurrentArgsPos] = '\\';
fSawBackslash = false;
}
currentArgs[++currentArgsPos] = ch;
fCurrentArgs[++fCurrentArgsPos] = ch;
}
return result;
}
SettingsArgvDispatcher::SettingsArgvDispatcher(const char *name)
: name(name)
// #pragma mark -
SettingsArgvDispatcher::SettingsArgvDispatcher(const char* name)
:
fName(name)
{
}
void
SettingsArgvDispatcher::SaveSettings(Settings *settings, bool onlyIfNonDefault)
SettingsArgvDispatcher::SaveSettings(Settings* settings, bool onlyIfNonDefault)
{
if (!onlyIfNonDefault || NeedsSaving()) {
settings->Write("%s ", Name());
@@ -222,8 +261,9 @@ SettingsArgvDispatcher::SaveSettings(Settings *settings, bool onlyIfNonDefault)
}
}
bool
SettingsArgvDispatcher::HandleRectValue(BRect &result, const char *const *argv,
SettingsArgvDispatcher::HandleRectValue(BRect &result, const char* const *argv,
bool printError)
{
if (!*argv) {
@@ -253,158 +293,165 @@ SettingsArgvDispatcher::HandleRectValue(BRect &result, const char *const *argv,
return true;
}
void
SettingsArgvDispatcher::WriteRectValue(Settings *setting, BRect rect)
SettingsArgvDispatcher::WriteRectValue(Settings* setting, BRect rect)
{
setting->Write("%d %d %d %d", (int32)rect.left, (int32)rect.top,
(int32)rect.right, (int32)rect.bottom);
}
#if 0
static int
CompareByNameOne(const SettingsArgvDispatcher *item1, const SettingsArgvDispatcher *item2)
{
return strcmp(item1->Name(), item2->Name());
}
#endif
Settings::Settings(const char *filename, const char *settingsDirName)
: fileName(filename),
settingsDir(settingsDirName),
list(0),
count(0),
listSize(30),
currentSettings(0)
// #pragma mark -
/*! \class Settings
this class represents a list of all the settings handlers, reads and
saves the settings file
*/
Settings::Settings(const char* filename, const char* settingsDirName)
:
fFileName(filename),
fSettingsDir(settingsDirName),
fList(0),
fCount(0),
fListSize(30),
fCurrentSettings(0)
{
#ifdef SINGLE_SETTING_FILE
settingsHandler = this;
#endif
list = (SettingsArgvDispatcher **)calloc(listSize, sizeof(SettingsArgvDispatcher *));
fList = (SettingsArgvDispatcher**)calloc(fListSize, sizeof(SettingsArgvDispatcher *));
}
Settings::~Settings()
{
for (int32 index = 0; index < count; index++)
delete list[index];
for (int32 index = 0; index < fCount; index++)
delete fList[index];
free(list);
free(fList);
}
const char *
Settings::ParseUserSettings(int, const char *const *argv, void *castToThis)
const char*
Settings::_ParseUserSettings(int, const char* const *argv, void* castToThis)
{
if (!*argv)
return 0;
#ifdef SINGLE_SETTING_FILE
Settings *settings = settingsHandler;
Settings* settings = settingsHandler;
#else
Settings *settings = (Settings *)castToThis;
Settings* settings = (Settings*)castToThis;
#endif
SettingsArgvDispatcher *handler = settings->Find(*argv);
SettingsArgvDispatcher* handler = settings->_Find(*argv);
if (!handler)
return "unknown command";
return handler->Handle(argv);
}
#if 0
static int
Compare(const SettingsArgvDispatcher *p1, const SettingsArgvDispatcher *p2)
{
return strcmp(p1->Name(), p2->Name());
}
#endif
bool
Settings::Add(SettingsArgvDispatcher *setting)
/*!
Returns false if argv dispatcher with the same name already
registered
*/
bool
Settings::Add(SettingsArgvDispatcher* setting)
{
// check for uniqueness
if (Find(setting->Name()))
if (_Find(setting->Name()))
return false;
if (count >= listSize) {
listSize += 30;
list = (SettingsArgvDispatcher **)realloc(list,
listSize * sizeof(SettingsArgvDispatcher *));
if (fCount >= fListSize) {
fListSize += 30;
fList = (SettingsArgvDispatcher **)realloc(fList,
fListSize * sizeof(SettingsArgvDispatcher *));
}
list[count++] = setting;
fList[fCount++] = setting;
return true;
}
SettingsArgvDispatcher *
Settings::Find(const char *name)
SettingsArgvDispatcher*
Settings::_Find(const char* name)
{
for (int32 index = 0; index < count; index++)
if (strcmp(name, list[index]->Name()) == 0)
return list[index];
for (int32 index = 0; index < fCount; index++)
if (strcmp(name, fList[index]->Name()) == 0)
return fList[index];
return 0;
}
void
void
Settings::TryReadingSettings()
{
BPath prefsPath;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &prefsPath, true) == B_OK) {
prefsPath.Append(settingsDir);
prefsPath.Append(fSettingsDir);
BPath path(prefsPath);
path.Append(fileName);
ArgvParser::EachArgv(path.Path(), Settings::ParseUserSettings, this);
path.Append(fFileName);
ArgvParser::EachArgv(path.Path(), Settings::_ParseUserSettings, this);
}
}
void
Settings::SaveSettings(bool onlyIfNonDefault)
{
ASSERT(SettingsHandler());
SettingsHandler()->SaveCurrentSettings(onlyIfNonDefault);
SettingsHandler()->_SaveCurrentSettings(onlyIfNonDefault);
}
void
Settings::MakeSettingsDirectory(BDirectory *resultingSettingsDir)
Settings::_MakeSettingsDirectory(BDirectory *resultingSettingsDir)
{
BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path, true) != B_OK)
return;
// make sure there is a directory
path.Append(settingsDir);
path.Append(fSettingsDir);
mkdir(path.Path(), 0777);
resultingSettingsDir->SetTo(path.Path());
}
void
Settings::SaveCurrentSettings(bool onlyIfNonDefault)
{
BDirectory settingsDir;
MakeSettingsDirectory(&settingsDir);
if (settingsDir.InitCheck() != B_OK)
void
Settings::_SaveCurrentSettings(bool onlyIfNonDefault)
{
BDirectory fSettingsDir;
_MakeSettingsDirectory(&fSettingsDir);
if (fSettingsDir.InitCheck() != B_OK)
return;
printf("+++++++++++ Settings::SaveCurrentSettings %s\n", fileName);
printf("+++++++++++ Settings::_SaveCurrentSettings %s\n", fFileName);
// nuke old settings
BEntry entry(&settingsDir, fileName);
BEntry entry(&fSettingsDir, fFileName);
entry.Remove();
BFile prefs(&entry, O_RDWR | O_CREAT);
if (prefs.InitCheck() != B_OK)
return;
currentSettings = &prefs;
for (int32 index = 0; index < count; index++) {
list[index]->SaveSettings(this, onlyIfNonDefault);
fCurrentSettings = &prefs;
for (int32 index = 0; index < fCount; index++) {
fList[index]->SaveSettings(this, onlyIfNonDefault);
}
currentSettings = 0;
fCurrentSettings = 0;
}
void
Settings::Write(const char *format, ...)
Settings::Write(const char* format, ...)
{
va_list args;
@@ -413,15 +460,17 @@ Settings::Write(const char *format, ...)
va_end(args);
}
void
Settings::VSWrite(const char *format, va_list arg)
Settings::VSWrite(const char* format, va_list arg)
{
char buffer[2048];
vsprintf(buffer, format, arg);
ASSERT(currentSettings && currentSettings->InitCheck() == B_OK);
currentSettings->Write(buffer, strlen(buffer));
ASSERT(fCurrentSettings && fCurrentSettings->InitCheck() == B_OK);
fCurrentSettings->Write(buffer, strlen(buffer));
}
#ifdef SINGLE_SETTING_FILE
Settings *Settings::settingsHandler = 0;
Settings* Settings::settingsHandler = 0;
#endif
+103 -97
View File
@@ -1,11 +1,14 @@
#ifndef __SETTINGS_FILE__
#define __SETTINGS_FILE__
#ifndef SETTINGS_HANDLER_H
#define SETTINGS_HANDLER_H
#include <SupportDefs.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <SupportDefs.h>
class BFile;
class BDirectory;
@@ -13,136 +16,139 @@ class BRect;
class Settings;
typedef const char *(*ArgvHandler)(int argc, const char *const *argv, void *params);
typedef const char* (*ArgvHandler)(int argc, const char *const *argv, void *params);
// return 0 or error string if parsing failed
const int32 kBufferSize = 1024;
class ArgvParser {
// this class opens a text file and passes the context in argv
// format to a specified handler
public:
static status_t EachArgv(const char *name,
ArgvHandler argvHandlerFunc, void *passThru);
public:
static status_t EachArgv(const char* name,
ArgvHandler argvHandlerFunc, void* passThru);
private:
ArgvParser(const char *name);
~ArgvParser();
private:
ArgvParser(const char* name);
~ArgvParser();
status_t EachArgvPrivate(const char *name,
ArgvHandler argvHandlerFunc, void *passThru);
char GetCh();
status_t SendArgv(ArgvHandler argvHandlerFunc, void *passThru);
// done with a whole line of argv, send it off and get ready
// to build a new one
status_t EachArgvPrivate(const char* name,
ArgvHandler argvHandlerFunc, void* passThru);
char GetCh();
void NextArgv();
// done with current string, get ready to start building next
void NextArgvIfNotEmpty();
// as above, don't commint current string if empty
status_t SendArgv(ArgvHandler argvHandlerFunc, void* passThru);
// done with a whole line of argv, send it off and get ready
// to build a new one
void MakeArgvEmpty();
void NextArgv();
// done with current string, get ready to start building next
void NextArgvIfNotEmpty();
// as above, don't commint current string if empty
FILE *file;
char *buffer;
int32 pos;
int32 numAvail;
int argc;
char **currentArgv;
void MakeArgvEmpty();
int32 currentArgsPos;
char currentArgs [1024];
FILE* fFile;
char* fBuffer;
int32 fPos;
int32 fNumAvail;
bool sawBackslash;
bool eatComment;
bool inDoubleQuote;
bool inSingleQuote;
int fArgc;
char** fCurrentArgv;
int32 lineNo;
const char *fileName;
int32 fCurrentArgsPos;
char fCurrentArgs[1024];
bool fSawBackslash;
bool fEatComment;
bool fInDoubleQuote;
bool fInSingleQuote;
int32 fLineNo;
const char* fFileName;
};
class SettingsArgvDispatcher {
// base class for a single setting item
public:
SettingsArgvDispatcher(const char *name);
// base class for a single setting item
public:
SettingsArgvDispatcher(const char* name);
void SaveSettings(Settings *settings, bool onlyIfNonDefault);
void SaveSettings(Settings* settings, bool onlyIfNonDefault);
const char *Name() const
{ return name; }
// name as it appears in the settings file
virtual const char *Handle(const char *const *argv) = 0;
// override this adding an argv parser that reads in the
// values in argv format for this setting
// return a pointer to an error message or null if parsed OK
const char* Name() const
{
return fName;
}
virtual const char* Handle(const char* const *argv) = 0;
// override this adding an argv parser that reads in the
// values in argv format for this setting
// return a pointer to an error message or null if parsed OK
// some handy reader/writer calls
bool HandleRectValue(BRect &, const char *const *argv, bool printError = true);
// static bool HandleColorValue(rgb_color &, const char *const *argv, bool printError = true);
void WriteRectValue(Settings *, BRect);
// void WriteColorValue(BRect);
bool HandleRectValue(BRect&, const char* const *argv,
bool printError = true);
protected:
virtual void SaveSettingValue(Settings *settings) = 0;
// override this to save the current value of this setting in a
// text format
virtual bool NeedsSaving() const
{ return true; }
// static bool HandleColorValue(rgb_color &, const char *const *argv, bool printError = true);
void WriteRectValue(Settings*, BRect);
// void WriteColorValue(BRect);
protected:
virtual void SaveSettingValue(Settings* settings) = 0;
// override this to save the current value of this setting in a
// text format
virtual bool NeedsSaving() const
{
return true;
}
// override to return false if current value is equal to the default
// and does not need saving
private:
const char *name;
private:
const char* fName;
};
class Settings {
// this class is a list of all the settings handlers, reads and
// saves the settings file
public:
Settings(const char *filename, const char *settingsDirName);
~Settings();
void TryReadingSettings();
void SaveSettings(bool onlyIfNonDefault = true);
public:
Settings(const char* filename, const char* settingsDirName);
~Settings();
void TryReadingSettings();
void SaveSettings(bool onlyIfNonDefault = true);
#ifdef SINGLE_SETTING_FILE
static Settings *SettingsHandler()
{ return settingsHandler; }
static Settings* SettingsHandler()
{
return settingsHandler;
}
#else
Settings *SettingsHandler()
{ return this; }
Settings* SettingsHandler()
{
return this;
}
#endif
bool Add(SettingsArgvDispatcher *);
// return false if argv dispatcher with the same name already
// registered
bool Add(SettingsArgvDispatcher *);
void Write(const char *format, ...);
void VSWrite(const char *, va_list);
void Write(const char* format, ...);
void VSWrite(const char*, va_list);
#ifdef SINGLE_SETTING_FILE
static Settings *settingsHandler;
static Settings* settingsHandler;
#endif
private:
void MakeSettingsDirectory(BDirectory *);
private:
void _MakeSettingsDirectory(BDirectory*);
SettingsArgvDispatcher *Find(const char *);
static const char *ParseUserSettings(int, const char *const *argv, void *);
void SaveCurrentSettings(bool onlyIfNonDefault);
SettingsArgvDispatcher* _Find(const char*);
static const char* _ParseUserSettings(int, const char *const *argv, void*);
void _SaveCurrentSettings(bool onlyIfNonDefault);
const char *fileName;
const char *settingsDir;
SettingsArgvDispatcher **list;
int32 count;
int32 listSize;
BFile *currentSettings;
const char* fFileName;
const char* fSettingsDir;
SettingsArgvDispatcher** fList;
int32 fCount;
int32 fListSize;
BFile* fCurrentSettings;
};
#endif
#endif // SETTINGS_HANDLER_H
File diff suppressed because it is too large Load Diff
+123 -151
View File
@@ -1,176 +1,148 @@
// Copyright (c) 1998-99, Be Incorporated, All Rights Reserved.
// SMS
/* VideoConsumer.h */
#ifndef VIDEO_CONSUMER_H
#define VIDEO_CONSUMER_H
#if !defined(VID_CONSUMER_H)
#define VID_CONSUMER_H
#include <View.h>
#include <Bitmap.h>
#include <Window.h>
#include <MediaNode.h>
#include <TranslationKit.h>
#include <BufferConsumer.h>
#include <TimedEventQueue.h>
#include <MediaEventLooper.h>
#include <MediaNode.h>
#include <TimedEventQueue.h>
#include <TranslationKit.h>
#include <View.h>
#include <Window.h>
typedef struct
{
port_id port;
bigtime_t rate;
uint32 imageFormat;
int32 translator;
bool passiveFtp;
char fileNameText[64];
char serverText[64];
char loginText[64];
char passwordText[64];
char directoryText[64];
typedef struct {
port_id port;
bigtime_t rate;
uint32 imageFormat;
int32 translator;
bool passiveFtp;
char fileNameText[64];
char serverText[64];
char loginText[64];
char passwordText[64];
char directoryText[64];
} ftp_msg_info;
#define FTP_INFO 0x60000001
#define FTP_INFO 0x60000001
class VideoConsumer :
public BMediaEventLooper,
public BBufferConsumer
{
public:
VideoConsumer(
const char * name,
BView * view,
BStringView * statusLine,
BMediaAddOn *addon,
const uint32 internal_id);
~VideoConsumer();
/* BMediaNode */
public:
virtual BMediaAddOn *AddOn(long *cookie) const;
protected:
class BStringView;
virtual void Start(bigtime_t performance_time);
virtual void Stop(bigtime_t performance_time, bool immediate);
virtual void Seek(bigtime_t media_time, bigtime_t performance_time);
virtual void TimeWarp(bigtime_t at_real_time, bigtime_t to_performance_time);
virtual void NodeRegistered();
virtual status_t RequestCompleted(
const media_request_info & info);
class VideoConsumer : public BMediaEventLooper, public BBufferConsumer {
public:
VideoConsumer(const char* name, BView* view, BStringView* statusLine,
BMediaAddOn *addon, const uint32 internalId);
~VideoConsumer();
/* BMediaNode */
public:
virtual BMediaAddOn* AddOn(long* cookie) const;
protected:
virtual void Start(bigtime_t performanceTime);
virtual void Stop(bigtime_t performanceTime, bool immediate);
virtual void Seek(bigtime_t mediaTime, bigtime_t performanceTime);
virtual void TimeWarp(bigtime_t atRealTime,
bigtime_t toPerformanceTime);
virtual void NodeRegistered();
virtual status_t RequestCompleted(const media_request_info& info);
virtual status_t HandleMessage(int32 message, const void* data,
size_t size);
virtual status_t DeleteHook(BMediaNode* node);
/* BMediaEventLooper */
protected:
virtual void HandleEvent(const media_timed_event* event,
bigtime_t lateness, bool realTimeEvent);
/* BBufferConsumer */
public:
virtual status_t AcceptFormat(const media_destination& dest,
media_format* format);
virtual status_t GetNextInput(int32* cookie, media_input* outInput);
virtual void DisposeInputCookie(int32 cookie);
protected:
virtual void BufferReceived(BBuffer* buffer);
private:
virtual void ProducerDataStatus(const media_destination &forWhom,
int32 status, bigtime_t atMediaTime);
virtual status_t GetLatencyFor(const media_destination& forWhom,
bigtime_t* outLatency, media_node_id* outId);
virtual status_t Connected(const media_source& producer,
const media_destination& where, const media_format& withFormat,
media_input* outInput);
virtual void Disconnected(const media_source& producer,
const media_destination& where);
virtual status_t FormatChanged(const media_source& producer,
const media_destination& consumer, int32 fromChangeCount,
const media_format& format);
/* implementation */
public:
status_t CreateBuffers(const media_format& withFormat);
void DeleteBuffers();
static status_t FtpRun(void* data);
void FtpThread();
virtual status_t HandleMessage(
int32 message,
const void * data,
size_t size);
void UpdateFtpStatus(char* status);
virtual status_t DeleteHook(BMediaNode * node);
status_t LocalSave(char* filename, BBitmap* bitmap);
/* BMediaEventLooper */
protected:
virtual void HandleEvent(
const media_timed_event *event,
bigtime_t lateness,
bool realTimeEvent);
/* BBufferConsumer */
public:
virtual status_t AcceptFormat(
const media_destination &dest,
media_format * format);
virtual status_t GetNextInput(
int32 * cookie,
media_input * out_input);
virtual void DisposeInputCookie(
int32 cookie);
protected:
status_t FtpSave(char* filename);
virtual void BufferReceived(
BBuffer * buffer);
private:
private:
BStringView* fStatusLine;
uint32 fInternalID;
BMediaAddOn* fAddOn;
virtual void ProducerDataStatus(
const media_destination &for_whom,
int32 status,
bigtime_t at_media_time);
virtual status_t GetLatencyFor(
const media_destination &for_whom,
bigtime_t * out_latency,
media_node_id * out_id);
virtual status_t Connected(
const media_source &producer,
const media_destination &where,
const media_format & with_format,
media_input * out_input);
virtual void Disconnected(
const media_source &producer,
const media_destination &where);
virtual status_t FormatChanged(
const media_source & producer,
const media_destination & consumer,
int32 from_change_count,
const media_format & format);
/* implementation */
thread_id fFtpThread;
public:
status_t CreateBuffers(
const media_format & with_format);
void DeleteBuffers();
static status_t FtpRun(
void *data);
void FtpThread(
void);
void UpdateFtpStatus(
char *status);
status_t LocalSave(
char *filename,
BBitmap *bitmap);
bool fConnectionActive;
media_input fIn;
media_destination fDestination;
bigtime_t fMyLatency;
status_t FtpSave(
char *filename);
BWindow* fWindow;
BView* fView;
BBitmap* fBitmap[3];
bool fOurBuffers;
BBufferGroup* fBuffers;
uint32 fBufferMap[3];
private:
BBitmap* fFtpBitmap;
volatile bool fTimeToFtp;
volatile bool fFtpComplete;
BStringView * mStatusLine;
uint32 mInternalID;
BMediaAddOn *mAddOn;
thread_id mFtpThread;
bool mConnectionActive;
media_input mIn;
media_destination mDestination;
bigtime_t mMyLatency;
BWindow *mWindow;
BView *mView;
BBitmap *mBitmap[3];
bool mOurBuffers;
BBufferGroup *mBuffers;
uint32 mBufferMap[3];
BBitmap *mFtpBitmap;
volatile bool mTimeToFtp;
volatile bool mFtpComplete;
bigtime_t mRate;
uint32 mImageFormat;
int32 mTranslator;
bool mPassiveFtp;
char mFileNameText[64];
char mServerText[64];
char mLoginText[64];
char mPasswordText[64];
char mDirectoryText[64];
bigtime_t fRate;
uint32 fImageFormat;
int32 fTranslator;
bool fPassiveFtp;
char fFileNameText[64];
char fServerText[64];
char fLoginText[64];
char fPasswordText[64];
char fDirectoryText[64];
};
#endif
#endif // VIDEO_CONSUMER_H