NO

Author Topic: Find entries in file  (Read 7841 times)

pitter2206

  • Guest
Find entries in file
« on: December 18, 2009, 02:09:01 PM »
Hello together,

sorry for my bad English, but I will try as best I can to explain my problem...  8)

I want to open a file and copy some entries to write them into a txt-file on a CE5.0 device.

this is a part of the file named "feature.xml"

Code: [Select]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE FeatureLicense SYSTEM "feature.dtd">

<FeatureLicense vendor="MEDION" appVersion="5.0" appName="GoPal Navigator" editionName="AE" master="yes" version="1.0" >

I want to write these three entries as string to the textfile

GoPal Navigator
5.0
AE

These entries would be written as "GoPal Navigator 5.0 AE" in a specified textfile wich is read out with an existing installer.

I´ve got no idea how to do that... would you please help me?
« Last Edit: December 18, 2009, 02:12:12 PM by pitter2206 »

pitter2206

  • Guest
Re: Find entries in file
« Reply #1 on: December 20, 2009, 10:45:05 PM »
Hello again,

I tried something...
Here I´ll show the hole code after <snap, because the script doesn´t read the XML-File

Code: [Select]
#include <windows.h>
#include <windowsx.h>
#include "main.h"
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include <winioctl.h>
#include <string.h>


static HANDLE ghInstance;

wchar_t variable[200];
FILE* src;

HKEY hkey;
DWORD dwValue;
DWORD dwType;
DWORD dwCount = sizeof ( DWORD );

#define MulDiv(a,b,c) (((a)*(b))/(c))

HBITMAP hbit;


HFONT BuildFont(LPCTSTR pszFontFace, int nFontSize, BOOL fBold)
{
    HDC hDC;
    int nHeight;
    LOGFONT lgf;

    hDC = GetDC(NULL);
    nHeight = -MulDiv(nFontSize, GetDeviceCaps(hDC, LOGPIXELSY), 72);
    ReleaseDC(NULL, hDC);

    memset(&lgf, 0, sizeof(lgf));

    lgf.lfHeight = nHeight;
    lgf.lfWeight = fBold ? FW_BOLD: FW_NORMAL;
    lgf.lfCharSet = DEFAULT_CHARSET;
    lgf.lfOutPrecision = OUT_DEFAULT_PRECIS;
    lgf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
    lgf.lfQuality = DEFAULT_QUALITY;
    lgf.lfPitchAndFamily = DEFAULT_PITCH;

    wcscpy(lgf.lfFaceName, pszFontFace);

    return CreateFontIndirect(&lgf);
}

int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
{

// delete old hardware.txt

DeleteFile(L"\\Temp\\hardware.txt");


// create W'indow#######################################
    WNDCLASS wc;
    
    ghInstance = hInstance;

    // Get system dialog information.
    if (!GetClassInfo(NULL, L"Dialog", &wc))
        return 0;

    // Register our own dialog.
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDR_ICO_MAIN));
  
    wc.lpszClassName = L"PCinstall";
    if (!RegisterClass(&wc))
        return 0;

hbit = LoadBitmap(hInstance, MAKEINTRESOURCE(IDR_BMP_LOGO));


HWND hWnd;

hWnd = CreateWindow(L"PCinstall",    // Window Class Name
                    L"Dialog",          // The title bar text
                    WS_VISIBLE | SS_BITMAP,       // Only need to make the window
                                      // visible in CE
                    0,                 // Don't bother with size
                    0,                 // as palm apps fill the
                    CW_USEDEFAULT,    // whole screen
                    CW_USEDEFAULT,    
                    NULL,             // No parent (Top Level)
                    NULL,             // No Menu
                    hInstance,
                    NULL);


ShowWindow (hWnd, nCmdShow);
UpdateWindow (hWnd);

HWND imgCtrl = CreateWindow( _T("STATIC"), NULL, SS_CENTERIMAGE | SS_BITMAP | WS_CHILD | WS_VISIBLE, 0, 0 , 480, 90, hWnd, NULL, NULL, NULL);
SendMessage(imgCtrl,STM_SETIMAGE, (WPARAM)IMAGE_BITMAP,(LPARAM)hbit);

HFONT hFont = BuildFont(TEXT("Tahoma"), 20, TRUE);
HWND hwnd2 = CreateWindow( _T("STATIC"), _T("HARDWAREERKENNUNG LÄUFT"), WS_CHILD | WS_VISIBLE | SS_CENTER, 10, 130 , 460, 60, hWnd, NULL, NULL, NULL);
SendMessage (hwnd2, WM_SETFONT, (WPARAM)hFont, TRUE);
HWND hwnd3 = CreateWindow( _T("STATIC"), _T("PNA NICHT AUSSCHALTEN!."), WS_CHILD | WS_VISIBLE | SS_CENTER, 10, 200 , 460, 60, hWnd, NULL, NULL, NULL);
SendMessage (hwnd3, WM_SETFONT, (WPARAM)hFont, TRUE);

UpdateWindow (hWnd);
_sleep(2);


//Sirf //Atlas ###############################################################

HANDLE hFile = CreateFile(L"\\Windows\\WisGPS200.exe\0", GENERIC_READ , FILE_SHARE_READ, NULL, OPEN_EXISTING,  FILE_ATTRIBUTE_NORMAL, NULL);

if (hFile != INVALID_HANDLE_VALUE)
{
wcscpy(variable, L"Atlas|");
}
else
{
wcscpy(variable, L"Sirf|");
}
CloseHandle(hFile);
<snip

.
.
.
.
<snap

//get software-version###############################

HANDLE hFile6 = CreateFile(TEXT("\\My Flash Disk\\navigation\\licenses\feature.xml\0"), GENERIC_READ , FILE_SHARE_READ, NULL, OPEN_EXISTING,  FILE_ATTRIBUTE_NORMAL, NULL);

if (hFile6 == INVALID_HANDLE_VALUE)
{
wcscat(variable, L"NO");
}
else
{
// open file in buffer

char *readFile(const char *filename);

  //code heavily changed from http://www.cplusplus.com/reference/clibrary/cstdio/fread/
    FILE *file = fopen("\\My Flash Disk\\navigation\\licenses\feature.xml", "rb");
   if (!file) return 0;
   //get file size
   fseek(file, 0, SEEK_END);
   size_t filesize = ftell(file); //files >4GB?
    rewind(file);
   //get memory to contain the whole file
   char *buffer = (char*)malloc(filesize+1);
   if (!buffer) return 0;
   //read file
     size_t temp;
    if ((temp=fread(buffer, 1, filesize+1, file))!=filesize){
        free(buffer);
       fclose(file);
        return 0;
}
  
//look for entries in buffer

int main();
wchar_t hStr6[100];
wchar_t hStr7[100];
   if (!buffer) exit(-1); //konnte feature.xml nicht lesen!
   char *position = strstr(buffer, "appVersion=\"");
   int appVersionLength;
   char *temp1 = position+sizeof("appVersion=\"");
   for (appVersionLength=0; *temp1!='"'; temp++) appVersionLength++;
   char *appVersion = (char *)malloc(appVersionLength+1);
   strncpy(appVersion, position+sizeof "appVersion=\"", appVersionLength);
swprintf(hStr7, L"%d", appVersion);
wcscat(variable, hStr7);

int editionNameLength;
   char *temp2 = position+sizeof("editionName=\"");
   for (editionNameLength=0; *temp2!='"'; temp++) editionNameLength++;
   char *editionName = (char *)malloc(editionNameLength+1);
   strncpy(editionName, position+sizeof "editionName=\"", editionNameLength);
swprintf(hStr6, L"%d", editionName);
wcscat(variable, hStr6);
   free(appVersion);
   free(editionName);


}
CloseHandle(hFile6);

//Write vars to file####################################

src = _wfopen(L"\\Temp\\hardware.txt", L"a+");
fputws(variable, src);
fclose(src);


Sleep(3000);

return 0;
}



Please,,, could someone help me???

Greets
Pitter

JohnF

  • Guest
Re: Find entries in file
« Reply #2 on: December 21, 2009, 04:18:52 PM »
It's a bit of a mess.

Don't mix ASCII characters with WideChar characters.

If I were you I'd make a console app while testing. Make sure you are using the correct conversions in any sprintf type function.

Have another go.

John






pitter2206

  • Guest
Re: Find entries in file
« Reply #3 on: December 22, 2009, 02:32:18 PM »
Hi John,

OK, thanks for answering John, but I am still a beginner in coding in C...  :(


I tried something and I think I am on the right way.

I can open the file and put the hole file into the buffer with this code:

Code: [Select]
          FILE *hfile6 = fopen("\\My Flash Disk\\navigation\\licenses\\feature.xml", "rb");
if (!hfile6)
          {
             wcscat(variable, L"NO");
          }
         else
{
                        fseek(hfile6, 0, SEEK_END);              //get file size
                        size_t filesize = ftell(hfile6); //files >4GB?
                        char *buffer = (char*)malloc(filesize+1);                //get memory to contain the whole file
                        if (!buffer) wcscat(variable, L"Buffer empty|");        // write to txt if buffer is empty
                        size_t temp;
                        if ((temp=fread(buffer, 1, filesize+1, hfile6))!=filesize)
        wcscat(variable, L"YES"); // look, if buffer is written
}


These wcscat are written to the defined textfile:

Code: [Select]
    //Write to file####################################

                src = _wfopen(L"\\Temp\\hardware.txt", L"a+");
                fputws(variable, src);
                fclose(src);
    return 0;
}

The hardware.txt is so far ok:

Sirf|mfd|-|4920320|NO|storage|484737024|480|272|YES

So far the program is running, but I need some entries out of the buffer.
Just now, I tried to read the buffer, but how can I search for these entries in temp?

<FeatureLicense vendor="MEDION" appVersion="5.0" appName="GoPal Navigator" editionName="AE" master="yes" version="1.0" >

