?? sendsm.cpp
字號:
/* Module: SMGP API File Name: SendSM.cpp Class Name: Version: 1.3 Author: Eric Xu Create Date: 2002-6-4 11:54 Last Modify Date: 2002-6-4 11:54 Description: Send batch or single short message according parameters.*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>#ifdef _WIN32#include <io.h>#include <winsock2.h>#else#include <unistd.h>#include <ctype.h>#endif#include "smgpdef.h"#include "SMGPAPI.hpp"#define SEND_MODE_SINGLE 0#define SEND_MODE_BATCH 1typedef struct{ char sMsgType[1+1]; int nNeedReply; int nMsgLevel; char sServiceID[10+1]; int nMsgFormat; char sFeeType[2+1]; char sFeeCode[6+1]; char sFixedFee[6+1]; char sValidTime[17+1]; char sAtTime[17+1]; char sSrcTermID[21+1]; char sChargeTermID[21+1]; char sDestTermID[21+1]; char sDestTermIDFile[1024+1]; char nMsgClass; char sMsgFile[1024+1]; char sMsgIDFile[1024+1]; char sReserved[8 +1];} SMArgument;void Usage(char *sProgram);int ReadArgumentFile(char *sFile, SMArgument *pSMArgument, int nOutput);int main(int argc, char ** argv){ int nSendMode = SEND_MODE_SINGLE, nOutput = 1; char sArgFile[1024 +1] = ""; SMArgument theSMArgument; char sMsgID[10+1] ={0}; int nErrorCode; FILE *pFile; int nMsgLen, n; char sMsgContent[10240 +1] ={0}; //char sChargeTermID[21+1] = ""; int nFeeUserType = 0; int nRepeat = 0; // get argument for(n=1; n<argc; ++n) { if(strcmp(argv[n], "-B") == 0) { nSendMode = SEND_MODE_BATCH; } else if(strcmp(argv[n], "-S") == 0) { nSendMode = SEND_MODE_SINGLE; } else if(strcmp(argv[n], "-n") == 0) { nOutput = 0; } else if(strcmp(argv[n], "-a") == 0) { n++; strncpy(sArgFile, argv[n], 1024); sArgFile[1024] = '\0'; } else if(strcmp(argv[n], "-u") == 0) { n++; nFeeUserType = atoi(argv[n]); } else if(strcmp(argv[n], "-r") == 0) { n++; nRepeat = atoi(argv[n]); }// else if(strcmp(argv[n], "-c") == 0)// {// n++;// strncpy(sChargeTermID, argv[n], 21);// sChargeTermID[21] = '\0' ;// } else { if(nOutput == 1) { Usage(argv[0]); } exit(1); } } if(strlen(sArgFile) == 0) { if(nOutput == 1) { printf("No argument file!\n"); } exit(1); } // read arguments from file if(ReadArgumentFile(sArgFile, &theSMArgument, nOutput) != 0) { if(nOutput == 1) { printf("Fail to read argument from file %s!\n", sArgFile); } exit(1); } else { if(nOutput == 1) { printf("need_reply=%d!\n", theSMArgument.nNeedReply); printf("msg_level=%d!\n", theSMArgument.nMsgLevel); printf("serviceid=%s!\n", theSMArgument.sServiceID); printf("msg_format=%d!\n", theSMArgument.nMsgFormat); printf("fee_type=%s!\n", theSMArgument.sFeeType); printf("fee_code=%s!\n", theSMArgument.sFeeCode); printf("fixed_fee=%s!\n", theSMArgument.sFixedFee); printf("valid_time=%s!\n", theSMArgument.sValidTime); printf("at_time=%s!\n", theSMArgument.sAtTime); printf("src_termid=%s!\n", theSMArgument.sSrcTermID ); printf("charge_termid=%s!\n", theSMArgument.sChargeTermID); printf("dest_termid=%s!\n", theSMArgument.sDestTermID); printf("dest_termid_file=%s!\n",theSMArgument.sDestTermIDFile); printf("reserved=%s!\n", theSMArgument.sReserved); if( theSMArgument.nMsgClass == 0) //if pass filename to sendbatch { printf("msg_file=%s!\n", theSMArgument.sMsgFile); } else { printf("msg_content=%s!\n", theSMArgument.sMsgFile); printf("msg_len=%d!\n", strlen(theSMArgument.sMsgFile) ); } printf("msgid_file=%s!\n", theSMArgument.sMsgIDFile); } } // initiate API if(InitSMGPAPI() != 0) { if(nOutput == 1) { printf("Fail to call InitSMGPAPI!\n"); } exit(1); } for(int iTimes = 0; iTimes<= nRepeat; iTimes++ ) if(nSendMode == SEND_MODE_SINGLE) { // get message from file struct stat sbuf; if(stat(theSMArgument.sMsgFile, &sbuf) != 0 ) { nMsgLen = -1; } else { nMsgLen = sbuf.st_size; } if(nMsgLen <= 0) { if(nOutput == 1) { printf("Fail to read message file %s!\n", theSMArgument.sMsgFile); } exit(1); } pFile = fopen(theSMArgument.sMsgFile, "rb"); fread(sMsgContent, nMsgLen, 1, pFile); fclose(pFile); // remove unseen character at the end of the sMsgContent, no binary format if(theSMArgument.nMsgFormat != 4) { sMsgContent[nMsgLen] = 0; for(n=nMsgLen-1; n>0; --n) { if((unsigned char)sMsgContent[n] <= '\r') { sMsgContent[n] = 0; } else { break; } } nMsgLen = strlen(sMsgContent); } if(nOutput == 1) { printf("msg_len=%d!\n", nMsgLen); } if(SMGPSendSingle( theSMArgument.nNeedReply, theSMArgument.nMsgLevel, theSMArgument.sServiceID, theSMArgument.nMsgFormat, theSMArgument.sFeeType, theSMArgument.sFeeCode, theSMArgument.sFixedFee, theSMArgument.sValidTime, theSMArgument.sAtTime, theSMArgument.sSrcTermID , theSMArgument.sChargeTermID, theSMArgument.sDestTermID , nMsgLen, sMsgContent, (char*)sMsgID, &nErrorCode, theSMArgument.sMsgType, theSMArgument.sReserved ) != 0) { if(nOutput == 1) { printf("Fail to call SMGPSendSingle, error=%d!\n", nErrorCode); } exit(1); } // write msgid and error code to file else { pFile = fopen(theSMArgument.sMsgIDFile, "w"); if(pFile != NULL) { fprintf(pFile, "%s\t%d\t%s\n", sMsgID, nErrorCode,theSMArgument.sDestTermID); fclose(pFile); } else { if(nOutput == 1) { printf("Fail to open file %s!\n", theSMArgument.sMsgIDFile); } } } } else { if( SMGPSendBatch( theSMArgument.nNeedReply, theSMArgument.nMsgLevel, theSMArgument.sServiceID, theSMArgument.nMsgFormat, theSMArgument.sFeeType, theSMArgument.sFeeCode, theSMArgument.sFixedFee, theSMArgument.sValidTime, theSMArgument.sAtTime, theSMArgument.sSrcTermID, theSMArgument.sChargeTermID, theSMArgument.sDestTermIDFile , (theSMArgument.nMsgClass == 0)? 0 : strlen(theSMArgument.sMsgFile) , theSMArgument.sMsgFile, theSMArgument.sMsgIDFile, theSMArgument.sMsgType, theSMArgument.sReserved ) != 0) { if(nOutput == 1) { printf("Fail to call SMGPSendBatch!\n"); } exit(1); } } printf("%s send message ok.\n",argv[0]); exit(0); return(0);}void Usage(char *sProgram){ printf("Usage: %s [-B]|[-S][-n] <-a argument_file> [-u fee_user_type]\n", sProgram); printf(" -B: send one short message to multiple users\n"); printf(" -S: send one short message to single user\n"); printf(" -a: argument file\n"); printf(" -n: no message output\n"); printf(" -u: fee_user_type, default is 0\n"); printf("e.g.: %s -S -a sm.arg # send single short message defined in file sm.arg\n", sProgram); printf("e.g.: %s -B -a sm.arg # send batch short message defined in file sm.arg\n", sProgram);}int ReadArgumentFile(char *sFile, SMArgument *pSMArgument, int nOutput){ FILE *pFile; char sLine[10240 +1]; int n, nLen, m; pFile = fopen(sFile, "r"); if(pFile == NULL) return(1); memset(pSMArgument,0 , sizeof(SMArgument)); for(n=0; n<17; ++n) { if(fgets(sLine, 10240, pFile) == NULL) { if(nOutput == 1) { printf("Fail to read no.%d parameter in file %s!\n", n+1, sFile); } fclose(pFile); return(1); } // remove '\n' and '\r' nLen = strlen(sLine); for(m=nLen-1; m>=0; --m) { if(sLine[m] == '\n' || sLine[m] == '\r') { sLine[m] = 0; } break; }// printf("%d=%s\n",n,sLine); switch(n) { case 0: if (strlen(sLine)>1) sLine[1]=0; strcpy(pSMArgument->sMsgType,sLine); break; case 1: pSMArgument->nNeedReply = atoi(sLine); break; case 2: pSMArgument->nMsgLevel = atoi(sLine); break; case 3: if(strlen(sLine) > 10) sLine[10] = 0; strcpy(pSMArgument->sServiceID, sLine); break; case 4: pSMArgument->nMsgFormat = atoi(sLine); break; case 5: if(strlen(sLine) > 2) sLine[2] = 0; strcpy(pSMArgument->sFeeType, sLine); break; case 6: if(strlen(sLine) > 6) sLine[6] = 0; strcpy(pSMArgument->sFeeCode, sLine); break; case 7: if (strlen(sLine)>6) sLine[6]=0; strcpy(pSMArgument->sFixedFee,sLine); break; case 8: if(strlen(sLine) > 17) sLine[17] = 0; strcpy(pSMArgument->sValidTime, sLine); break; case 9: if(strlen(sLine) > 17) sLine[17] = 0; strcpy(pSMArgument->sAtTime, sLine); break; case 10: if(strlen(sLine) > 21) sLine[21] = 0; strcpy(pSMArgument->sSrcTermID , sLine); break; case 11: if(strlen(sLine) > 21) sLine[21] = 0; strcpy(pSMArgument->sChargeTermID , sLine); break; case 12: if(strlen(sLine) > 21) sLine[21] = 0; strcpy(pSMArgument->sDestTermID, sLine); break; case 13: strncpy(pSMArgument->sDestTermIDFile, sLine,1024); break; case 14: pSMArgument->nMsgClass = atoi(sLine); break; case 15: strncpy(pSMArgument->sMsgFile, sLine,1024); break; case 16: strncpy(pSMArgument->sMsgIDFile, sLine, 1024); break; case 17: strncpy(pSMArgument->sReserved, sLine, 8); break; } } fclose(pFile); return(0);}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -