LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-10-2010, 01:58 PM   #1
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
[C] help parsing lines into chunks and writing each chunk


My C foo is terrible! I am working with some code which reads lines from a file and then reformats the lines and writes them to a new file.

The input lines look like this:
Code:
+[NEOTEST?]
+[NEOTEST]
+[TESTNOTEQUAL | TESTINEQUAL2]
After reformatting they are written out like this:
Code:
NEOTEST&?<$=1253
NEOTEST&<$=1187
TESTNOTEQUAL | TESTINEQUAL2&<$=1694
I need to alter the following (somewhat stripped down) code, so that it breaks the last example input into chunks like this:
Code:
TESTNOTEQUAL&<$=1694
TESTINEQUAL2&<$=1694
Note that the line number out put should be the same for both items.

Here's the code which produces the original output:
Code:
int splitaine (char *src)
{		/* prepares the temp file */
	unsigned long size_this = 0;
	int patterns = 0, templates = 0, bytecount = 0;
	char *pos, *pos1;
	fpos_t tpos;
	char *line, *next;
	register int linenr = -1;
	bool atomic = FALSE, that_used = FALSE;
	unsigned short int i, j;
	
	aine = fopen (src, "r+");  /* into the template and pattern files. */

	line = (char*) malloc (sizeof(char)*MAX_LINE_SIZE);
	next = (char*) malloc (sizeof(char)*MAX_LINE_SIZE);
	fgets (line, MAX_LINE_SIZE, aine);

	while (fgets (next, MAX_LINE_SIZE, aine)) {
		/* we now make the temp.txt into unsorted.txt
  		 * and templates.txt also we exchange some tags for shorthand tags. */
		linenr++;
		replacer (line);
    
		if (strstr (line, "+[")) {
			size++;
			size_this++;
			if (output_check) 
				panic ("Error before line %d\n", linenr);
			else output_check = TRUE;
		
			that_used = FALSE;
			while (!strchr (line, ']')) {
				strcpy (line + strlen (line), next);
				replacer (line);
				linenr++;
				fgets (next, MAX_LINE_SIZE, aine);
			}
			/*  start of where loop needs to go???*/
                     /* do { */
			/*  I need lines like: 'something | something else | something more' */
			/* broken up into chunks of content between '|' pipe chars */
			patterns++;
			*(strchr (line, ']') + 1) = '\0';
			replace (line, "+[", "");
			
			replace (line, ".", "&.");
			replace (line, "?", "&?");
			replace (line, "!", "&!");
			
			if (replace (line, "]", "")  &&  !strchr (line, '&') )
				strcat (line, "&");

			
				if (strchr (line, '*') || strchr (line, '_')) { 
					fprintf (unsorted_d_f, "0}%s", line);
					atomic = FALSE;
				}
				else { 
					fprintf (unsorted_a_f, "%s", line);
					atomic = TRUE;
				}
			/* end of where I think loop needs to go */
                    /* } while (strchr (line, '|')); */  /* any multiple-choice inputs? */
			bytecount += strlen (line) - 3;
		}

		strcpy (line, next);
	}
	printf ("OK, added %lu patterns\n", size_this);
	free (line);
	free (next);
	fclose (aine);
	return (1);
}
I actually got a weird loop half-way working close to the points indicated in the code comments, but it would never exit -it kept appending scrap + scrap forever.
Can anyone kindly point me in the right direction?
 
Old 09-11-2010, 08:35 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Does it have to be done in "C" ? I.e. why not in, say, Perl ?
 
Old 09-11-2010, 08:49 AM   #3
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Original Poster
Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
No, it's a C program with lots of other functions -I just posted the cut-down part I'm having trouble with
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Need help writing a script to remove lines in which >X% of the characters are dashes kmkocot Linux - Newbie 14 12-02-2009 11:27 PM
paste commonly used chunks of text? Chriswaterguy Linux - Software 10 11-30-2009 07:28 AM
Diff for files with resequenced chunks johnsfine Linux - Software 1 04-16-2009 10:43 PM
Error while parsing chunk header. Mplayer error only under Slack vdemuth Linux - Software 5 02-19-2007 11:09 AM
rute - in no chunks format? itsjustme Linux - General 1 04-18-2005 11:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration