Why does update not update any records? - sql

I am using the OCI functions to update a database table in Oracle but despite the commands all returning success nothing in the database table is changing. It changes if I hard code the Where clause values so I think I might be doing something wrong in the binding code?
If I create the database table as follows:
create table updatebuddTI(i char(10), j int);
insert into updatebuddTI values ('test1',1);
insert into updatebuddTI values ('test2',2);
insert into updatebuddTI values ('test3',3);
and then use the code:
#include "stdafx.h"
#include <string>
#include <oci.h>
#include <stdlib.h>
#define OCI_NOT_NULL 0
#define OCI_VALUE_NULL -1
#define OCI_VALUE_TRUNCATED -2
#define ORACLE_MAX_SESSIONS 30
int _tmain(int argc, _TCHAR* argv[]) {
// OCI handles
OCIEnv *envhp;
OCIError *errhp;
OCIServer *srvhp;
OCISvcCtx *svchp;
OCISession *authp;
OCIStmt *stmtp;
OCIDefine *defnpp;
// Connection information
text* user = (text*)"test";
text* pwd = (text*)"password";
text* sid = (text*)"oracle-server";
char *query = "UPDATE updatebuddTI SET I = 'test3' WHERE J = :1";// :2";
int dataReceivedI[10];
// Fetched data indicators, lengths and codes
ub2 dataReceived_len[10];
ub2 dataReceived_code[10];
sb2 dataReceived_indI[3];
ub2 dataReceived_lenI[3];
ub2 dataReceived_codeI[3];
oratext message[512];
sb4 errcode;
// Allocate environment
int rc = OCIEnvCreate(&envhp, OCI_DEFAULT, NULL, NULL, NULL, NULL, 0, NULL);
ub2 code = OCINlsCharSetNameToId(envhp, (const oratext *)"WE8MSWIN1252");
OCIEnv *envHandle(0);
if(code) {
rc = OCIEnvNlsCreate(&envHandle,
OCI_OBJECT | OCI_THREADED,
NULL,
NULL,
NULL,
NULL,
0,
NULL,
code,
code);
} else {
printf("problem with OCIEnvNlsCreate!\n");
}
// Allocate error handle
rc = OCIHandleAlloc(envhp, (void**)&errhp, OCI_HTYPE_ERROR, 0, NULL);
// Allocate server and service context handles
rc = OCIHandleAlloc(envhp, (void**)&srvhp, OCI_HTYPE_SERVER, 0, NULL);
rc = OCIHandleAlloc(envhp, (void**)&svchp, OCI_HTYPE_SVCCTX, 0, NULL);
// Attach to the server
//rc = OCIServerAttach(srvhp, errhp, sid, strlen((char*)sid), 0);
// Set server in the service context
rc = OCIAttrSet(svchp, OCI_HTYPE_SVCCTX, (dvoid*)srvhp, 0, OCI_ATTR_SERVER, errhp);
// Allocate session handle
rc = OCIHandleAlloc(envhp, (void**)&authp, OCI_HTYPE_SESSION, 0, NULL);
// Set user name and password
rc = OCIAttrSet(authp, OCI_HTYPE_SESSION, (void*)user, strlen((char*)user),
OCI_ATTR_USERNAME, errhp);
rc = OCIAttrSet(authp, OCI_HTYPE_SESSION, (void*)pwd, strlen((char *)pwd),
OCI_ATTR_PASSWORD, errhp);
std::string path("oracle-server");
rc = OCIServerAttach(srvhp, errhp, (text *)path.c_str(), (sb4)path.length(), 0);
// Connect
rc = OCISessionBegin(svchp, errhp, authp, OCI_CRED_RDBMS, OCI_DEFAULT);
// Set session in the service context
rc = OCIAttrSet(svchp, OCI_HTYPE_SVCCTX, authp, 0, OCI_ATTR_SESSION, errhp);
// Allocate statement handle
rc = OCIHandleAlloc(envhp, (void**)&stmtp, OCI_HTYPE_STMT, 0, NULL);
// Prepare the query
rc = OCIStmtPrepare(stmtp, errhp, (text*)query, strlen(query), OCI_NTV_SYNTAX, OCI_DEFAULT);
char text[10];
int option=0;
// Define the select list items
rc = OCIDefineByPos(stmtp, &defnpp, errhp, 1, (void*)text, 5, SQLT_CHR, (void*)dataReceivedI,
dataReceived_len, dataReceived_code, OCI_DEFAULT);
if (rc != 0) {
OCIErrorGet(errhp, (ub4)1, NULL, &errcode, message, sizeof(message), (ub4)OCI_HTYPE_ERROR);
printf("%s", message);
}
rc = OCIDefineByPos(stmtp, &defnpp, errhp, 2, (void*)option, sizeof(int), SQLT_NUM, (void*)dataReceived_indI,
dataReceived_lenI, dataReceived_codeI, OCI_DEFAULT);
if (rc != 0) {
OCIErrorGet(errhp, (ub4)1, NULL, &errcode, message, sizeof(message), (ub4)OCI_HTYPE_ERROR);
printf("%s", message);
}
OCIBind* bindHandle2;
rc = OCIBindByPos(stmtp, &bindHandle2, errhp, 1,
(dvoid *)&option, (sword) sizeof(int), SQLT_NUM,
(dvoid *)0, (ub2 *)0, (ub2 *)0, (ub4)0, (ub4 *)0, OCI_DEFAULT);
if (rc != 0) {
OCIErrorGet(errhp, (ub4)1, NULL, &errcode, message, sizeof(message), (ub4)OCI_HTYPE_ERROR);
printf("%s", message);
}
strcpy_s(text, "test3");
option = 2;
rc = OCIStmtExecute(svchp, stmtp, errhp, 1, 0, 0,0, OCI_DEFAULT);
if (rc != 0) {
OCIErrorGet(errhp, (ub4)1, NULL, &errcode, message, sizeof(message), (ub4)OCI_HTYPE_ERROR);
printf("%s", message);
}
rc = OCIHandleFree(stmtp, OCI_HTYPE_STMT);
// Disconnect
rc = OCISessionEnd(svchp, errhp, authp, OCI_DEFAULT);
rc = OCIServerDetach(srvhp, errhp, OCI_DEFAULT);
rc = OCIHandleFree(envhp, OCI_HTYPE_ENV);
}
Can anyone see what I have done wrong?

Related

Encoding H.264 Compression Session with CGDisplayStream

I'm trying to create an H.264 Compression Session with the data from my screen. I've created a CGDisplayStreamRef instance like so:
displayStream = CGDisplayStreamCreateWithDispatchQueue(0, 100, 100, k32BGRAPixelFormat, nil, self.screenCaptureQueue, ^(CGDisplayStreamFrameStatus status, uint64_t displayTime, IOSurfaceRef frameSurface, CGDisplayStreamUpdateRef updateRef) {
//Call encoding session here
});
Below is how I currently have the encoding function setup:
- (void) encode:(CMSampleBufferRef )sampleBuffer {
CVImageBufferRef imageBuffer = (CVImageBufferRef)CMSampleBufferGetImageBuffer(sampleBuffer);
CMTime presentationTimeStamp = CMTimeMake(frameID++, 1000);
VTEncodeInfoFlags flags;
OSStatus statusCode = VTCompressionSessionEncodeFrame(EncodingSession,
imageBuffer,
presentationTimeStamp,
kCMTimeInvalid,
NULL, NULL, &flags);
if (statusCode != noErr) {
NSLog(#"H264: VTCompressionSessionEncodeFrame failed with %d", (int)statusCode);
VTCompressionSessionInvalidate(EncodingSession);
CFRelease(EncodingSession);
EncodingSession = NULL;
return;
}
NSLog(#"H264: VTCompressionSessionEncodeFrame Success");
}
I'm trying to understand how I can convert the data from my screen into a CMSampleBufferRef so I can properly call my encode function. So far, I've not been able to determine if this is possible, or the right approach for what I'm trying to do. Does anyone have any suggestions?
EDIT: I've gotten my IOSurfaceconverted to a CMBlockBuffer, but haven't yet figured out how to convert that to a CMSampleBufferRef:
void *mem = IOSurfaceGetBaseAddress(frameSurface);
size_t bytesPerRow = IOSurfaceGetBytesPerRow(frameSurface);
size_t height = IOSurfaceGetHeight(frameSurface);
size_t totalBytes = bytesPerRow * height;
CMBlockBufferRef blockBuffer;
CMBlockBufferCreateWithMemoryBlock(kCFAllocatorNull, mem, totalBytes, kCFAllocatorNull, NULL, 0, totalBytes, 0, &blockBuffer);
EDIT 2
Some more progress:
CMSampleBufferRef *sampleBuffer;
OSStatus sampleStatus = CMSampleBufferCreate(
NULL, blockBuffer, TRUE, NULL, NULL,
NULL, 1, 1, NULL,
0, NULL, sampleBuffer);
[self encode:*sampleBuffer];
Possibly, I'm a bit late but nevertheless, it could be helpful for others:
CGDisplayStreamCreateWithDispatchQueue(CGMainDisplayID(), 100, 100, k32BGRAPixelFormat, nil, self.screenCaptureQueue, ^(CGDisplayStreamFrameStatus status, uint64_t displayTime, IOSurfaceRef frameSurface, CGDisplayStreamUpdateRef updateRef) {
// The created pixel buffer retains the surface object.
CVPixelBufferRef pixelBuffer;
CVPixelBufferCreateWithIOSurface(NULL, frameSurface, NULL, &pixelBuffer);
// Create the video-type-specific description for the pixel buffer.
CMVideoFormatDescriptionRef videoFormatDescription;
CMVideoFormatDescriptionCreateForImageBuffer(NULL, pixelBuffer, &videoFormatDescription);
// All the necessary parts for creating a `CMSampleBuffer` are ready.
CMSampleBufferRef sampleBuffer;
CMSampleTimingInfo timingInfo;
CMSampleBufferCreateReadyWithImageBuffer(NULL, pixelBuffer, videoFormatDescription, &timingInfo, &sampleBuffer);
// Do the stuff
// Release the resources to let the frame surface be reused in the queue
// `kCGDisplayStreamQueueDepth` is responsible for the size of the queue
CFRelease(sampleBuffer);
CFRelease(pixelBuffer);
});

OpenCL Local memory and Xcode

I'm trying to learn OpenCL on a Mac, which appears to have some differences in implementation from the OpenCL book I'm reading. I want to be able to dynamically allocate local memory on the GPU. What I'm reading is I need to use the clSetKernelArg function, but that doesn't work within Xcode 6.4. Here's the code as it stands (never mind it's a pointless program, just trying to learn the syntax for shared memory). In Xcode, the kernel is written as a stand-alone .cl file similar to CUDA, so that's a separate file.
add.cl:
kernel void add(int a, int b, global int* c, local int* d)
{
d[0] = a;
d[1] = b;
*c = d[0] + d[1];
}
main.c:
#include <stdio.h>
#include <OpenCL/opencl.h>
#include "add.cl.h"
int main(int argc, const char * argv[]) {
int a = 3;
int b = 5;
int c;
int* cptr = &c;
dispatch_queue_t queue = gcl_create_dispatch_queue(CL_DEVICE_TYPE_GPU, NULL);
void* dev_c = gcl_malloc(sizeof(cl_int), NULL, CL_MEM_WRITE_ONLY);
// attempt to create local memory buffer
void* dev_d = gcl_malloc(2*sizeof(cl_int), NULL, CL_MEM_READ_WRITE);
// clSetKernelArg(add_kernel, 3, 2*sizeof(cl_int), NULL);
dispatch_sync(queue, ^{
cl_ndrange range = { 1, {0, 0, 0}, {1, 0, 0}, {1, 0, 0} };
// This gives a warning:
// Warning: Incompatible pointer to integer conversion passing 'cl_int *'
// (aka 'int *') to parameter of type 'size_t' (aka 'unsigned long')
add_kernel(&range, a, b, (cl_int*)dev_c, (cl_int*)dev_d);
gcl_memcpy((void*)cptr, dev_c, sizeof(cl_int));
});
printf("%d + %d = %d\n", a, b, c);
gcl_free(dev_c);
dispatch_release(queue);
return 0;
}
I've tried putting clSetKernelArg where indicated and it doesn't like the first argument:
Error: Passing 'void (^)(const cl_ndrange *, cl_int, cl_int, cl_int *, size_t)' to parameter of incompatible type 'cl_kernel' (aka 'struct _cl_kernel *')
I've looked and looked but can't find any examples illustrating this point within the Xcode environment. Can you point me in the right direction?
Managed to solve this by ditching Apple's extensions and using standard OpenCL 1.2 calls. That means replacing gcl_malloc with clCreateBuffer, replacing dispatch_sync with clEnqueueNDRangeKernel, and most importantly, using clSetKernelArg with NULL in the last argument for local variables. Works like a charm.
Here's the new version:
char kernel_add[1024] =
"kernel void add(int a, int b, global int* c, local int* d) \
{\
d[0] = a;\
d[1] = b;\
*c = d[0] + d[1];\
}";
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <OpenCL/opencl.h>
int main(int argc, const char * argv[]) {
int a = 3;
int b = 5;
int c;
cl_device_id device_id;
int err = clGetDeviceIDs(NULL, CL_DEVICE_TYPE_GPU, 1, &device_id, NULL);
cl_context context = clCreateContext(0, 1, &device_id, NULL, NULL, &err);
cl_command_queue queue = clCreateCommandQueue(context, device_id, 0, &err);
const char* srccode = kernel;
cl_program program = clCreateProgramWithSource(context, 1, &srccode, NULL, &err);
err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
cl_kernel kernel = clCreateKernel(program, "kernel_add", &err);
cl_mem dev_c = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(int), NULL, NULL);
err = clSetKernelArg(kernel, 0, sizeof(int), &a);
err |= clSetKernelArg(kernel, 1, sizeof(int), &b);
err |= clSetKernelArg(kernel, 2, sizeof(cl_mem), &dev_c);
err |= clSetKernelArg(kernel, 3, sizeof(int), NULL);
size_t one = 1;
err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &one, NULL, 0, NULL, NULL);
clFinish(queue);
err = clEnqueueReadBuffer(queue, dev_c, true, 0, sizeof(int), &c, 0, NULL, NULL);
clReleaseMemObject(dev_c);
clReleaseKernel(kernel);
clReleaseProgram(program);
clReleaseCommandQueue(queue);
clReleaseContext(context);
return 0;
}
In regular OpenCL, for a kernel parameter declared as a local pointer, you don't allocate a host buffer and pass it in (like you're doing with dev_d). Instead you do a clSetKernelArg with the size of the desired local storage but a NULL pointer (like this: clSetKernelArg(kernel, 2, sizeof(cl_int) * local_work_size[0], NULL)). You'll have to translate that into the Xcode way if you insist on being platform-specific.

