| |||||||||||
| |||||||||||
Complete example - regex, realloc, string and file handling, etc
Memory Management example from a Well House Consultants training course
More on Memory Management [link]
Source code: loglook.c Module: C212
/* Here's a complete sample program in C, written at the end of this week's C
and C++ training, as several delegates will be using some C++ but more C, and we were looking to pull all the various elements together. The program opens and reads a web server access log line by line, and checks if each line in turn is a search arrival from Google. If it is, the data is stored into a structure for later processing. In this sample, I chose not to do a great deal with the data once we had read it in, as the reading and settig up of the structure is a good example, and the further processing would be more code along the same lines. But I did report on a sample of 20 of the records stored, to show that they were set up correctly. */ #include <stdlib.h> #include <stdio.h> #include <string.h> #include <regex.h> #define CAPSTEP 10 #define NSAMPS 20 /* Sample data line ... */ /* 98.249.0.47 - - [28/Jun/2010:03:31:47 +0100] "GET /mouth/158_MySQL- LEFT-JOIN-and-RIGHT-JOIN-INNER-JOIN-and-OUTER-JOIN.html HTTP/1.1" 200 65651 "http://www.google.com/search?hl=&q=left+join+vs+right+join&sour ceid=navclient-ff&rlz=1B3GGLL_enUS358US358&ie=UTF-8" "Mozilla/5.0 (Win dows; U; Windows NT 6.1; en-US; rv:1.9.2.4) Gecko/20100611 Firefox/3.6 .4 GTB7.0" */ typedef struct { char * ip; char * searchstring; int status; int size; } Arrival ; /* Extract a character string from within another string and return its address on the heap (so that it remains in memory until released with free or cfree, or at the end of the program */ char * extract (char * start, int len) { char * here = malloc((len + 1) * sizeof(char)); /* Allocate Memory */ strncpy(here,start,len); /* Copy string to the heap */ *(here+len) = '\0'; /* Add null terminator */ return here; } main () { FILE *fh; Arrival *google; /* Pointer to an array of arrivals */ int capacity = 0; /* Current capacity of arrivals area */ char buffer[12049]; /* Read buffer and counters */ int records = 0; int gooey_records = 0; int stepper; int k; /* Regular Expression to match arrival */ char * querystring = "\\.google\\..*[?&]q=([^\"& ]+)"; regex_t query; regmatch_t matches[2]; char *qs; /* Regular expression for status and byte count */ char * resultstring = "\" ([0-9]+) ([0-9]+) \""; regex_t result; regmatch_t info[3]; int fspace; /* Temporary variables for setting up each Arrival record */ int status; char * ipadd; char * ipheap; /* Open File */ fh = fopen("ac_20100630","r"); /* Should read name from command line? */ /* Set up for a whole lot (unknown number) of arrivals */ google = calloc(0,sizeof(Arrival)); regcomp(&query,querystring,REG_EXTENDED); /* Compile regexes just once! */ regcomp(&result,resultstring,REG_EXTENDED); /* Loop through file */ while (fgets(buffer,12048,fh)) { records ++; /* If it's a google arrival, find the bits we want in the line */ if (! regexec(&query,buffer,2,matches,0)) { regexec(&result,buffer,3,info,0); ipadd = strtok(buffer," "); /* Extract those bits into clean variables */ ipheap = malloc((strlen(ipadd)+1) * sizeof(char)); strcpy(ipheap,ipadd); status = atoi(extract(buffer + (int)info[1].rm_so,3)); fspace = atoi(extract(buffer + (int)info[2].rm_so, (int)info[2].rm_eo - (int)info[2].rm_so)); qs = extract(buffer + (int)matches[1].rm_so, (int)matches[1].rm_eo - (int)matches[1].rm_so); /* Do we have room already allocated to store result? If not, increase capacity */ if (capacity <= gooey_records) { google = realloc (google, (capacity + CAPSTEP) * sizeof(Arrival)); capacity += CAPSTEP; } /* If it's an arrival, add it to saved ones */ (google+gooey_records)->status = status; (google+gooey_records)->size = fspace; (google+gooey_records)->searchstring = qs; (google+gooey_records)->ip = ipheap; gooey_records++; } } /* Prove it's there! - Sample it*/ stepper = gooey_records / NSAMPS; for (k=0; k<gooey_records; k += stepper) { printf ("%6d %3d %7d %16s %s\n", k, (google+k)->status , (google+k)->size , (google+k)->ip , (google+k)->searchstring ) ; } printf("There were %d records\n",records); printf("There were %d Google arrivals\n",gooey_records); } /* Here is some sample output: wizzard:c graham$ ./loglook 0 200 73390 88.77.57.79 perl+script+md5 766 200 35818 116.197.178.83 perl+how+to+unset+a+env+variable 1532 200 116072 115.113.232.135 telnet+program+using+tcl 2298 200 65755 202.164.34.26 inner+join+in+mysql 3064 200 13699 72.179.17.70 php+add+1+variable 3830 200 57144 24.131.249.242 programming+an+image+uploader 4596 200 17786 168.159.160.57 monitoring+process+using+perl 5362 200 17493 196.213.166.66 python+elif 6128 200 16317 213.55.110.198 c%2B%2B+function+example 6894 200 22856 221.120.194.162 learn+linux+administration 7660 200 19953 192.71.200.230 java+running+system+commands 8426 200 66187 82.69.172.105 left+join+inner+join 9192 200 25809 222.124.114.65 Python+thread 9958 200 13875 188.129.69.221 python+gui+example 10724 200 30441 67.127.45.246 php+adding+%22%5C%22 11490 200 38712 208.50.255.30 load+word+ribbon+with+database+connection 12256 200 20468 167.206.168.187 splitting+string+in+python+example 13022 200 35531 198.206.219.38 what+is+_+in+perl 13788 200 33595 129.74.86.78 perl+stdout+%3E 14554 200 37758 41.238.231.219 debug+cgi+perl+file 15320 200 17356 202.32.73.47 dopost+response There were 157995 records There were 15325 Google arrivals wizzard:c graham$ */ Learn about this subject
This module and example are covered on the following public courses:
* Learning to Program in C * Learning to program in C and C++ * Programming in C * C and C++ Programming * Learning to program in C and C++ * C and C++ Programming Also available on on site courses for larger groups Books covering this topic
Yes. We have over 700 books in our library. Books
covering C and C++ are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "Memory Management" training module. You'll find a description of the topic and some
other closely related examples on the "Memory Management" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Web site author
Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. |
| ||||||||||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |