Biatu

C, inf parsing help

3 posts in this topic

#1 ·  Posted (edited)

Hello team, I am trying to take another crack at C, and im having trouble understanding why this code isn't working quite right...

Basically the point of this code is to Parse an INF file, and dump an index file formatted as follows:
 

[sInfPath|sDate|sVersion|iDeviceCount|sArchetecture]
HWID=Description

I have already written this in AutoIt and figured that C would be much faster, but lol it's much more intricate!

Can anyone help? Thanks

ParseINF.c:

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

char **FileReadArray(char File[]);
char *StrLeft(const char array[],int count);
char *StrRight(const char array[],int count);
char *StrTrimRight(char array[],int count);
char *StrTrimLeft(char array[],int count);
size_t StripWS(char *out, const char *str);
int CharInStr(char array[],char search,int rtl);
void FilterArray(char *array[]);
char *StrSplit(const char str[],int count);

int main(void){
    char Inf[]="Machine.ans.inf";
    printf("Reading...",Inf);
    char **data=FileReadArray(Inf);//Read File into Array.
    int i=(int)data[0];
    printf("Done\n");
    printf("Cleaning...\n");
    FilterArray(data);
    //printf("Failed\n");
    for (i;i>=1;i--){//Free Array
        free(data[i]);
    }
    free(data);
    return 0;
    }

/*
*/

void FilterArray(char *array[]){
    int i=(int)array[0];
    if (i<1) return 0;
    for (int j=1;j<=i;j++){

        //Strip Leading and Trailing Spaces.
        StripWS(array[j],array[j]);

        //Skip Comment lines, NULL or Space.
        if (array[j][0]==';') continue;
        if (array[j][0]==0x0) continue;
        if (array[j][0]==' ') continue;

        //Strip Comments from lines
        int cPos=CharInStr(array[j],';',1);
        if (cPos>0){
            array[j]=StrLeft(array[j],cPos-1);
        }

        //Strip Leading and Trailing Spaces.
        StripWS(array[j],array[j]);

        //Split Elements
        cPos=CharInStr(array[j],'=',1);
        if (cPos>0){
            array[j]=StrSplit(array[j],cPos);
         //   printf("%d|%s|\n\n",cPos,RightStr);
        }

        //char Right[strlen(array[j])-cPos];
    }

}
char *StrLeft(const char str[],int count){
    int iStrip=count-1;
    strncpy(str,str,iStrip);
    str[iStrip]='\0';
    return str;
}
char *StrRight(const char str[],int count){
    int iStrip=count;
    strncpy(str,str+iStrip,strlen(str)-1);
    str[strlen(str)+1]='\0';
    return str;
}

char *StrSplit(const char str[],int count){
    char **split[2];
    split[0]=StrLeft(str,count);
    split[1]=StrRight(str,count);
    printf("|%s|%s|\n",split[0],split[1]);
    return str;
}

size_t StripWS(char *out, const char *str){
  size_t len=strlen(out);
  const char *end;
  size_t out_size;

  // Trim leading space
  while(isspace(*str)) str++;

  if(*str == 0)  // All spaces?
  {
    *out = 0;
    return 1;
  }

  // Trim trailing space
  end = str + strlen(str) - 1;
  while(end > str && isspace(*end)) end--;
  end++;

  // Set output size to minimum of trimmed string length and buffer size minus 1
  out_size = (end - str) < len ? (end - str) : len;

  // Copy trimmed string and add null terminator
  memcpy(out, str, out_size);
  out[out_size] = 0;

  return out_size;
}

int CharInStr(char array[],char search,int rtl){
    int len=strlen(array);
    if (len<1) return 0;
    if (rtl==0) {
        for (int i=0;i<=len;i++){
            if (array[i]==search) return i+1;
        }
    } else if(rtl==1){
        for (int i=len;i>0;i--){
            if (array[i]==search) return i+1;
        }
    }
    return 0;
}