OpenCL - Kernel crashes on the second run

I am trying to run a code which works the first time but crashes the second time that it runs. The function which causes the crash is part of the class Octree_GPU and is this:
int Octree_GPU::runCreateNodeKernel(int length)
{
cl_uint nodeLength;
if(nodeNumsArray[length-1] == 0)
nodeLength = nodeAddArray[length-1];
else
nodeLength = nodeAddArray[length-1]+8;
nodeArray = (cl_uint*)malloc(sizeof(cl_uint)*nodeLength);
nodePointsArray = (cl_int*)malloc(sizeof(cl_uint)*nodeLength);
startIndexArray = (cl_int*)malloc(sizeof(cl_int)*nodeLength);
d_nodeAdd = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(cl_uint)*length, NULL, &err);
d_nodeArray = clCreateBuffer(context,CL_MEM_READ_WRITE, sizeof(cl_uint)*temp_length, NULL, &err);
d_numPoints = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(cl_uint)*length, NULL, &err);
d_pointIndex = clCreateBuffer(context, CL_MEM_READ_WRITE,sizeof(cl_uint)*length,NULL, &err);
d_nodePointsArray = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(cl_int)*temp_length, NULL, &err);
d_nodeIndexArray = clCreateBuffer(context,CL_MEM_READ_WRITE, sizeof(cl_int)*temp_length, NULL, &err);
err |= clEnqueueWriteBuffer(commands, d_nodeAdd, CL_TRUE, 0, sizeof(cl_uint)*length, nodeAddArray, 0, NULL,NULL);
err |= clEnqueueWriteBuffer(commands, d_numPoints,CL_TRUE, 0, sizeof(cl_uint)*length,numPointsArray,0,NULL,NULL);
err |= clEnqueueWriteBuffer(commands, d_pointIndex, CL_TRUE, 0, sizeof(cl_uint)*length,pointStartIndexArray,0, NULL, NULL);
clFinish(commands);
err = clSetKernelArg(createNodeKernel, 0, sizeof(cl_mem), &d_odata);
err |= clSetKernelArg(createNodeKernel, 1, sizeof(cl_mem), &d_nodeNums);
err |= clSetKernelArg(createNodeKernel, 2, sizeof(cl_mem), &d_nodeAdd);
err |= clSetKernelArg(createNodeKernel, 3, sizeof(cl_mem), &d_numPoints);
err |= clSetKernelArg(createNodeKernel, 4, sizeof(cl_mem), &d_pointIndex);
err |= clSetKernelArg(createNodeKernel, 5, sizeof(cl_mem), &d_nodeArray);
err |= clSetKernelArg(createNodeKernel, 6, sizeof(cl_mem), &d_nodePointsArray);
err |= clSetKernelArg(createNodeKernel, 7, sizeof(cl_mem), &d_nodeIndexArray);
clFinish(commands);
if(err != CL_SUCCESS) {
printf("Cannot set Kernel Arg \n");
exit(1);
}
size_t global_size[1] = {limit-1};
err = clEnqueueNDRangeKernel(commands, createNodeKernel, 1, NULL, global_size, NULL, 0, NULL, NULL);
if(err != CL_SUCCESS) {
printf(" Kernel does not work \n");
exit(1);
}
clFinish(commands);
err = clEnqueueReadBuffer(commands, d_nodeArray, CL_TRUE, 0, sizeof(cl_uint)*temp_length, nodeArray, 0, NULL, NULL);
err|= clEnqueueReadBuffer(commands, d_nodePointsArray, CL_TRUE, 0, sizeof(cl_int)*nodeLength, nodePointsArray, 0, NULL, NULL);
err|= clEnqueueReadBuffer(commands, d_nodeIndexArray, CL_TRUE, 0, sizeof(cl_int)*nodeLength, startIndexArray, 0, NULL, NULL);
clFinish(commands);
clReleaseMemObject(d_nodeAdd);
clReleaseMemObject(d_numPoints);
clReleaseMemObject(d_nodeArray);
clReleaseMemObject(d_nodePointsArray);
clFinish(commands);
return 0;
}
Please note that d_odata and d_nodeNums have been declared in the previous functions. The kernel code is given below for the same:
__kernel void createNode(__global int* uniqueCode, __global int* nodeNums,__global int* nodeAdd, __global int* numPoints, __global int* pointIndex,__global int* nodeArray, __global int* nodePoints,__global int* nodeIndex)
{
int ig = get_global_id(0);
int add;
int num = uniqueCode[ig];
int pt = numPoints[ig];
int ind = pointIndex[ig];
int temp,j;
if(nodeNums[ig] == 8)
{
for(int i=0;i<8;i++)
{
temp = ((int)num/10)*10+i;
add = nodeAdd[ig] + i;
nodeArray[add] = temp;
nodePoints[add] = select(0, pt, temp==num);
nodeIndex[add] = select(-1, ind, temp==num);
barrier(CLK_LOCAL_MEM_FENCE);
}
}
else
{
j = num % 10;
nodeAdd[ig] = nodeAdd[ig-1];
add = nodeAdd[ig]+j;
nodePoints[add] = pt;
nodeIndex[add] = ind;
barrier(CLK_LOCAL_MEM_FENCE);
}
}
I have tried to find out why but have not succeeded. I might be overlooking something really simple. Thank you for your help.
I'm not 100% sure this is causing the crash, but where you've written
if(nodeNums[ig] == 8)
{
for(int i=0;i<8;i++)
{
barrier(CLK_LOCAL_MEM_FENCE);
}
}
else
{
barrier(CLK_LOCAL_MEM_FENCE);
}
This means that different threads in a work group will be executing different numbers of barriers, which may cause a hang/crash. A barrier (with CLK_LOCAL_MEM_FENCE) is for synchronising accesses to local memory, so all work items in a group must execute this before continuing
On a non crash note, it looks like you're using CLK_LOCAL_MEM_FENCE (ensure that local memory accesses are visible across threads) when you mean CLK_GLOBAL_MEM_FENCE (ensure that global memory accesses are visible across threads)
Also
nodeAdd[ig] = nodeAdd[ig-1];
Is not correct for ig == 0. This may not be causing the actual crash (because I've found that OpenCL can be unfortunately quite forgiving), but its worth fixing

SecKeychainFindInternetPassword is not working

I have a problem with storing and reading from my keychain. First I tried to store use a char like this "account_name" in the Method SecKeychainFindInternetPassword for accountname and this runs. But now i would like to store a variable inside. But if i run this code the Programm cannot find the Keychain Item.
Please Help me.
(Sorry for my bad Englisch, i am a german student)
-(void)StorePasswordKeychain:(void*)password :(UInt32)passwordLength
{
char *userString;
userString = (char *)[_username UTF8String];
SecKeychainAddInternetPassword(
NULL,
StrLength("myserver.com"),
"myserver.com",
0,
NULL,
StrLength(userString),
userString,
0,
nil,
0,
kSecProtocolTypeHTTPS,
kSecAuthenticationTypeHTMLForm,
passwordLength,
password,
NULL
);
}
-(OSStatus)GetPasswordKeychain:(void *)passwordData :(UInt32 *)passwordLength
{
OSStatus status;
char *userString;
userString = (char *)[_username UTF8String];
status = SecKeychainFindInternetPassword(
NULL,
StrLength("myserver.com"),
"myserver.com",
0,
NULL,
StrLength(userString),
userString,
0,
nil,
0,
kSecProtocolTypeHTTPS,
kSecAuthenticationTypeHTMLForm,
passwordLength,
passwordData,
NULL
);
return status;
}
Two suggestions.. don't pass null into itemRef (the last arg). Then you'll have a pointer to the keychain you wish to modify.
Also, you should really check the error code to see if your add function worked.
OSStatus result = SecKeychainAddInternetPassword(
NULL,
StrLength("myserver.com"),
"myserver.com",
0,
NULL,
StrLength(userString),
userString,
0,
nil,
0,
kSecProtocolTypeHTTPS,
kSecAuthenticationTypeHTMLForm,
passwordLength,
password,
NULL
);
if(result != noErr){
NSLog(#"Error AddPassword result=:%d", result );
}
This is my sample program with the same code that you provided and it works fine.
int main(int argc, const char * argv[])
{
#autoreleasepool {
char *inputpassword = "topsecret";
UInt32 inputpassLength = strlen(inputpassword);
OSStatus status;
NSString *_username = #"account_name";
char *userString;
userString = (char *)[_username UTF8String];
status = SecKeychainAddInternetPassword(
NULL,
StrLength("myserver.com"),
"myserver.com",
0,
NULL,
StrLength(userString),
userString,
0,
nil,
0,
kSecProtocolTypeHTTPS,
kSecAuthenticationTypeHTMLForm,
inputpassLength,
inputpassword,
NULL
);
NSLog(#"Adding Status:%d", status);
UInt32 returnpasswordLength = 0;
char *passwordData;
status = SecKeychainFindInternetPassword(
NULL,
StrLength("myserver.com"),
"myserver.com",
0,
NULL,
StrLength(userString),
userString,
0,
nil,
0,
kSecProtocolTypeHTTPS,
kSecAuthenticationTypeHTMLForm,
&returnpasswordLength,
(void *)&passwordData,
NULL
);
NSLog(#"Retrieving status:%d", status);
NSLog(#"Password:%#", [[NSString alloc] initWithBytes:passwordData
length:returnpasswordLength
encoding:NSUTF8StringEncoding]);
}
return 0;
}

Wrong output while parsing a .sql file

I need to write a simple c program that takes a particular dump of a table as input and arrange it in a particular way in the output. I'm trying to parse the information I need and put them inside the memory, but I have a problem. I've outputed my data, and I can see "strange chars" as output. Here's the code:
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
typedef struct entityTail
{
char entity[50];
struct entityTail* next;
} entityTail;
typedef struct keyTail
{
char key[300];
struct keyTail* next;
} keyTail;
typedef struct eventTail
{
char event[5];
struct eventTail* next;
} eventTail;
void setATuple(entityTail* entityNew, entityTail* entityNext, keyTail* keyNew, keyTail* keyNext, eventTail* eventNew, eventTail* eventNext, char* startOfParam);
int main(int argc, char* argv[])
{
if(argc != 2)
{
printf("Only 1 param needed: the filename");
return -1;
}
char* buffer = NULL; //this buffer will contain all our conf file
long size = 0; //in bytes
long length = 0; //in chars, must match the "size" variable
FILE* hFile;
bool exit = false;
char* startOfTuple = NULL;
bool headsSet = false; //used within a cycle
entityTail* entityHead = (entityTail*) malloc(sizeof(entityTail));
keyTail* keyHead = (keyTail*) malloc(sizeof(keyTail));
eventTail* eventHead = (eventTail*) malloc(sizeof(eventTail));
entityTail* theEntityTail = NULL;
keyTail* theKeyTail = NULL;
eventTail* theEventTail = NULL;
hFile = fopen( argv[1], "r"); //opening the file in "read-only" mode
if (hFile == NULL)
{
printf("Error: the input file doesn't exist\n");
return -2; //error opening the file
}
//retrieving the size (in bytes) of my text file
fseek (hFile , 0 , SEEK_END); //point to the end of the file
size = ftell (hFile); //retrieving the size of the file
rewind (hFile); //point to the start
// allocate memory to contain the whole file:
buffer = malloc (sizeof(char) * size);
if (buffer == NULL)
{
printf("Error: memory error\n");
return -3; //memory error
}
// copy the file into the buffer:
length = fread (buffer,1,size,hFile); //reading "size" elements of 1 byte, stored in hFile
if (length != size)
{
printf("Error: read error\n");
return -4; //reading error, length and size doesn't match
}
/* the whole file is now loaded in the memory buffer. */
// terminate
fclose (hFile);
//now we look for the position where our data starts
//our entity param is just an int, so it will be not enclosed by the ' symbol
startOfTuple = strstr(buffer, "VALUES"); //finding "VALUES" inside our table dump
startOfTuple = strstr(startOfTuple + 1, "("); //now we point to our first tuple
//now we process the rest of our file
while(!exit)
{
entityTail* entityNew = (entityTail*) malloc(sizeof(entityTail));
keyTail* keyNew = (keyTail*) malloc(sizeof(keyTail));
eventTail* eventNew = (eventTail*) malloc(sizeof(eventTail));
if(!headsSet) //we need to set our heads
{
setATuple(entityHead, NULL, keyHead, NULL, eventHead, NULL, startOfTuple);
theEntityTail = entityHead;
theKeyTail = keyHead;
theEventTail = eventHead;
headsSet = true;
printf("\n%s %s %s\n", entityHead->entity, keyHead->key, eventHead->event);
}
else
{
setATuple(entityNew, theEntityTail, keyNew, theKeyTail, eventNew, theEventTail, startOfTuple);
theEntityTail = entityNew;
theKeyTail = keyNew;
theEventTail = eventNew;
printf("\n%s %s %s\n", entityNew->entity, keyNew->key, eventNew->event);
exit = true; //debugging purpose
}
startOfTuple = strchr(startOfTuple + 1 , '('); //pointing to the next tuple
if(startOfTuple == NULL) exit = true;
}
return 0;
}
void setATuple(entityTail* entityNew, entityTail* entityNext, keyTail* keyNew, keyTail* keyNext, eventTail* eventNew, eventTail* eventNext, char* startOfParam)
{
int i = 0;
char* endOfParam = NULL;
//first we reach the "entity" param (an integer)
for(i = 0; i < 3; i++)
{
startOfParam = strchr(startOfParam + 1, ',');
endOfParam = strchr(startOfParam + 1, ',');
}
//now we set the entityHead
if(entityNext != NULL)
entityNew->next = entityNext;
else //we're setting the heads
entityNew->next = NULL;
strncpy(entityNew->entity, startOfParam + 1, endOfParam - startOfParam - 1);
//now we set the keyHead; that param is enclosed by two ' symbols
startOfParam = strchr(endOfParam + 1, '\''); //we look now for the ' symbol, it's easier that look for a comma and then remove the ' symbol
endOfParam = strchr(startOfParam + 1, '\''); //it should work even if that field in the database is empty
if(keyNext != NULL)
keyNew->next = keyNext;
else
keyNew->next = NULL;
strncpy(keyNew->key, startOfParam + 1, endOfParam - startOfParam - 1);
//now we set the eventHead
startOfParam = strchr(endOfParam + 1, '\''); //we look now for the ' symbol, it's easier that look for a comma and then remove the ' symbol
endOfParam = strchr(startOfParam + 1, '\''); //it should work even if that field in the database is empty
if(eventNext != NULL)
eventNew->next = eventNext;
else
eventNew->next = NULL;
strncpy(eventNew->event, startOfParam + 1, endOfParam - startOfParam - 1);
}
I think the problem is related to the function setATuple, but I can't figure out what's wrong.
Since it's a specific file-related program, I'm going to copy the file it's supposed to process. Here it is:
-- MySQL dump 10.13 Distrib 5.5.24, for debian-linux-gnu (i686)
--
-- Host: localhost Database: tesi
-- ------------------------------------------------------
-- Server version 5.5.24-0ubuntu0.12.04.1
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET #OLD_TIME_ZONE=##TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET #OLD_SQL_NOTES=##SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `log`
--
DROP TABLE IF EXISTS `log`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `log` (
`eid` int(11) NOT NULL AUTO_INCREMENT,
`system` int(11) NOT NULL,
`host` varchar(15) NOT NULL,
`entity` int(30) NOT NULL,
`key` varchar(40) NOT NULL,
`event` varchar(5) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`eid`),
KEY `event` (`event`),
CONSTRAINT `log_ibfk_1` FOREIGN KEY (`event`) REFERENCES `events` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=238 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = #saved_cs_client */;
--
-- Dumping data for table `log`
--
LOCK TABLES `log` WRITE;
/*!40000 ALTER TABLE `log` DISABLE KEYS */;
INSERT INTO `log` VALUES (1,1,'127.000.000.001',0,'10','CMP','2012-10-09 15:13:37'),(2,1,'127.000.000.001',0,'19','CMP','2012-10-09 15:13:37'),(3,1,'127.000.000.001',0,'38','CMP','2012-10-09 15:13:37'),(4,1,'127.000.000.001',0,'77','CMP','2012-10-09 15:13:37'),(5,1,'127.000.000.001',0,'108','CMP','2012-10-09 15:13:37'),(6,1,'127.000.000.001',0,'119','CMP','2012-10-09 15:13:37'),(7,1,'127.000.000.001',0,'149','CMP','2012-10-09 15:13:37'),(8,1,'127.000.000.001',0,'154','CMP','2012-10-09 15:13:38'),(9,1,'127.000.000.001',1,'34','CMP','2012-10-09 15:13:38'),(10,1,'127.000.000.001',1,'143','CMP','2012-10-09 15:13:38'),(11,1,'127.000.000.001',2,'10','CMP','2012-10-09 15:13:38'),(12,1,'127.000.000.001',2,'30','CMP','2012-10-09 15:13:38'),(13,1,'127.000.000.001',2,'87','CMP','2012-10-09 15:13:38'),(14,1,'127.000.000.001',2,'102','CMP','2012-10-09 15:13:39'),(15,1,'127.000.000.001',2,'125','CMP','2012-10-09 15:13:39'),(16,1,'127.000.000.001',2,'161','CMP','2012-10-09 15:13:39'),(17,1,'127.000.000.001',2,'163','CMP','2012-10-09 15:13:39'),(18,1,'127.000.000.001',2,'181','CMP','2012-10-09 15:13:39'),(19,1,'127.000.000.001',2,'182','CMP','2012-10-09 15:13:39'),(20,1,'127.000.000.001',2,'183','CMP','2012-10-09 15:13:39'),(21,1,'127.000.000.001',3,'0','CMP','2012-10-09 15:13:39'),(22,1,'127.000.000.001',3,'40','CMP','2012-10-09 15:13:39'),(23,1,'127.000.000.001',3,'43','CMP','2012-10-09 15:13:39'),(24,1,'127.000.000.001',3,'101','CMP','2012-10-09 15:13:39'),(25,1,'127.000.000.001',3,'126','CMP','2012-10-09 15:13:40'),(26,1,'127.000.000.001',3,'190','CMP','2012-10-09 15:13:40'),(27,1,'127.000.000.001',4,'21','CMP','2012-10-09 15:13:40'),(28,1,'127.000.000.001',4,'22','CMP','2012-10-09 15:13:40'),(29,1,'127.000.000.001',4,'75','CMP','2012-10-09 15:13:40'),(30,1,'127.000.000.001',4,'88','CMP','2012-10-09 15:13:40'),(31,1,'127.000.000.001',4,'110','CMP','2012-10-09 15:13:40'),(32,1,'127.000.000.001',4,'182','CMP','2012-10-09 15:13:40'),(33,1,'127.000.000.001',5,'66','CMP','2012-10-09 15:13:40'),(34,1,'127.000.000.001',5,'90','CMP','2012-10-09 15:13:41'),(35,1,'127.000.000.001',5,'193','CMP','2012-10-09 15:13:41'),(36,1,'127.000.000.001',6,'57','CMP','2012-10-09 15:13:41'),(37,1,'127.000.000.001',6,'67','CMP','2012-10-09 15:13:41'),(38,1,'127.000.000.001',6,'71','CMP','2012-10-09 15:13:41'),(39,1,'127.000.000.001',6,'73','SER','2012-10-09 15:13:41'),(40,1,'127.000.000.001',6,'90','CMP','2012-10-09 15:13:41'),(41,1,'127.000.000.001',6,'91','CMP','2012-10-09 15:13:41'),(42,1,'127.000.000.001',6,'112','CMP','2012-10-09 15:13:41'),(43,1,'127.000.000.001',6,'126','CMP','2012-10-09 15:13:41'),(44,1,'127.000.000.001',6,'173','CMP','2012-10-09 15:13:41'),(45,1,'127.000.000.001',7,'20','CMP','2012-10-09 15:13:41'),(46,1,'127.000.000.001',7,'22','CMP','2012-10-09 15:13:42'),(47,1,'127.000.000.001',7,'33','CMP','2012-10-09 15:13:42'),(48,1,'127.000.000.001',7,'73','CMP','2012-10-09 15:13:42'),(49,1,'127.000.000.001',7,'97','CMP','2012-10-09 15:13:42'),(50,1,'127.000.000.001',7,'126','CMP','2012-10-09 15:13:42'),(51,1,'127.000.000.001',7,'127','CMP','2012-10-09 15:13:42'),(52,1,'127.000.000.001',7,'170','CMP','2012-10-09 15:13:42'),(53,1,'127.000.000.001',7,'189','CMP','2012-10-09 15:13:42'),(54,1,'127.000.000.001',8,'5','CMP','2012-10-09 15:13:42'),(55,1,'127.000.000.001',8,'80','CMP','2012-10-09 15:13:43'),(56,1,'127.000.000.001',8,'90','CMP','2012-10-09 15:13:43'),(57,1,'127.000.000.001',8,'102','CMP','2012-10-09 15:13:43'),(58,1,'127.000.000.001',8,'158','CMP','2012-10-09 15:13:43'),(59,1,'127.000.000.001',8,'185','CMP','2012-10-09 15:13:43'),(60,1,'127.000.000.001',9,'22','CMP','2012-10-09 15:13:43'),(61,1,'127.000.000.001',9,'34','CMP','2012-10-09 15:13:43'),(62,1,'127.000.000.001',9,'50','CMP','2012-10-09 15:13:43'),(63,1,'127.000.000.001',9,'51','CMP','2012-10-09 15:13:43'),(64,1,'127.000.000.001',9,'89','CMP','2012-10-09 15:13:43'),(65,1,'127.000.000.001',9,'110','CMP','2012-10-09 15:13:43'),(66,1,'127.000.000.001',9,'137','CMP','2012-10-09 15:13:43'),(67,1,'127.000.000.001',9,'141','CMP','2012-10-09 15:13:44'),(68,1,'127.000.000.001',9,'186','CMP','2012-10-09 15:13:44'),(69,1,'127.000.000.001',9,'194','CMP','2012-10-09 15:13:44'),(70,1,'127.000.000.001',10,'23','CMP','2012-10-09 15:13:44'),(71,1,'127.000.000.001',10,'42','CMP','2012-10-09 15:13:44'),(72,1,'127.000.000.001',10,'51','CMP','2012-10-09 15:13:44'),(73,1,'127.000.000.001',10,'52','CMP','2012-10-09 15:13:44'),(74,1,'127.000.000.001',11,'44','CMP','2012-10-09 15:13:44'),(75,1,'127.000.000.001',11,'57','CMP','2012-10-09 15:13:44'),(76,1,'127.000.000.001',11,'123','CMP','2012-10-09 15:13:45'),(77,1,'127.000.000.001',11,'125','CMP','2012-10-09 15:13:45'),(78,1,'127.000.000.001',11,'145','CMP','2012-10-09 15:13:45'),(79,1,'127.000.000.001',12,'6','CMP','2012-10-09 15:13:45'),(80,1,'127.000.000.001',12,'26','CMP','2012-10-09 15:13:45'),(81,1,'127.000.000.001',12,'28','CMP','2012-10-09 15:13:45'),(82,1,'127.000.000.001',12,'61','CMP','2012-10-09 15:13:45'),(83,1,'127.000.000.001',12,'117','CMP','2012-10-09 15:13:45'),(84,1,'127.000.000.001',12,'134','CMP','2012-10-09 15:13:45'),(85,1,'127.000.000.001',12,'139','CMP','2012-10-09 15:13:45'),(86,1,'127.000.000.001',12,'158','CMP','2012-10-09 15:13:45'),(87,1,'127.000.000.001',12,'186','CMP','2012-10-09 15:13:45'),(88,1,'127.000.000.001',12,'189','CMP','2012-10-09 15:13:45'),(89,1,'127.000.000.001',12,'191','CMP','2012-10-09 15:13:46'),(90,1,'127.000.000.001',12,'193','CMP','2012-10-09 15:13:46'),(91,1,'127.000.000.001',13,'31','CMP','2012-10-09 15:13:46'),(92,1,'127.000.000.001',13,'93','CMP','2012-10-09 15:13:46'),(93,1,'127.000.000.001',13,'109','CMP','2012-10-09 15:13:46'),(94,1,'127.000.000.001',13,'149','CMP','2012-10-09 15:13:46'),(95,1,'127.000.000.001',14,'5','CMP','2012-10-09 15:13:46'),(96,1,'127.000.000.001',14,'24','CMP','2012-10-09 15:13:46'),(97,1,'127.000.000.001',14,'128','CMP','2012-10-09 15:13:47'),(98,1,'127.000.000.001',14,'142','CMP','2012-10-09 15:13:47'),(99,1,'127.000.000.001',14,'161','CMP','2012-10-09 15:13:47'),(100,1,'127.000.000.001',15,'17','CMP','2012-10-09 15:13:47'),(101,1,'127.000.000.001',15,'19','CMP','2012-10-09 15:13:47'),(102,1,'127.000.000.001',15,'27','CMP','2012-10-09 15:13:47'),(103,1,'127.000.000.001',15,'71','CMP','2012-10-09 15:13:47'),(104,1,'127.000.000.001',15,'74','CMP','2012-10-09 15:13:47'),(105,1,'127.000.000.001',15,'104','CMP','2012-10-09 15:13:47'),(106,1,'127.000.000.001',15,'126','CMP','2012-10-09 15:13:48'),(107,1,'127.000.000.001',15,'143','CMP','2012-10-09 15:13:48'),(108,1,'127.000.000.001',15,'155','CMP','2012-10-09 15:13:48'),(109,1,'127.000.000.001',15,'185','CMP','2012-10-09 15:13:48'),(110,1,'127.000.000.001',16,'69','CMP','2012-10-09 15:13:48'),(111,1,'127.000.000.001',16,'103','CMP','2012-10-09 15:13:48'),(112,1,'127.000.000.001',16,'139','CMP','2012-10-09 15:13:48'),(113,1,'127.000.000.001',16,'169','CMP','2012-10-09 15:13:48'),(114,1,'127.000.000.001',17,'51','CMP','2012-10-09 15:13:48'),(115,1,'127.000.000.001',17,'67','CMP','2012-10-09 15:13:49'),(116,1,'127.000.000.001',17,'138','CMP','2012-10-09 15:13:49'),(117,1,'127.000.000.001',17,'162','CMP','2012-10-09 15:13:49'),(118,1,'127.000.000.001',17,'173','CMP','2012-10-09 15:13:49'),(119,1,'127.000.000.001',17,'185','CMP','2012-10-09 15:13:49'),(120,1,'127.000.000.001',18,'17','CMP','2012-10-09 15:13:49'),(121,1,'127.000.000.001',18,'57','CMP','2012-10-09 15:13:49'),(122,1,'127.000.000.001',18,'107','CMP','2012-10-09 15:13:49'),(123,1,'127.000.000.001',18,'163','CMP','2012-10-09 15:13:50'),(124,1,'127.000.000.001',18,'193','CMP','2012-10-09 15:13:50'),(125,1,'127.000.000.001',19,'3','CMP','2012-10-09 15:13:50'),(126,1,'127.000.000.001',19,'11','CMP','2012-10-09 15:13:50'),(127,1,'127.000.000.001',19,'36','CMP','2012-10-09 15:13:50'),(128,1,'127.000.000.001',19,'109','CMP','2012-10-09 15:13:50'),(129,1,'127.000.000.001',19,'126','CMP','2012-10-09 15:13:50'),(130,1,'127.000.000.001',19,'194','CMP','2012-10-09 15:13:50'),(131,1,'127.000.000.001',19,'196','CMP','2012-10-09 15:13:50'),(132,1,'127.000.000.001',0,'19','IER','2012-10-09 15:13:55'),(133,1,'127.000.000.001',0,'20','SER','2012-10-09 15:13:55'),(134,1,'127.000.000.001',0,'174','SER','2012-10-09 15:13:55'),(135,1,'127.000.000.001',0,'199','IER','2012-10-09 15:13:55'),(136,1,'127.000.000.001',1,'140','SER','2012-10-09 15:13:55'),(137,1,'127.000.000.001',1,'147','SER','2012-10-09 15:13:56'),(138,1,'127.000.000.001',1,'164','IER','2012-10-09 15:13:56'),(139,1,'127.000.000.001',1,'167','IER','2012-10-09 15:13:56'),(140,1,'127.000.000.001',1,'183','IER','2012-10-09 15:13:56'),(141,1,'127.000.000.001',2,'29','IER','2012-10-09 15:13:56'),(142,1,'127.000.000.001',2,'33','IER','2012-10-09 15:13:56'),(143,1,'127.000.000.001',2,'129','IER','2012-10-09 15:13:56'),(144,1,'127.000.000.001',2,'155','SER','2012-10-09 15:13:56'),(145,1,'127.000.000.001',2,'180','IER','2012-10-09 15:13:56'),(146,1,'127.000.000.001',3,'8','SER','2012-10-09 15:13:57'),(147,1,'127.000.000.001',3,'25','SER','2012-10-09 15:13:57'),(148,1,'127.000.000.001',3,'33','SER','2012-10-09 15:13:57'),(149,1,'127.000.000.001',3,'109','SER','2012-10-09 15:13:57'),(150,1,'127.000.000.001',3,'144','IER','2012-10-09 15:13:57'),(151,1,'127.000.000.001',4,'13','SER','2012-10-09 15:13:57'),(152,1,'127.000.000.001',4,'56','SER','2012-10-09 15:13:57'),(153,1,'127.000.000.001',4,'94','SER','2012-10-09 15:13:58'),(154,1,'127.000.000.001',4,'125','IER','2012-10-09 15:13:58'),(155,1,'127.000.000.001',4,'171','SER','2012-10-09 15:13:58'),(156,1,'127.000.000.001',4,'199','SER','2012-10-09 15:13:58'),(157,1,'127.000.000.001',5,'4','IER','2012-10-09 15:13:58'),(158,1,'127.000.000.001',5,'93','SER','2012-10-09 15:13:58'),(159,1,'127.000.000.001',5,'94','SER','2012-10-09 15:13:58'),(160,1,'127.000.000.001',6,'8','IER','2012-10-09 15:13:58'),(161,1,'127.000.000.001',6,'103','IER','2012-10-09 15:13:59'),(162,1,'127.000.000.001',6,'107','SER','2012-10-09 15:13:59'),(163,1,'127.000.000.001',6,'154','IER','2012-10-09 15:13:59'),(164,1,'127.000.000.001',6,'167','SER','2012-10-09 15:13:59'),(165,1,'127.000.000.001',7,'14','SER','2012-10-09 15:13:59'),(166,1,'127.000.000.001',7,'42','SER','2012-10-09 15:13:59'),(167,1,'127.000.000.001',7,'109','IER','2012-10-09 15:13:59'),(168,1,'127.000.000.001',7,'115','SER','2012-10-09 15:13:59'),(169,1,'127.000.000.001',7,'132','IER','2012-10-09 15:14:00'),(170,1,'127.000.000.001',7,'148','IER','2012-10-09 15:14:00'),(171,1,'127.000.000.001',7,'162','IER','2012-10-09 15:14:00'),(172,1,'127.000.000.001',8,'13','IER','2012-10-09 15:14:00'),(173,1,'127.000.000.001',8,'33','SER','2012-10-09 15:14:00'),(174,1,'127.000.000.001',8,'57','IER','2012-10-09 15:14:00'),(175,1,'127.000.000.001',8,'65','IER','2012-10-09 15:14:00'),(176,1,'127.000.000.001',8,'84','SER','2012-10-09 15:14:00'),(177,1,'127.000.000.001',8,'90','IER','2012-10-09 15:14:00'),(178,1,'127.000.000.001',9,'9','IER','2012-10-09 15:14:01'),(179,1,'127.000.000.001',9,'78','IER','2012-10-09 15:14:01'),(180,1,'127.000.000.001',9,'92','SER','2012-10-09 15:14:01'),(181,1,'127.000.000.001',9,'114','SER','2012-10-09 15:14:01'),(182,1,'127.000.000.001',10,'54','SER','2012-10-09 15:14:01'),(183,1,'127.000.000.001',10,'79','SER','2012-10-09 15:14:01'),(184,1,'127.000.000.001',10,'86','SER','2012-10-09 15:14:01'),(185,1,'127.000.000.001',10,'91','SER','2012-10-09 15:14:01'),(186,1,'127.000.000.001',10,'97','SER','2012-10-09 15:14:01'),(187,1,'127.000.000.001',10,'158','SER','2012-10-09 15:14:02'),(188,1,'127.000.000.001',11,'56','IER','2012-10-09 15:14:02'),(189,1,'127.000.000.001',11,'132','SER','2012-10-09 15:14:02'),(190,1,'127.000.000.001',11,'136','IER','2012-10-09 15:14:02'),(191,1,'127.000.000.001',11,'163','SER','2012-10-09 15:14:02'),(192,1,'127.000.000.001',11,'165','IER','2012-10-09 15:14:02'),(193,1,'127.000.000.001',12,'55','IER','2012-10-09 15:14:02'),(194,1,'127.000.000.001',12,'137','SER','2012-10-09 15:14:02'),(195,1,'127.000.000.001',12,'142','IER','2012-10-09 15:14:02'),(196,1,'127.000.000.001',12,'192','IER','2012-10-09 15:14:03'),(197,1,'127.000.000.001',13,'53','IER','2012-10-09 15:14:03'),(198,1,'127.000.000.001',13,'56','SER','2012-10-09 15:14:03'),(199,1,'127.000.000.001',13,'135','IER','2012-10-09 15:14:03'),(200,1,'127.000.000.001',13,'139','SER','2012-10-09 15:14:03'),(201,1,'127.000.000.001',13,'143','IER','2012-10-09 15:14:03'),(202,1,'127.000.000.001',13,'155','IER','2012-10-09 15:14:03'),(203,1,'127.000.000.001',13,'178','IER','2012-10-09 15:14:03'),(204,1,'127.000.000.001',13,'181','IER','2012-10-09 15:14:03'),(205,1,'127.000.000.001',13,'182','SER','2012-10-09 15:14:03'),(206,1,'127.000.000.001',14,'25','SER','2012-10-09 15:14:04'),(207,1,'127.000.000.001',14,'55','IER','2012-10-09 15:14:04'),(208,1,'127.000.000.001',14,'66','SER','2012-10-09 15:14:04'),(209,1,'127.000.000.001',14,'70','SER','2012-10-09 15:14:04'),(210,1,'127.000.000.001',14,'86','SER','2012-10-09 15:14:04'),(211,1,'127.000.000.001',14,'93','SER','2012-10-09 15:14:04'),(212,1,'127.000.000.001',14,'108','IER','2012-10-09 15:14:04'),(213,1,'127.000.000.001',14,'183','SER','2012-10-09 15:14:04'),(214,1,'127.000.000.001',15,'24','IER','2012-10-09 15:14:04'),(215,1,'127.000.000.001',15,'65','IER','2012-10-09 15:14:04'),(216,1,'127.000.000.001',15,'153','IER','2012-10-09 15:14:05'),(217,1,'127.000.000.001',15,'157','SER','2012-10-09 15:14:05'),(218,1,'127.000.000.001',16,'60','IER','2012-10-09 15:14:05'),(219,1,'127.000.000.001',16,'108','IER','2012-10-09 15:14:05'),(220,1,'127.000.000.001',16,'148','IER','2012-10-09 15:14:05'),(221,1,'127.000.000.001',16,'170','IER','2012-10-09 15:14:05'),(222,1,'127.000.000.001',16,'181','SER','2012-10-09 15:14:05'),(223,1,'127.000.000.001',17,'20','IER','2012-10-09 15:14:05'),(224,1,'127.000.000.001',17,'134','SER','2012-10-09 15:14:06'),(225,1,'127.000.000.001',17,'200','IER','2012-10-09 15:14:06'),(226,1,'127.000.000.001',18,'4','SER','2012-10-09 15:14:06'),(227,1,'127.000.000.001',18,'63','IER','2012-10-09 15:14:06'),(228,1,'127.000.000.001',18,'74','SER','2012-10-09 15:14:06'),(229,1,'127.000.000.001',18,'84','SER','2012-10-09 15:14:06'),(230,1,'127.000.000.001',18,'88','IER','2012-10-09 15:14:06'),(231,1,'127.000.000.001',18,'94','SER','2012-10-09 15:14:06'),(232,1,'127.000.000.001',18,'108','IER','2012-10-09 15:14:06'),(233,1,'127.000.000.001',19,'24','SER','2012-10-09 15:14:07'),(234,1,'127.000.000.001',19,'88','SER','2012-10-09 15:14:07'),(235,1,'127.000.000.001',19,'137','IER','2012-10-09 15:14:07'),(236,1,'127.000.000.001',19,'142','SER','2012-10-09 15:14:07'),(237,1,'127.000.000.001',19,'187','SER','2012-10-09 15:14:07');
/*!40000 ALTER TABLE `log` ENABLE KEYS */;
It "dies" as soon as it reaches the second tuple. Sorry for my poor english, I'm from Italy. Thank you for your help!
I was unable to debug your code; :-(, however I rewrote this code, see if you can use it:
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
/*
* Apparently only interested in 4,5 & 6th comma separated values
* strtok_r() should be preferred over strtok(); Windows MINGW doesn't have it
*/
int process_record(char *recordPtr/* and , your DataStructure Ptr if necessary */)
{
unsigned count;
char* fieldPtr = NULL;
fprintf(stderr, "\nFields :");
for (count = 1; NULL != (fieldPtr = strtok_r(NULL, ",", &recordPtr)); count++)
{
if (count > 3 && count < 7)
{// required fields range
/*TODO Trim unwanted characters */
switch (count)
{
case 4: // copy to relevant DS
break;
case 5: // copy to relevant DS
break;
case 6: // copy to relevant DS
break;
}
fprintf(stderr, "\t %s", fieldPtr);
}
}
return 0;
}
int main(int argc, char* argv[])
{
int status;
unsigned count;
unsigned readBytes = 0;
struct stat fStat;
unsigned fileSize ;
FILE* hFile;
char* fileData = NULL;
char* dataPtr =NULL;
char* recordPtr = NULL;
fprintf (stderr, "\n");
if (argc != 2)
{
fprintf(stderr, "\nERROR: Only <filename> required");
return -1;
}
/*
* Get the file size for Memory Load
*/
status = stat(argv[1], &fStat);
if (status < 0) {
fprintf(stderr, "\nERROR: Line[%d] ErroNo[%d]", __LINE__, errno);
return -2;
}
fileSize = fStat.st_size;
fprintf (stderr, "\nDump File size[%u]", fileSize);
/*
* Allocate sufficient memory to load file data on to memory
*/
fileData = (char*) malloc (sizeof(char) * fileSize);
if (NULL == fileData) {
fprintf(stderr, "\nERROR: Allocating memory");
return -3;
}
/* Open file and read data on to memory
*/
hFile = fopen( argv[1], "r"); //opening the file in "read-only" mode
if (hFile == NULL)
{
fprintf(stderr, "ERROR: Opening file[%s] ErroNo[%d]\n", argv[1], errno);
return -4; //error opening the file
}
/* assuming file will be read within 3 attempts */
for (status =0; fileSize != readBytes && status < 3; status ++) {
readBytes += fread (fileData + readBytes, 1, fileSize - readBytes, hFile);
fprintf (stderr, "\nData read Size [%u]", readBytes);
}
fclose (hFile);
//fprintf (stderr, "\nNOTE: Data read[\n%s\n]", fileData);
/*
* Chunk it into manageable records
*/
dataPtr = strstr (fileData, "VALUES (") + strlen ("VALUES ("); //handle error checks
for(count =1; NULL != (recordPtr = strtok_r(NULL, "()", &dataPtr)); count ++) {
if (strlen(recordPtr) > 1) { // NOTE: "," between each record will be tokenised
//fprintf (stderr, "\nRecord[%d] : [%s]", count, recordPtr);
/* Process Record
*/
process_record (recordPtr /* and , your DataStructure Ptr if necessary */);
}
}
free (fileData);
return 0;
}