I need the followed entries 5.0 and AE because these entries are somtimes different...

Would you please help me?

thanks
Pitter

JohnF

  • Guest
Re: Find entries in file
« Reply #4 on: December 22, 2009, 03:47:59 PM »
You nearly had it earlier.

The following code extracts the version number and then converts the ASCII string to WideChar - for your wchar_t variable[] buffer.

Code: [Select]
#include <windows.h>
#include <stdio.h>

wchar_t variable[200];
char Str7[200];

// appVersion assumes that the version number will not be greater than 19 chars.
char appVersion[20];

int main(void)
{
char buffer[] = "<FeatureLicense vendor=\"MEDION\" appVersion=\"5.0\" appName=\"GoPal Navigator\" editionName=\"AE\" master=\"yes\" version=\"1.0\" >";

char * position = strstr(buffer, "appVersion=");

int appVersionLength = 0;

char * temp = position + sizeof("appVersion=");

for (appVersionLength = 0; *temp != '"'; temp++)
appVersionLength++;

strncpy(appVersion, position + sizeof("appVersion="), appVersionLength);

sprintf(Str7, "%s", appVersion);

// Must convert ASCII to WideChar.
MultiByteToWideChar(CP_ACP, 0, Str7, strlen(Str7), variable, 200);

// the widechar buffer variable now contains the version number.
wprintf(L"%ls\n", variable);

return 0;
}

pitter2206

  • Guest
Re: Find entries in file
« Reply #5 on: December 22, 2009, 09:41:34 PM »
Hi John,

thank you very much!  :D

It works fine, with both entries.

I wish Merry Xmas to you and your family.

Greets
Pitter

JohnF

  • Guest
Re: Find entries in file
« Reply #6 on: December 22, 2009, 10:14:48 PM »
Hi John,

I wish Merry Xmas to you and your family.

Greets
Pitter

And I wish the same for you and your family.

John

pitter2206

  • Guest
Re: Find entries in file
« Reply #7 on: December 23, 2009, 02:31:49 PM »
Ok...  :)

I thought, that die code will work, but it doesn´t.
It only shows what is written in the line in the script:

Code: [Select]
char buffer[] = "<FeatureLicense vendor=\"MEDION\" appVersion=\"5.0\" appName=\"GoPal Navigator\" editionName=\"AE\" master=\"yes\" version=\"1.0\" >";
That is not that, what I need...  ;D

The file is different in case, so I need the real text written in the xml. Can you please help again?

Greets
Pitter

JohnF

  • Guest
Re: Find entries in file
« Reply #8 on: December 23, 2009, 04:03:49 PM »
Are you saying that the text in the file is not like this?

<FeatureLicense vendor="MEDION" appVersion="5.0" appName="GoPal Navigator" editionName="AE" master="yes" version="1.0" >

John

pitter2206

  • Guest
Re: Find entries in file
« Reply #9 on: December 23, 2009, 04:10:44 PM »
In some Editions ther might be another line...
As I know, there would only some differences.

Teh red-marked entries are variable:
<FeatureLicense vendor="MEDION" appVersion="5.0" appName="GoPal Navigator" editionName="AE" master="yes" version="1.0" >

It might be, that at editionName is written ME, AE or PE
and at appVersion 4.1... 4.8, or 5.1

So... to get the exactly GoPal-Version and Edition, I have to look for these Entries...

Greets
Pitter

JohnF

  • Guest
Re: Find entries in file
« Reply #10 on: December 23, 2009, 04:31:47 PM »
You need to deal with each possibility.

Code: [Select]
char * position;

position = strstr(buffer, "editionName=");
if(position == NULL){
position = strstr(buffer, "ME=");
if(position == NULL){
position = strstr(buffer, "AE=");
}
// etc.
}
// Still no luck, exit.
if(position == NULL){
// FAILED
return 0;
}

John

pitter2206

  • Guest
Re: Find entries in file
« Reply #11 on: December 23, 2009, 04:42:52 PM »
Hi John,

I think we talk about different things... It´s my fault, because to exlpain it in english is not my best... :(

As now, the script looks NOT for the file on \\My Flash Disk..., it looks for this line in the Script:
<FeatureLicense vendor="MEDION" appVersion="5.0" appName="GoPal Navigator" editionName="AE" master="yes" version="1.0" >
because this line is read in the buffer NOT the file on My Flash Disk.

char buffer[] = "<FeatureLicense vendor=\"MEDION\" appVersion=\"5.0\" appName=\"GoPal Navigator\" editionName=\"AE\" master=\"yes\" version=\"1.0\" >"

JohnF

  • Guest
Re: Find entries in file
« Reply #12 on: December 23, 2009, 04:57:58 PM »
I thought you had already loaded that text so I wrote the code as an example, that's all. It's up to you to integrate my code into yours making any
necessary changes.

John

 

pitter2206

  • Guest
Re: Find entries in file
« Reply #13 on: December 23, 2009, 05:01:09 PM »
ok, thanks for all,

I´ll give it a try...

Greets
Pitter