![]() |
| | #1 |
| Registered User Join Date: Jan 2004
Posts: 49
| Converting a C structure into a C++ class Code:
#define SIOC_SERIAL _IOR('S', 254, struct scsi_serial)
struct scsi_serial{
ubit8 device_type; // SCSI Device Type.
ubit8 page_code; // SCSI Page Code.
ubit8 reserved; // SCSI Reserved.
ubit8 page_length; // SCSI Page Length.
ubit8 serial[252]; // SCSI Serial Number.
};
The closest that I have been able to come is as follows: Code: /**************************************************
**
** Filename: cdskinfo2.h
**
** Purpose:
**
**************************************************/
#ifndef _CDSKINFO2_H_
#define _CDSKINFO2_H_
#include <string>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#define SIOC_SERIAL _IOR('S', 254, struct scsi_serial)
struct scsi_serial{
ubit8 device_type; // SCSI Device Type.
ubit8 page_code; // SCSI Page Code.
ubit8 reserved; // SCSI Reserved.
ubit8 page_length; // SCSI Page Length.
ubit8 serial[252]; // SCSI Serial Number.
};
class CdskInfo{
public:
/* Data Members */
char device_name[256]; // SCSI Device Name.
struct scsi_serial sn;
// Constructor
CdskInfo( char *s );
/* Public Methods */
void print_device_name();
void dump();
int open_device();
};
#endif
Code: /**************************************************
**
** Filename: cdskinfo2.c
**
** Purpose:
**
**************************************************/
#include "cdskinfo2.h"
CdskInfo::CdskInfo( char *s ){
int fd, ret;
strcpy( device_name, s );
open_device();
}
void CdskInfo::print_device_name(){
printf ( "SCSI_DEVICE_NAME=%s\n", device_name );
}
void CdskInfo::dump(){
printf ( "SCSI_DEVICE_NAME=%s\n", device_name );
printf ( "SCSI_SERIAL_NUMBER=%s\n", sn.serial );
}
int CdskInfo::open_device ( ){
int fd, ret;
if ( ( fd = open( device_name, O_RDONLY | O_NDELAY ) ) <= 0 ){
perror( "Open Error" );
return ( -1 );
}
if ( ( ret = ioctl( fd, SIOC_SERIAL, &sn ) ) != 0 ){
perror( "SIOC_SERIAL Error" );
return ( -1 );
}
return ( ret );
}
Code: /**************************************************
**
** Filename: cdskinfo_main.cc
**
** Purpose:
**
**************************************************/
#include <iostream.h>
#include <cstdlib>
#include "cdskinfo2.h"
int main ( int argc, char *argv[] ) {
CdskInfo sserial( argv[1] );
sserial.print_device_name();
sserial.dump();
exit( 0 );
}
Can anyone help me in solving this problem. Thanks in advance, DeadPoet |
| deadpoet is offline | |
| | #2 |
| Registered User Join Date: Mar 2003
Posts: 3,903
| Consider the following: Code: #include <iostream>
#include <cstring>
using namespace std;
struct Test
{
char c;
int i;
char str[20];
float f;
};//Test
void print_Test(const Test *t)
{
cout << "c = " << t->c << endl
<< "i = " << t->i << endl
<< "f = " << t->f << endl
<< "str = " << t->str << endl;
}//print_Test
class CTest : public Test
{
public:
CTest()
{
c = 'X';
i = 69;
f = 2.5;
strcpy(str, "Hello");
}//constructor
void print()
{
print_Test(this);
}//print
};//CTest
int main()
{
CTest t;
t.print();
print_Test(&t);
return 0;
}//main
|
| Codeplug is offline | |
| | #3 |
| Registered User Join Date: Nov 2001
Posts: 1,348
| Specifically, what is the difficulty with ioctl()? Kuphryn |
| kuphryn is offline | |
| | #4 |
| Registered User Join Date: Jan 2004
Posts: 49
| Kuphryn and All Others, Consider the following code changes from the original posted source--All modifications are noted by CHANGE_MADE_HERE. Code: /*************************************************
**
** Filename: cdskinfo3.h
**
** Purpose:
**
*************************************************/
#ifndef _CDSKINFO2_H_
#define _CDSKINFO2_H_
#include <string>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/scsi.h>
/* CHANGE_MADE_HERE */
#define SIOC_SERIAL _IOR('S', 254, class CdskInfo)
/* CHANGE_MADE_HERE */
/* Structure commented and now defined as Data Members */
/*
struct scsi_serial{
ubit8 device_type; // SCSI Device Type.
ubit8 page_code; // SCSI Page Code.
ubit8 reserved; // SCSI Reserved.
ubit8 page_length; // SCSI Page Length.
ubit8 serial[252]; // SCSI Serial Number.
};
*/
class CdskInfo{
public:
/* Data Members */
char device_name[256]; // SCSI Device Name.
/* CHANGE_MADE_HERE */
// struct scsi_serial sn;
/* CHANGE_MADE_HERE */
ubit8 device_type; // SCSI Device Type.
ubit8 page_code; // SCSI Page Code.
ubit8 reserved; // SCSI Reserved.
ubit8 page_length; // SCSI Page Length.
ubit8 serial[252]; // SCSI Serial Number.
// Constructor
CdskInfo( char *s );
/* Public Methods */
void print_device_name();
void dump();
int open_device();
};
#endif
Code: /*************************************************
**
** Filename: cdskinfo3.c
**
** Purpose:
**
*************************************************/
#include "cdskinfo2.h"
CdskInfo::CdskInfo( char *s ){
int fd, ret;
strcpy( device_name, s );
open_device();
}
void CdskInfo::print_device_name(){
printf ( "SCSI_DEVICE_NAME=%s\n", device_name );
}
void CdskInfo::dump(){
printf ( "SCSI_DEVICE_NAME=%s\n", device_name );
printf ( "SCSI_SERIAL_NUMBER=%s\n", serial );
}
int CdskInfo::open_device ( ){
int fd, ret;
if ( ( fd = open( device_name, O_RDONLY | O_NDELAY ) ) <= 0 ){
perror( "Open Error" );
return ( -1 );
}
/* CHANGE_MADE_HERE */
/* ioctl Now does not open the device */
if ( ( ret = ioctl( fd, CSIOC_SERIAL, &(*this) ) ) != 0 ){
perror( "SIOC_SERIAL ERROR" );
return ( -1 );
}
return ( ret );
}
Code: /*************************************************
**
** Filename: cdskinfo_main.cc
**
** Purpose:
**
*************************************************/
#include <iostream.h>
#include <cstdlib> // for exit()
#include "cdskinfo2.h"
int main ( int argc, char *argv[] ) {
CdskInfo sserial( argv[1] );
cout << "*****DEBUG*****" << endl;
sserial.print_device_name();
sserial.dump();
exit( 0 );
}
Any help is greatly appreciated. Thanks, DeadPoet |
| deadpoet is offline | |
| | #5 |
| Registered User Join Date: Jan 2004
Posts: 49
| One more update: CSIOC_SERIAL should be Code: /* CHANGE_MADE_HERE */
/* ioctl Now does not open the device */
if ( ( ret = ioctl( fd, SIOC_SERIAL, &(*this) ) ) != 0 ){
perror( "SIOC_SERIAL ERROR" );
return ( -1 );
}
DeadPoet |
| deadpoet is offline | |
| | #6 |
| Registered User Join Date: Mar 2003
Posts: 3,903
| Put device_name after all the original scsi_serial members in CdskInfo. The memory layout of CdskInfo and scsi_serial must be identical for the first sizeof(scsi_serial) bytes. gg |
| Codeplug is offline | |
| | #7 |
| Registered User Join Date: Jan 2004
Posts: 49
| Cool , If I remove the device_name then It does compile and run, without the device_name. However, I need the device_name and I need to add additional structures into this class. Any ideas ?Thanks You are the MAN, DeadPoet |
| deadpoet is offline | |
| | #8 | |
| Registered User Join Date: Mar 2003
Posts: 3,903
| Here's an idea... Quote:
gg | |
| Codeplug is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Getting an error with OpenGL: collect2: ld returned 1 exit status | Lorgon Jortle | C++ Programming | 6 | 05-08-2009 08:18 PM |
| Structure vs. Class | bobbelPoP | C++ Programming | 4 | 07-09-2008 09:44 AM |
| Need help to build network class | weeb0 | C++ Programming | 0 | 02-01-2006 11:33 AM |
| Mmk, I give up, lets try your way. (Resource Management) | Shamino | Game Programming | 31 | 01-18-2006 09:54 AM |
| Serial Communications in C | ExDigit | Windows Programming | 7 | 01-09-2002 10:52 AM |