char **FileReadArray(char File[]){
    int lines_allocated=1;
    int max_line_len=4096;
    char **words=(char **)malloc(sizeof(char*)*lines_allocated);
    if (words==NULL){
        fprintf(stderr,"~!Error@InitArray, Cannot Allocate Initial Array.\n    Skipping \"%s\".",File);
        return 0;
    }
    FILE *fp = fopen(File,"r");
    if (fp == NULL){
        fprintf(stderr,"~!Error@fopen, Cannot open \"%s\", Skipping.\n",File);
        return 0;
    }
    int i;
    int j;
    for (i=1;1;i++){
        if (i >= lines_allocated){ /* Have we gone over our line allocation? */
            int new_size;
            new_size=lines_allocated+1; /* Add to allocation and re-allocate */
            words=(char **)realloc(words,sizeof(char*)*new_size);
            if (words==NULL){
                fprintf(stderr,"~!Error@ReAllocate, Failed to allocate new array. Skipping \"%s\".\n",File);
                return 0;
            }
            lines_allocated=new_size;
        }
        words[i]=malloc(max_line_len); /* Allocate space for the next line */
        if (words[i]==NULL){
            fprintf(stderr,"~!Error@ReAllocate, Failed to allow new line. Skipping \"%s\".\n",File);
            return 0;
            }
        if (fgets(words[i],max_line_len-1,fp)==NULL) break;
        for (j=strlen(words[i])-1;j>=0 && (words[i][j]=='\n' || words[i][j]=='\r');j--);//Strip CR+LF from end of line.
        words[i][j+1]='\0';
    }
    (int)words[0]=i;
    fclose(fp);
    return words;
}
    //ArrayInfGetSections,
    //ArrayInfGetElement, Parse Array, Return Element if exists
    //CleanString, Strip Quotes, Comments, etc.

Output:

Reading...Done
Cleaning...
|"$WINDOWe|"$WINDOWe|
|Systs|Systs|
|{4D36E97d|{4D36E97d|
|%MSFT%|%MSFT%|
|1|1|
|06/21/20r|06/21/20r|
|win6|win6|
| 3426| 3426|
| 3426| 3426|
| 3426| 3426|
| 3426| 3426|
| 3426| 3426|
| 12| 12|
|*|*|
|*|*|
|GENDEV_SYS,%|GENDEV_SYS,%|
|ACC_SYS,%|ACC_SYS,%|
|ACER_SYS,%|ACER_SYS,%|
|ALI_SYS,%|ALI_SYS,%|
|AMD_SYS,%|AMD_SYS,%|
|ATI_SYS,%|ATI_SYS,%|
|COMPAQ_SYS,%|COMPAQ_SYS,%|
|CONTAQ_SYS,%|CONTAQ_SYS,%|
|CYRIX_SYS,%|CYRIX_SYS,%|
|HITACHI_SYS,%|HITACHI_SYS,%|
|HP_SYS,%|HP_SYS,%|
|IBM_SYS,%|IBM_SYS,%|
|INTEL_SYS,%|INTEL_SYS,%|
|MICRON_SYS,%|MICRON_SYS,%|
|NATSEMI_SYS,%|NATSEMI_SYS,%|
|NEC_SYS,%|NEC_SYS,%|
|NVIDIA_SYS,%|NVIDIA_SYS,%|
|OPTI_SYS,%|OPTI_SYS,%|
|SERVERWORKS_SYS,%|SERVERWORKS_SYS,%|
|SIS_SYS,%|SIS_SYS,%|
|SMSC_SYS,%|SMSC_SYS,%|
|TOSHIBA_SYS,%|TOSHIBA_SYS,%|
|ULI_SYS,%|ULI_SYS,%|
|UMC_SYS,%|UMC_SYS,%|
|VIA_SYS,%|VIA_SYS,%|
|VLSI_SYS,%|VLSI_SYS,%|
|LEGACY_SYS,%|LEGACY_SYS,%|
...

 

My primary issue is with StrSplit, becasue it keeps giving me corrupted results.

Edited by Biatu

What is what? What is what.

Share this post


Link to post
Share on other sites



There are Windows APIs to handle INF and INI files. For INF files you'd use the Setupapi.dll functions.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Yes I am aware of those APIs, however my goal is not to use those APIs yet.


What is what? What is what.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now