📄 mrptr.c
字号:
/*
* Copyright (C) Obigo AB, 2002-2005.
* All rights reserved.
*
* This software is covered by the license agreement between
* the end user and Obigo AB, and may be
* used and copied only in accordance with the terms of the
* said agreement.
*
* Obigo AB assumes no responsibility or
* liability for any errors or inaccuracies in this software,
* or any consequential, incidental or indirect damage arising
* out of the use of the software.
*
*/
#include "cansilib.h"
#include "cmnconf.h"
#include "aapifile.h"
#include "aapicmn.h"
#include "gmem.h"
#include "mmstypes.h"
#include "mmsconf.h"
#include "aapimms.h"
#include "mmem.h"
#define MAX_ALLOCATED_MEM MMS_MAX_CHUNK_SIZE
#define REPORT_FILESIZE (INT32)(sizeof(OneRowInFile) * MAX_NUM_OF_REPORT_STORE)
#define FILE_NAME_REPORT 0
typedef enum
{
FIND_MESSAGE_ID,
FIND_MSGID,
FIND_FREE_ROW
} searchAttr;
typedef struct
{
MmsMsgId msgId;
char messageId[MAX_MESSAGE_ID + 1];
unsigned char lineCtrl;
} OneRowInFile;
static unsigned char* mrptrAlloc(INT32 fileSize, INT32 *bufSize);
static void mrpthInitFile(void);
static void mrpthFileClose(void);
static int mrpthFileOpen(void);
static INT32 mrpthFileRead(void* buf, const INT32 lengthOfBuf, INT32 pos);
static INT32 mrpthFileWrite(void* buf, const INT32 lengthToWrite, INT32 pos);
static INT32 mrpthGetNextFree(const unsigned char *buf, const INT32 buflen,
MmsMsgId msgId);
static INT32 mrpthFindMessageId(unsigned char* buf, const INT32 buflen,
MmsMsgId msgId);
static INT32 mrpthFindMsgId(unsigned char* buf, const INT32 buflen,
const char* messageId);
static INT32 mrptFindRow(searchAttr keyDef, OneRowInFile *row);
static unsigned char* mrptrAlloc(INT32 fileSize, INT32 *bufSize)
{
if ((UINT32)fileSize > MAX_ALLOCATED_MEM)
{
*bufSize = ((UINT32)MAX_ALLOCATED_MEM / sizeof(OneRowInFile))
* sizeof(OneRowInFile);
}
else
{
*bufSize = fileSize;
}
return (unsigned char*)M_CALLOC((UINT32)*bufSize);
}
static void mrpthInitFile(void)
{
INT32 bufSize = 0;
INT32 writedBytes = 0;
INT32 bytesToWrite = 0;
INT32 pos = 0;
unsigned char *buf;
if ( FILEa_setSize(MMS_FILE_CATEGORY_RPT, FILE_NAME_REPORT,
REPORT_FILESIZE) == -1)
{
MMS_LOG_I(("%s(%d): error - the size of the file could not be set!\n",
__FILE__, __LINE__));
return;
}
buf = mrptrAlloc( REPORT_FILESIZE, &bufSize);
bytesToWrite = bufSize;
do
{
writedBytes = mrpthFileWrite(buf, bytesToWrite, pos);
pos += writedBytes;
if ((REPORT_FILESIZE - writedBytes) < bufSize)
{
bytesToWrite = (REPORT_FILESIZE - writedBytes);
}
} while (writedBytes != -1 && REPORT_FILESIZE > pos);
M_FREE(buf);
}
static void mrpthFileClose(void)
{
FILEa_close(MMS_FILE_CATEGORY_RPT, FILE_NAME_REPORT);
}
static int mrpthFileOpen(void)
{
INT32 fileSize;
if (FILEa_open(MMS_FILE_CATEGORY_RPT, FILE_NAME_REPORT,
FILE_OPTION_WRITE | FILE_OPTION_READ) == -1)
{
MMS_LOG_I(("%s(%d): Failed to open the report file %d %d\n",
__FILE__, __LINE__, MMS_FILE_CATEGORY_RPT, FILE_NAME_REPORT));
return -1;
}
fileSize = FILEa_getSize(MMS_FILE_CATEGORY_RPT, FILE_NAME_REPORT);
if (fileSize < REPORT_FILESIZE)
{
mrpthInitFile();
}
return 0;
}
static INT32 mrpthFileRead(void* buf, const INT32 lengthOfBuf, INT32 pos)
{
return FILEa_read(MMS_FILE_CATEGORY_RPT, FILE_NAME_REPORT,
buf, pos, lengthOfBuf);
}
static INT32 mrpthFileWrite(void* buf, const INT32 lengthToWrite, INT32 pos)
{
return FILEa_write(MMS_FILE_CATEGORY_RPT, FILE_NAME_REPORT,
buf, pos, lengthToWrite);
}
static INT32 mrpthGetNextFree(const unsigned char *buf, const INT32 buflen,
MmsMsgId msgId)
{
INT32 tmpPos = 0;
INT32 firstFree = -1;
OneRowInFile *row;
row = (OneRowInFile*)buf;
while (tmpPos < buflen)
{
if (row->msgId == msgId)
{
return tmpPos;
}
if (row->lineCtrl == 0 && firstFree == -1 )
{
firstFree = tmpPos;
}
tmpPos += (INT32)sizeof(OneRowInFile);
row++;
}
return firstFree;
}
static INT32 mrpthFindMessageId(unsigned char* buf, const INT32 buflen,
MmsMsgId msgId)
{
INT32 tmpPos = 0;
OneRowInFile *row;
row = (OneRowInFile*)buf;
while (tmpPos < buflen)
{
if ((row->lineCtrl != 0) && (row->msgId == msgId))
{
break;
}
tmpPos += (INT32)sizeof(OneRowInFile);
row++;
}
if (tmpPos < buflen && (row->lineCtrl != 0))
{
return tmpPos;
}
else
{
return -1;
}
}
static INT32 mrpthFindMsgId(unsigned char* buf, const INT32 buflen,
const char* messageId)
{
INT32 tmpPos = 0;
OneRowInFile *row;
row = (OneRowInFile*)buf;
while (tmpPos < buflen)
{
if ((row->lineCtrl != 0)
&& (strcmp(row->messageId, messageId) == 0))
{
break;
}
tmpPos += (INT32)sizeof(OneRowInFile);
row++;
}
if (tmpPos < buflen && (row->lineCtrl != 0))
{
return tmpPos;
}
else
{
return -1;
}
}
static INT32 mrptFindRow(searchAttr keyDef, OneRowInFile *row)
{
INT32 fileSize = 0;
INT32 bufSize = 0;
INT32 readBytes = 0;
INT32 pos = 0;
INT32 foundAtPos = 0;
INT32 memPos = 0;
unsigned char *buf = NULL;
fileSize = FILEa_getSize(MMS_FILE_CATEGORY_RPT, FILE_NAME_REPORT);
if (fileSize <= 0)
{
MMS_LOG_I(("%s(%d): invalid size (%ld) for the file.\n",
__FILE__, __LINE__, fileSize));
return -1;
}
buf = mrptrAlloc(fileSize, &bufSize);
do {
readBytes = mrpthFileRead(buf, bufSize, pos);
if (readBytes <= 0)
{
MMS_LOG_I(("%s(%d): Couldn't read %ld bytes (%ld)\n",
__FILE__, __LINE__, bufSize, readBytes));
M_FREE(buf);
return -1;
}
switch (keyDef)
{
case FIND_MESSAGE_ID:
if ((foundAtPos = mrpthFindMessageId(buf, readBytes, row->msgId)) != -1)
{
memPos = foundAtPos;
foundAtPos += pos;
}
else
{
pos += readBytes;
}
break;
case FIND_MSGID:
if ((foundAtPos = mrpthFindMsgId(buf, readBytes, row->messageId)) != -1)
{
memPos = foundAtPos;
foundAtPos += pos;
}
else
{
pos += readBytes;
}
break;
case FIND_FREE_ROW:
if ((foundAtPos = mrpthGetNextFree(buf, readBytes,row->msgId)) != -1)
{
memPos = foundAtPos;
foundAtPos += pos;
}
else
{
pos += readBytes;
}
break;
default:
M_FREE(buf);
MMS_LOG_I(("%s(%d): Wrong parameter %d is used\n",
__FILE__, __LINE__, keyDef));
return -1;
}
} while ((foundAtPos == -1) && (pos < fileSize));
if (pos >= fileSize)
{
foundAtPos = -1;
}
else if (foundAtPos != -1 && (foundAtPos <
(fileSize - (INT32)sizeof(OneRowInFile))))
{
memcpy(row, buf + memPos, sizeof(OneRowInFile));
}
M_FREE(buf);
return foundAtPos;
}
INT32 mrpthAddMessageId(const MmsMsgId msgId, const char* messageId)
{
INT32 foundAtPos = 0;
INT32 ret = -1;
OneRowInFile newRow;
MMS_LOG_I(("mrpthAddMessageId: Add report info to file msgId: %lu MessageId: %s\n",
msgId, messageId == NULL ? "NULL" : messageId));
if (messageId == NULL)
{
MMS_LOG_I(("%s(%d): No data to store.\n",
__FILE__, __LINE__));
return 0;
}
if (mrpthFileOpen() == -1)
{
MMS_LOG_I(("%s(%d): file already opened or created\n",
__FILE__, __LINE__));
return -1;
}
newRow.msgId = msgId;
foundAtPos = mrptFindRow(FIND_FREE_ROW, &newRow);
if (foundAtPos != -1 )
{
memset(&newRow,0, sizeof(OneRowInFile));
newRow.msgId = msgId;
strncpy( newRow.messageId, messageId, MAX_MESSAGE_ID);
newRow.lineCtrl = 1;
ret = mrpthFileWrite(&newRow, sizeof(OneRowInFile), foundAtPos);
if (ret == (INT32)sizeof(OneRowInFile))
{
ret = 0;
}
else
{
MMS_LOG_I(("%s(%d): failed to write requested bytes \n",
__FILE__, __LINE__));
ret = -1;
}
}
else
{
MMS_LOG_I(("%s(%d): No free row in the report file \n",
__FILE__, __LINE__));
ret = -1;
MMSa_error(MMS_RESULT_REPORT_FILE_IS_FULL);
}
mrpthFileClose();
return ret;
}
INT32 mrpthGetMsgId(const char* messageId)
{
INT32 foundAtPos = 0;
OneRowInFile newRow;
if (messageId == NULL)
{
return -1;
}
if (mrpthFileOpen() == -1)
{
MMS_LOG_I(("%s(%d): error - could not open file\n",
__FILE__, __LINE__));
return -1;
}
memset(&newRow, 0, sizeof(OneRowInFile));
strncpy(newRow.messageId, messageId, MAX_MESSAGE_ID);
foundAtPos = mrptFindRow(FIND_MSGID, &newRow);
mrpthFileClose();
if (foundAtPos != -1)
{
return (INT32)newRow.msgId;
}
return -1;
}
INT32 mrpthRemove(const MmsMsgId msgId)
{
INT32 foundAtPos = 0;
INT32 ret = -1;
OneRowInFile newRow;
if (mrpthFileOpen() == -1)
{
MMS_LOG_I(("%s(%d): the file could be open or created \n",
__FILE__, __LINE__));
return -1;
}
newRow.msgId = msgId;
newRow.messageId[0] = '\0';
foundAtPos = mrptFindRow(FIND_MESSAGE_ID, &newRow);
if (foundAtPos != -1 )
{
memset(&newRow,0, sizeof(OneRowInFile));
ret = mrpthFileWrite(&newRow, sizeof(OneRowInFile), foundAtPos);
if (ret != -1 && ret == (INT32)sizeof(OneRowInFile))
{
ret = 0;
}
else
{
MMS_LOG_I(("%s(%d): REPORTHANDLER error - failed to write requested bytes \n",
__FILE__, __LINE__));
ret = -1;
}
}
else
{
ret = -1;
}
mrpthFileClose();
return ret;
}
void mrpthFileDelete(void)
{
if (FILEa_open(MMS_FILE_CATEGORY_RPT, FILE_NAME_REPORT,
FILE_OPTION_WRITE | FILE_OPTION_READ) == -1)
{
MMS_LOG_I(("%s(%d): Failed to open the report file %d %d\n",
__FILE__, __LINE__, MMS_FILE_CATEGORY_RPT, FILE_NAME_REPORT));
return;
}
mrpthInitFile();
mrpthFileClose();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -