Salve a tutti,
cerco di spiegarvi la situazione in cui mi trovo... devo realizzare un programma che prenda delle stringhe, le passi a Google accoppiate e ritorni il numero delle pagine trovate (su cui poi dovro' fare altri calcoli). Il problema è che ho già il codice che interroga Google e, dal momento che vorrei evitare di riscrivere tutto daccapo mi chiedevo se fosse fattibile creare un file header da includere poi nel file principale.
Il programma è il seguente
codice:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <curl/curl.h>
#include <json/json.h>

struct wd_in {
  size_t size;
  size_t len;
  char *data;
};

/* This function is registered as a callback with CURL.  As the data
   from the requested webpage is returned in chunks, write_data is
   called with each chunk.  */
static size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) {

	struct wd_in *wdi = userp;

	while(wdi->len + (size * nmemb) >= wdi->size) {
	/* check for realloc failing in real code. */
		wdi->data = realloc(wdi->data, wdi->size*2);
		wdi->size*=2;
	}

	memcpy(wdi->data + wdi->len, buffer, size * nmemb);
	wdi->len+=size*nmemb;

	return size * nmemb;

}

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

	CURL *curl;
	CURLcode res;
	struct wd_in wdi;
	struct json_object *new_obj;
	const char *URL = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
	char uri[1024];
	const char *bs = "+";
	char *key; struct json_object *val;
	int i;

	memset(&wdi, 0, sizeof(wdi));


	if (argc < 2){
		printf("Usage: %s <search terms>\n", argv[0]);
	}else{

		strcpy(uri, URL);

		for(i = 1; i < argc; i++){

			strcat(uri, argv[i]);

			strcat(uri, bs);
		}

		/* Get a curl handle.  Each thread will need a unique handle. */
		curl = curl_easy_init();

		if(NULL != curl) {
			wdi.size = 1024;
		/* Check for malloc failure in real code. */
			wdi.data = malloc(wdi.size);

		/* Set the URL for the operation. */
			curl_easy_setopt(curl, CURLOPT_URL, uri);

		/* "write_data" function to call with returned data. */
			curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);

		/* userp parameter passed to write_data. */
			curl_easy_setopt(curl, CURLOPT_WRITEDATA, &wdi);

		/* Actually perform the query. */
			res = curl_easy_perform(curl);

		/* Check the return value and do whatever. */

		/* Clean up after ourselves. */
			curl_easy_cleanup(curl);

		}else {
			fprintf(stderr, "Error: could not get CURL handle.\n");
			exit(EXIT_FAILURE);

		}

		/* Now wdi.data has the data from the GET and wdi.len is the length
		   of the data available, so do whatever. */

		new_obj = json_tokener_parse(wdi.data);
 
		struct lh_entry *entry = json_object_get_object(json_object_object_get((json_object_object_get(new_obj,"responseData")),"cursor"))->head;
    long results_count = -1;

		for(; ({if(entry) { key = (char*)entry->k; val = (struct json_object*)entry->v; } entry; }); entry = entry->next ) {
      if (strcmp(key, "estimatedResultCount") == 0)
        results_count = atoi(json_object_get_string(val));
/*			printf("\nkey: %s\nval: %s\n",key,json_object_get_string(val)); */
    }
    if (results_count == -1)
      printf("error trying to fetch result count, sorry!\n");
    else
      printf("%ld\n", results_count);

		/* cleanup wdi.data buffer. */
		free(wdi.data);

	}

	return EXIT_SUCCESS;

}
C'è una cosa che non riesco a capire: dal momento che questo codice nasceva come "stand-alone" è principalmente un main che sfrutta una struttura e una funzione ausiliaria. Se volessi scriverne un header dovrei includervi anche la struttura e questa seconda funzione? Pensavo di no, dal momento che entrambe vengono utilizzate solo durante l'esecuzione di questo codice ma sono in cerca di conferme.
Ma soprattutto: come gestisco il main? Lo devo trasformare in una funzione? E se si, come fare?

Scusate la prolissità ma sono davvero impantanato e non so come uscirne!