[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] string replace i C



On Tue, 27 Feb 2007, Peter Maersk-Moller wrote:

Jeg soeger et eksempel paa en implenetering af replace i C virkende paa strings vaerende nul-termineret arrays af char.

Jeg har noget der ligner...

Med replace forstaar jeg en funkion, hvor man kan soege i str1 efter str2 og erstatte str2 med str3 i str1 og derved faa str4. Lidt i stil med

str4=`echo $str1 |sed -e 's/'$str2'/'$str3'/g'`

Mit lille program er beregnet på on-the-fly patching af Knoppix CD'er (så man kunne konfigurere før download), men den kræver at str2 og str3 er lige store, og de behøver ikke være NULL termineret.


If any help..

/*
 * binsearchreplace file1 file2
 * While reading from stdin a pattern matching
 * file1 will be replaced with file2.
 * Both files must have same size.
 *
 * Copyright © 2004 Hans Schou.
 * License: GPL
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

size_t readfile(char *name, unsigned char **buffer)
{
	struct stat buf;
	FILE *fd;

	if (!lstat(name, &buf)) {
		fprintf(stderr, "File Open: %s, size:%d\n", name, buf.st_size);
		*buffer = (unsigned char *)malloc(buf.st_size);
		fd = fopen(name, "r");
		fread( *buffer, buf.st_size, 1, fd);
		fclose(fd);
	} else {
		fprintf(stderr, "Error, can not open file: %s\n", name);
		exit(2);
	}

	return buf.st_size;
}

int idx = 0;
size_t matchlen = 0;
size_t substlen = 0;
unsigned char *match = NULL;
unsigned char *subst = NULL;

int searchreplace(int c)
{
	size_t written = 0;

	if (c != EOF) {
		if (c == match[idx]) {
			/* current character matches */
			idx++;
			if (idx == matchlen) {
				/* got full match, flush substitution block */
				written = fwrite( subst, matchlen, 1, stdout);
				idx = 0;
			}
		} else {
			/* current character does not match */
			if (idx) {
				/* some previous part matching, flush that */
				written = fwrite( match, idx, 1, stdout);
				idx = 0;
			}
			fputc(c, stdout);
			written++;
		}
	}

	return written;
}

int main(int argc, char **argv)
{

	if (argc < 3) {
		fprintf(stderr, "%s by Hans Schou 2004.\n", argv[0]);
		fprintf(stderr, "Usage:\n");
		fprintf(stderr, "\tfoo | %s search-file replace-file | bar\n", argv[0]);
		fprintf(stderr, "\tcat foo.iso | %s syslinux.old syslinux.new | cdrecord -\n", argv[0]);
		return 1;
	}

	matchlen = readfile(argv[1], &match);
	substlen = readfile(argv[2], &subst);

	if (!matchlen) {
		fprintf(stderr, "Error, the file '%s' is empty.\n", argv[1]);
		exit(3);
	}

	if (matchlen != substlen) {
		fprintf(stderr, "Error, file size not equal\n");
		exit(4);
	}

	while (!feof(stdin)) {
		searchreplace( fgetc(stdin) );
	}

	return 0;
}

--
Horsebakken 78, DK-2400 København NV, Phone: +45 2264 8020
-------------------------------------------------------------
OLPC: Does not ship with: Java or Flash
		http://wiki.laptop.org/go/BTest_Software_Notes


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2007-03-01, 02:01 CET [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *