Jump to content

C, inf parsing help


Recommended Posts

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:


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


#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";
    char **data=FileReadArray(Inf);//Read File into Array.
    int i=(int)data[0];
    for (i;i>=1;i--){//Free Array
    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.

        //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){

        //Strip Leading and Trailing Spaces.

        //Split Elements
        if (cPos>0){
         //   printf("%d|%s|\n\n",cPos,RightStr);

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

char *StrLeft(const char str[],int count){
    int iStrip=count-1;
    return str;
char *StrRight(const char str[],int count){
    int iStrip=count;
    return str;

char *StrSplit(const char str[],int count){
    char **split[2];
    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--;

  // 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;
        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.
    return words;
    //ArrayInfGetElement, Parse Array, Return Element if exists
    //CleanString, Strip Quotes, Comments, etc.


| 3426| 3426|
| 3426| 3426|
| 3426| 3426|
| 3426| 3426|
| 3426| 3426|
| 12| 12|


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

Edited by Biatu

What is what? What is what.

Link to comment
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

Link to comment
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...