![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Programming JunkHere is some code that I have come up with over the years. Some of this stuff I lost serious hair over; some I just thought may be useful to others. I apologize for lack of documentation, but most of this stuff was eeked out under heavy deadline. If you really need something here & just can't figure out how to use it, throw me some mail & I'll try to help (time permitting, of course). Enjoy. (By the way, you should always step through someone else's code before you unleash it on your project/machine. If you do happen to use any of these classes / functions without first testing them, you do so at your own risk. In no way will I be responsible for any damage, misfortune, shifting of tectonic plates, or anything else bad that may occur through the use of the code samples contained herein) MFC / Win32
UNIX
Berkeley Sockets
Windows 3.X stuff
Determining if a file is a directory #include <sys/stat.h> int is_dirfile(char *path) { struct stat s; if (stat(path, &s) == -1) return 0; else if ((s.st_mode & S_IFMT) == S_IFDIR) return 1; return 0; } Determining if a file is a regular file #include <sys/stat.h> int is_regfile(char *path) { struct stat s; if (stat(path, &s) == -1 || (s.st_mode & S_IFMT) == S_IFDIR || !(s.st_mode & S_IFREG)) return 0; return 1; } Recursively listing directory contents #include <stdio.h> #include <dirent.h> #include <string.h> static void recursive_list() { DIR *dirp; struct dirent *direntp; if ((dirp = opendir( "." )) != NULL) { while ((direntp=readdir(dirp)) != NULL) { if (is_dirfile(direntp->d_name)) { /* don't follow . or .. */ if (strcmp(direntp->d_name,".") && strcmp(direntp->d_name,"..")) { printf("%s:\n", direntp->d_name); /* if we found a directory, change to it & recurse */ if (chdir(direntp->d_name)==0) { recursive_list(); chdir(".."); } } } else { printf("\t%s\n",direntp->d_name); } } closedir( dirp ); free(direntp); } } Drawing pixmaps on an XmDrawingArea widget void load_pixmap(Display *dpy, char *filename, Pixmap *p) { if (*filename != NULL && access(filename,F_OK) == 0) { Screen *scr = XDefaultScreenOfDisplay(dpy); *p = XmGetPixmapByDepth(scr, filename, XBlackPixelOfScreen(scr), XWhitePixelOfScreen(scr), 8); if (*p == XmUNSPECIFIED_PIXMAP) printf("Unable to load %s\n",filename); } else printf("Unable to locate %s\n",filename); } void draw_pixmap(Display *dpy, Widget dest, GC gc, Pixmap p, int x, int y, int width, int height) { XCopyArea(dpy, p, XtWindow(dest), gc, 0, 0, width, height, x, y); } Creating a TCP/IP socket server #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> int sd,sdcur; struct sockaddr_in sin; int addrlen; . . memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; sin.sin_port = htons(PORT); /* get socket */ if ((sd=socket(sin.sin_family, SOCK_STREAM, 0)) == -1) { perror("unable to get socket"); exit(1); } if (bind(sd, &sin, sizeof(sin)) == -1) { perror("unable to bind socket"); exit(1); } /* listen for client connections */ if (listen(sd, 5) == -1) { perror("listen"); exit(1); } /* accept a connection */ sdcur=accept(sd, &cin, &addrlen); . . /* now, talk to client on sdcur using send/recv or read/write */ . . close(sdcur); close(sd); /* close socket, or loop back and accept another connection */ Creating a TCP/IP socket client #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> struct sockaddr_in sin; struct hostent *hp; . . /* get host entry */ if ((hp=gethostbyname("corwest")) == 0) /* your server name goes here */ { perror("unable to get host info"); exit(1); } memset(&sin, 0, sizeof(sin)); dom_inet.sin_family = AF_INET; dom_inet.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr; dom_inet.sin_port = htons(0x9999); /* your port number goes here */ /* get socket */ if ((sd=socket(sin.sin_family, SOCK_STREAM, 0)) == -1) { perror("unable to get socket"); exit(1); } /* connect to server */ if (connect(sd, &sin, sizeof(sin)); { /* do your thing here */ } close(sd); #include <fcntl.h> void unblock_socket(int sd) { int flags; /* get current socket flags */ if ((flags=fcntl(sd, F_GETFL)) == -1) { perror("GETFL"); exit(1); } /* set socket to non-blocking */ flags |= O_NDELAY; if (fcntl(sd, F_SETFL, flags) == -1) { perror("SETFL"); exit(1); } } Waiting for an un-blocked socket to be writeable void waitfor_send_ready(int sd) { fd_set wfds; int response; do { FD_ZERO(&wfds); FD_SET(sd, &wfds); if ((response=select(sd+1, NULL, &wfds, NULL, NULL)) == -1) { perror("select write"); exit(1); } } while (!(response != 0 && FD_ISSET(sd, &wfds))); } Checking if an un-blocked socket has incoming data int recv_ready(int sockfd) { fd_set rfds; int response; FD_ZERO(&rfds); FD_SET(sockfd, &rfds); if ((response=select(sockfd+1, &rfds, NULL, NULL, NULL)) == -1) { perror("select read"); exit(1); } return (response != 0 && FD_ISSET(sockfd, &rfds)); } Spawning an external app struct LOADPARMS { WORD segEnv; LPSTR lpszCmdLine; LPWORD lpwShow, lpwReserved; }; HINSTANCE hinst; struct LOADPARMS parms; WORD awShow[2] = {2,SW_SHOWNORMAL}; parms.lpwReserved=(LPWORD)NULL; parms.segEnv=0; parms.lpszCmdLine = (LPSTR)" c:\\mydoc.txt"; parms.lpwShow = (LPWORD)awShow; hinst = LoadModule((LPSTR)"notepad.exe",&parms); if ((UINT)hinst < 32) MessageBox((LPSTR)"Couldn't spawn editor"); Getting the HWND of a spawned app HWND hwChild; BOOL CALLBACK GetWins(HWND hWnd, LONG lParam) { if ((HINSTANCE)GetWindowWord(hWnd, GWW_HINSTANCE) == (HINSTANCE)LOWORD(lParam)) { hwChild = hWnd; return FALSE; } return TRUE; } HWND HinstToHwnd(HINSTANCE target) { WNDENUMPROC lpEnumProc = (WNDENUMPROC)MakeProcInstance((FARPROC)GetWins, AfxGetApp()->m_hInstance); hwChild = 0; EnumWindows(lpEnumProc, MAKELONG(target,0)); FreeProcInstance((FARPROC)lpEnumProc); return hwChild; }
|
Please send any comments or suggestions to grimmd@oocities.com