Hola,
escribí lo mejor que pude el siguiente programa en Windows:
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <process.h>
#include <direct.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
#define MAX_THREAD 1
#define BUFSIZE 4096
#define WIN32_LEAN_AND_MEAN
typedef struct {
char* filein;
char* palabra;
CRITICAL_SECTION * wcs;
char* myset;
HANDLE* semaforo;
}param;
extern unsigned int WINAPI myRun(LPVOID lpParam);
void lock(CRITICAL_SECTION * cs);
void unlock(CRITICAL_SECTION * cs);
int elabora(char* path, char* palabra, HANDLE semaforo, char* myset, char* fileOut);
int busquedaPalabra(char* path, char* palabra, HANDLE sem, char* myset, CRITICAL_SECTION * wcs, HANDLE* semaforo);
void produceFileOut(char* fileOut, char* myset);
char *getPath(const char* filename);
int main (int argc, char* argv[]) {
char* path = argv[1];
char* palabra = argv[2];
char* fileOut = argv[3];
char* myset = " ";
CRITICAL_SECTION * wcs;
HANDLE semaforo;
InitializeCriticalSection(&wcs);
semaforo = CreateSemaphore(NULL, MAX_THREAD, MAX_THREAD, NULL);
elabora(path, palabra, semaforo, myset, fileOut);
CloseHandle(semaforo);
DeleteCriticalSection(&wcs);
return 0;
}
void lock(CRITICAL_SECTION * cs){
EnterCriticalSection(&cs);
}
void unlock(CRITICAL_SECTION * cs){
LeaveCriticalSection(&cs);
}
int elabora(char* path, char* palabra, HANDLE semaforo, char* myset, char* fileOut) {
int i;
for(i=0; i < MAX_THREAD; i++ )
WaitForSingleObject(semaforo,INFINITE);
printf("La cadena a buscar no se encuentra en el directorio especificado \n");
else
produceFileOut(fileOut, myset);
return 0;
}
int busquedaPalabra(char* path, char* palabra, HANDLE sem, char* myset, CRITICAL_SECTION * wcs, HANDLE* semaforo) {
HANDLE hthread;
char *puntero;
char* dirname=getPath(path);
WIN32_FIND_DATA file1;
HANDLE fd=INVALID_HANDLE_VALUE ;
fd=FindFirstFile(dirname,&file1);
if(fd==INVALID_HANDLE_VALUE )
return -1;
while(FindNextFile(fd,&file1)!=0) {
if((file1.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)!=0) {
if((strcmp(file1.
cFileName,".")!=0) && (strcmp(file1.
cFileName,"..")!=0)) {
char* subdir=dirname;
its--;
strcat(puntero
, file1.
cFileName);
busquedaPalabra(puntero,palabra,sem,myset,wcs,semaforo);
}
}
else {
char* nameF=dirname;
its--;
strcat(puntero
, file1.
cFileName);
WaitForSingleObject(semaforo,INFINITE);
param.filein=puntero;
param.palabra=palabra;
param.myset=myset;
param.wcs=wcs;
param.semaforo=&semaforo;
hthread= (HANDLE)_beginthreadex(NULL,0,&myRun,(void *)(param),0,NULL );
CloseHandle(hthread);
}
}
FindClose(fd);
return 0;
}
void produceFileOut(char* fileOut, char* myset) {
FILE *fout;
fout
= fopen (fileOut
, "wt");
}
char *getPath(const char* filename) {
DWORD retval = 0;
char fullPath[BUFSIZE]=TEXT("");
char valor;
LPTSTR lpszFilePart = NULL;
retval = GetFullPathName(filename, BUFSIZE, fullPath, &lpszFilePart);
valor= fullPath;
return valor;
}
unsigned int WINAPI myRun(LPVOID lpParam) {
Param *pa=lpParam;
char* pos;
char* s;
FILE *fin;
char* ret;
fin
= fopen(pa.
filein(), "wt");
while(getline(fin,s)) {
if (pos!=npos) {
lock(*pa.wcs);
ret
=strcat(pa.
myset, pa.
filein);
break;
}
}
unlock(*pa.wcs);
ReleaseSemaphore(*(pa.semaforo),1L,NULL);
return 0;
}
y como buenamente puedo lo paso a Ubuntu:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <pthread.h>
#include <limits.h>
#include <unistd.h>
#include <semaphore.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <dirent.h>
#define MAX_THREAD 1
#define BUFSIZE 4096
#define WIN32_LEAN_AND_MEAN
typedef struct {
char* filein;
char* palabra;
pthread_mutex_t * wcs;
char* myset;
sem_t semaforo;
}param;
extern unsigned int WINAPI myRun(LPVOID lpParam);
void lock(pthread_mutex_t * cs);
void unlock(pthread_mutex_t * cs);
int elabora(char* path, char* palabra, sem_t semaforo, char* myset, char* fileOut);
int busquedaPalabra(char* path, char* palabra, sem_t sem, char* myset, pthread_mutex_t * wcs, sem_t semaforo);
void produceFileOut(char* fileOut, char* myset);
char *getPath(const char* filename);
int main (int argc, char* argv[]) {
char* path = argv[1];
char* palabra = argv[2];
char* fileOut = argv[3];
char* myset = " ";
pthread_mutex_t * wcs;
sem_t semaforo;
pthread_mutex_init(&wcs);
sem_init(&semaforo, MAX_THREAD, NULL);
elabora(path, palabra, semaforo, myset, fileOut);
sem_post(&semaforo);
pthread_mutex_destroy(&wcs);
return 0;
}
void lock(pthread_mutex_t * cs){
pthread_mutex_lock(&cs);
}
void unlock(pthread_mutex_t * cs){
pthread_mutex_unlock(&cs);
}
int elabora(char* path, char* palabra, sem_t semaforo, char* myset, char* fileOut) {
int i;
for(i=0; i < MAX_THREAD; i++ )
wait(&semaforo);
printf("La cadena a buscar no se encuentra en el directorio especificado \n");
else
produceFileOut(fileOut, myset);
return 0;
}
int busquedaPalabra(char* path, char* palabra, HANDLE sem, char* myset, pthread_mutex_t * wcs, sem_t semaforo) {
pthread_t hthread;
char *puntero;
char* dirname=getPath(path);
DIR file1;
struct stat statbuf;
struct dirent *direntp;
int fd=NULL;
fd=readdir(dirname,&file1);
if(fd==NULL )
return -1;
direntp = readdir(file1);
stat(file1, &statbuf);
while(readdir(fd,&file1)!=0) {
if(statbuf.st_mode & S_IFDIR) {
if((direntp.d_name != 0) && (direntp.d_name != 0)) {
char* subdir=dirname;
its--;
strcat(puntero
, file1.
cFileName);
busquedaPalabra(puntero,palabra,sem,myset,wcs,semaforo);
}
}
else {
char* nameF=dirname;
its--;
strcat(puntero
, file1.
cFileName);
wait(&semaforo);
param.filein=puntero;
param.palabra=palabra;
param.myset=myset;
param.wcs=wcs;
param.semaforo=&semaforo;
pthread_create((hthread, NULL, &myRun, (void *)(param)));
pthread_kill(hthread);
}
}
closedir(fd);
return 0;
}
void produceFileOut(char* fileOut, char* myset) {
FILE *fout;
fout
= fopen (fileOut
, "wt");
}
char *getPath(const char* filename) {
getcwd(filename,BUFSIZE);
}
unsigned int WINAPI myRun(LPVOID lpParam) {
Param *pa=lpParam;
char* pos;
char* s;
FILE *fin;
char* ret;
fin
= fopen(pa.
filein(), "wt");
while(getline(fin,s)) {
if (pos!=npos) {
lock(*pa.wcs);
ret
=strcat(pa.
myset, pa.
filein);
break;
}
}
unlock(*pa.wcs);
sem_destroy(*(pa.semaforo));
return 0;
}
El problema es que no se como sustituir la función WINAPI con alguna que funcione en ubuntu y tenga la misma funcionalidad, o adaptar el programa entero a ubuntu de la mejor manera posible.
Por cierto, el lenguaje de programación es C, no C++
Muchas gracias a todos.
Lh: Utiliza las etiquetas GeSHi para poner código.
WINAPI no es una función, en este caso es una macro predefinida que implica una convención de llamada (http://unixwiz.net/techtips/win32-callconv.html). En este caso __stdcall (http://msdn.microsoft.com/en-us/library/zxk0tw93%28VS.80%29.aspx).
Para mas información lee esto:
Calling conventions (http://en.wikipedia.org/wiki/X86_calling_conventions)
y tendrás claro como seguir.
En cuanto al código en general, no lo he leído todo pero a simple vista podrías ordenarlo utilizando compilación condicional (http://es.wikipedia.org/wiki/Preprocesador_de_C#Compilaci.C3.B3n_condicional)
Saludos
Gracias.
He aprendido mucho