标题: 如何把这个程序改成linux用的啊
sqwzhandsome
新生入学
Rank: 1



UID 207420
精华 0
积分 10(积分有什么用?)
帖子 1
阅读权限 10
注册 2007-8-10
状态 离线
发表于 2007-8-10 17:12  资料  个人空间  短消息  加为好友 
如何把这个程序改成linux用的啊



#include <windows.h>
#include <winsock.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>

SOCKET call_socket(const char *hostname, unsigned short portnum);
void printLogoFile(FILE *printer, int logoNum);
void printLogoNetwork(SOCKET s, int logoNum);
void printReceiptFile(FILE *printer, char buffer[4096], int bufferLength);
void printReceiptNetwork(SOCKET s, char buffer[4096], int bufferLength);
void cutPaperFile(FILE *printer);
void cutPaperNetwork(SOCKET s);
void printMarketingFile(FILE *printer, char *message);
void printMarketingNetwork(SOCKET s, char *message, int messageLen);
void enqueue(char c, char* q);
int getClientName(char *clientName);
int getHostName(char *hostname);

int main() {

int sections;
int i, i2;
int marketingSetup[64];
int portnum;
int bufferLength = 0;
int numReceipts = 1;
int tempStringLen;
int logoSetup[64];
char c;
char buffer[4096];
char dataString[1024];
char emptyString[64] = "\0";
char marketingString[64];
char marketingPath[64][64];
char tempString[1024];
char sectionString[64];
char sectionSetup[64];
char hostname[64];
char portType[2];
char q[5] = "0000";
char filter[4];
FILE *marketingFile;
FILE *printer;
WSADATA wsaData;
SOCKET s;

numReceipts = GetPrivateProfileInt("Setup", "numberCopies", 1, "tmPrint.ini");
GetPrivateProfileString("Setup", "portType", emptyString, portType, 2, "tmPrint.ini");
GetPrivateProfileString("Setup", "hostname", emptyString, hostname, 64, "tmPrint.ini");
portnum = GetPrivateProfileInt("Setup", "hostport", 9100, "tmPrint.ini");
sections = GetPrivateProfileInt("PrintSections", "number", -1, "tmPrint.ini");
GetPrivateProfileString("Setup", "filter", emptyString, filter, 4, "tmPrint.ini");

/*printf("numberCopies:  %d\n", numReceipts);
printf("portType:      %s\n", portType);
printf("hostname:      %s\n", hostname);
printf("hostport:      %d\n", portnum);
printf("sections:      %d\n", sections);
printf("filter:        %s\n", filter);*/

for(i=0; i<sections; i++) {
        sprintf(sectionString, "PrintSection%d", i+1);

        if(GetPrivateProfileInt(sectionString, "logo", -1, "tmPrint.ini") > 0) {
                sectionSetup[i] = 'l';
                logoSetup[i] = GetPrivateProfileInt(sectionString, "logo", -1, "tmPrint.ini");
        }
        else if(GetPrivateProfileInt(sectionString, "marketing", -1, "tmPrint.ini") > 0) {
                sectionSetup[i] = 'm';
                marketingSetup[i] = GetPrivateProfileInt(sectionString, "marketing", -1, "tmPrint.ini");
        }
        else if(GetPrivateProfileInt(sectionString, "text", -1, "tmPrint.ini") > 0)
                sectionSetup[i] = 'r';
        else if(GetPrivateProfileInt(sectionString, "cut", -1, "tmPrint.ini") > 0)
                sectionSetup[i] = 'c';
}

if(toupper(portType[0]) == 'P') {
        if ((printer = fopen("LPT1", "w")) == NULL) {
                printf("Error opening Printer");
                exit(1);
        }
}
else if(toupper(portType[0]) == 'S') {
        if((printer = fopen("COM1", "w")) == NULL) {
                printf("Error opening printer");
                exit(1);
        }
}
else if(toupper(portType[0]) == 'N') {
        if (WSAStartup(MAKEWORD(1,1), &wsaData ) != 0 ) {
                printf("Could not find a usable WinSock DLL.\n");
                exit(1);
        }

        s = call_socket(hostname, portnum);
        if(s == INVALID_SOCKET) {
                printf("connect() failed");
                exit(1);
        }
}
else if(toupper(portType[0]) == 'T') {
        portnum = getHostName(hostname);
        portType[0] = 'N';
        if (WSAStartup(MAKEWORD(1,1), &wsaData ) != 0 ) {
                printf("Could not find a usable WinSock DLL.\n");
                exit(1);
        }

        s = call_socket(hostname, portnum);
        if(s == INVALID_SOCKET) {
                printf("connect() failed");
                exit(1);
        }
}

/* buffer the input */
while(!feof(stdin)) {
        if(toupper(filter[0]) == 'Y') {
                scanf("%c", &c);
                enqueue(c, q);
                if(q[3]==0x4A && q[2]=='\e' && q[4]==0xC0)
                        buffer[bufferLength++] = 0x18;
                else if(q[3]==0x4A && q[2]=='\e' && q[4]==0xD0)
                        buffer[bufferLength++] = 0x18;
                else
                        buffer[bufferLength++] = c;
        }
        else {
                scanf("%c", &c);
                buffer[bufferLength++] = c;
        }
}       

for(i2=0; i2<numReceipts; i2++) {

                /* 1C  70  n  m  -  print NV image
                        n = image number
                        m = image mode
                                0        normal
                                1        double-width
                                2        double height
                                4        quadruple

                   1B  64  n - print and feed n lines
                   1D  56  m - select cut mode and cut paper
                        m = cut mode
                                0        full cut
                                1        partial cut
                   0D  -  CR
                   00  -  null
                */


        for(i=0; i<sections; i++) {
                switch(sectionSetup[i]) {
                        case 'l':
                                if(toupper(portType[0]) == 'S' || toupper(portType[0]) == 'P')
                                        printLogoFile(printer, logoSetup[i]);
                                else if(toupper(portType[0]) == 'N')
                                        printLogoNetwork(s, logoSetup[i]);
                                break;
                        case 'm':
                                sprintf(marketingString, "Marketing%d", marketingSetup[i]);
                                GetPrivateProfileString(marketingString, "path", emptyString, marketingPath[i], 64, "tmPrint.ini");
                                marketingFile = fopen(marketingPath[i], "r");
                                tempStringLen = 0;
                                while(!feof(marketingFile)) {
                                        fscanf(marketingFile, "%c", &c);
                                        tempString[tempStringLen++] = c;
                                }       
                                tempString[tempStringLen++] = '\0';
                                if(toupper(portType[0]) == 'S' || toupper(portType[0]) == 'P')
                                        printMarketingFile(printer, tempString);
                                else if(toupper(portType[0]) == 'N')
                                        printMarketingNetwork(s, tempString, tempStringLen);
                                break;
                        case 'r':
                                if(toupper(portType[0]) == 'S' || toupper(portType[0]) == 'P')
                                        printReceiptFile(printer, buffer, bufferLength);
                                else if(toupper(portType[0]) == 'N')
                                        printReceiptNetwork(s, buffer, bufferLength);
                                break;
                        case 'c':
                                if(toupper(portType[0]) == 'S' || toupper(portType[0]) == 'P')
                                        cutPaperFile(printer);
                                else if(toupper(portType[0]) == 'N')
                                        cutPaperNetwork(s);
                                break;
                }
        }
}

if(toupper(portType[0]) == 'N') {
        closesocket(s);
        WSACleanup();
}

return 0;

}

void printLogoFile(FILE *printer, int logoNum) {
        fprintf(printer,"%c%c%c%c%c%c%c%c",0x1C,0x70,logoNum,0x00,0x0D,0x00,0x00,0x00);
}

void printLogoNetwork(SOCKET s, int logoNum) {
        char dataString[1024];
        sprintf(dataString,"%c%c%c%c%c%c%c%c",0x1C,0x70,logoNum,0x00,0x0D,0x00,0x00,0x00);
        if (send(s, dataString, 8, 0) != 8) {
                printf("send() sent a different number of bytes than expected");
                exit(1);
        }
}

void printReceiptFile(FILE *printer, char buffer[4096], int bufferLength) {
        int i;

        for(i = 0; i<bufferLength; i++) {
                fprintf(printer, "%c", buffer[i]);
        }
}

void printReceiptNetwork(SOCKET s, char buffer[4096], int bufferLength) {
        if (send(s, buffer, bufferLength, 0) != bufferLength) {
                printf("send() sent a different number of bytes than expected");
                exit(1);
        }
}

void cutPaperFile(FILE *printer) {
        fprintf(printer,"%c%c%c%c%c%c",0x1B,0x64,0x05,0x1D,0x56,0x00);

}

void cutPaperNetwork(SOCKET s) {
        char dataString[1024];

        sprintf(dataString,"%c%c%c%c%c%c",0x1B,0x64,0x05,0x1D,0x56,0x00);
        if (send(s, dataString, 6, 0) != 6) {
                printf("send() sent a different number of bytes than expected");
                exit(1);
        }
}

void printMarketingFile(FILE *printer, char *message) {
        fprintf(printer, "%s", message);
}

void printMarketingNetwork(SOCKET s, char *message, int messageLen) {
        if (send(s, message, messageLen, 0) != messageLen) {
                printf("send() sent a different number of bytes than expected");
                exit(1);
        }
}

SOCKET call_socket(const char *hostname, unsigned short portnum) {
        struct sockaddr_in sa;
        struct hostent *hp;
        SOCKET s;
        hp = gethostbyname(hostname);
       
        if (hp == NULL) /* we don't know who this host is */
                return INVALID_SOCKET;
       
        memset(&sa,0,sizeof(sa));
        memcpy((char *)&sa.sin_addr, hp->h_addr, hp->h_length); /* set address */
        sa.sin_family = hp->h_addrtype;
        sa.sin_port = htons((u_short)portnum);
        s = socket(hp->h_addrtype, SOCK_STREAM, 0);
       
        if (s == INVALID_SOCKET)
                return INVALID_SOCKET; /* try to connect to the specified socket */
        if (connect(s, (struct sockaddr *)&sa, sizeof sa) == SOCKET_ERROR) {
                closesocket(s);
                return INVALID_SOCKET;
        }
       
        return s;
}

void enqueue(char c, char* q) {
        q[0] = q[1];
        q[1] = q[2];
        q[2] = q[3];
        q[3] = q[4];
        q[4] = c;
}

int getHostName(char *hostname) {
        int portNum;
        char clientName[16] = "\0";
        char emptyString[64] = "\0";

        getClientName(clientName);

        GetPrivateProfileString(clientName, "hostname", emptyString, hostname, 63, "tmPrint.ini");
        portNum = GetPrivateProfileInt(clientName, "hostport", 9100, "tmPrint.ini");

        return portNum;
}


int getClientName(char *clientName) {

        char *userName;
        char userFilePath[64];
        char emptyString[64] = "\0";
        FILE *userFile;

        GetPrivateProfileString("Setup", "userFilePath", emptyString, userFilePath, 63, "tmPrint.ini");
        userName = getenv("REDMON_USER");
        strcat(userFilePath, userName);
        userFile = fopen(userFilePath, "r");
        if(userFile == NULL) {
                printf("Unable to open the %s file\n", userFilePath);
                exit(1);
        }
        fscanf(userFile, "%s", clientName);
        return 0;
}


顶部
不容错过的IBM 集群技术资源汇总
flag
版主
Rank: 7Rank: 7Rank: 7
版主



UID 19801
精华 1
积分 28(积分有什么用?)
帖子 5581
阅读权限 100
注册 2002-7-19
状态 离线
发表于 2007-8-13 09:30  资料  个人空间  短消息  加为好友 
把那些GetPrivateProfileString和WSACleanup这些非标准库的函数替换掉,用gcc编译一下就知道哪些是“非法函数”了





上帝说,有问题,找GOOGLE 写程序是很神圣的事情!同样只是装系统,卖菜的大娘会的事情不见得就跟卖菜一样了。
顶部
海上飞洪
新生入学
Rank: 1



UID 203813
精华 0
积分 10(积分有什么用?)
帖子 12
阅读权限 10
注册 2007-7-18
状态 离线
发表于 2007-9-2 14:35  资料  个人空间  短消息  加为好友 
#include <windows.h>
h4??
M6UN
^ J
#include <winsock.h>:
a\"XF/X'z8@O.\4Blol 这两个是MFC的哦,在LINUX是用不了的

顶部
不容错过的IBM 集群技术资源汇总
voodoo_cat
新生入学
Rank: 1
注册用户



UID 179294
精华 0
积分 0(积分有什么用?)
帖子 14
阅读权限 10
注册 2007-1-17
状态 离线
发表于 2007-9-25 22:44  资料  个人空间  短消息  加为好友 
呵呵,这个程序还好。前面的xd已经说的差不多啦。程序不是很长,自己编译一点一点看就可以了。要不了多长时间的!

顶部
 



当前时区 GMT+8, 现在时间是 2008-5-17 03:11

    Powered by Discuz! 5.5.0  © 2001-2007 Comsenz Inc.
Processed in 0.491878 second(s), 5 queries

清除 Cookies - 联系我们 - Linux伊甸园 - Archiver - WAP