mux/src/tools/dbpuller.c

Go to the documentation of this file.
00001 /**************************************************************************************************
00002  * Db puller - used to pull data from a MUX flatfile and dump it into a file in @decompile format *
00003  * Version: 1.01                                                                                  *
00004  * By: Ashen-Shugar (08/16/2005)                                                                  *
00005  * Modifications: List modifications below                                                        *
00006  *     11/02/05 : filenames are now saved with _<dbref> extensions.                               *
00007  *                the name of the object starts the file with a '@@' comment prefix.              *
00008  *                                                                                                *
00009  **************************************************************************************************/
00010 
00011 #include <stdio.h>
00012 #include <stdlib.h>
00013 #include <string.h>
00014 #include <ctype.h>
00015 
00016 /* Standard MUX 2.x definitions */
00017 /* This should be over twice the size of the LBUF.  If it's not, it'll misbehave */
00018 #define MALSIZE 16535
00019 /* This should be SBUF_SIZE + 1. If it's not, it'll coredump*/
00020 #define SBUFSIZE 65
00021 
00022 stricmp(char *buf1, char *buf2)
00023 {
00024     char *p1, *p2;
00025 
00026     p1 = buf1;
00027     p2 = buf2;
00028     while ((*p1 != '\0') && (*p2 != '\0') && (tolower(*p1) == tolower(*p2))) {
00029         p1++;
00030         p2++;
00031     }
00032     if ((*p1 == '\0') && (*p2 == '\0'))
00033         return 0;
00034     if (*p1 == '\0')
00035         return -1;
00036     if (*p2 == '\0')
00037         return 1;
00038     if (*p1 < *p2)
00039         return -1;
00040     return 1;
00041 }
00042 
00043 int main(int argc, char **argv) 
00044 {
00045    FILE *f_muxflat, *f_mymuxfile, *f_muxattrs, *f_muxout, *f_muxlock;
00046    char *pt1, *spt2, *spt3, *pt2, *pt3, s_attrib[SBUFSIZE], s_filename[80],
00047         s_attrval[SBUFSIZE], s_attr[SBUFSIZE], s_finattr[SBUFSIZE];
00048    int i_chk = 0, i_lck = 1, i_atrcntr = 0, i_atrcntr2 = 0, i_pullname = 0;
00049    
00050    if ( argc < 3 ) {
00051       fprintf(stderr, "Syntax: %s mux-flatfile dbref# (no preceeding # character) [optional attribute-name]\r\n", argv[0]);
00052       exit(1);
00053    }
00054    if ( (f_muxflat = fopen(argv[1], "r")) == NULL ) {
00055       fprintf(stderr, "ERROR: Unable to open %s for reading.", argv[1]);
00056       exit(1);
00057    }
00058    pt1 = argv[2];
00059    while (*pt1) {
00060       if ( !isdigit(*pt1) ) {
00061          fprintf(stderr, "ERROR: Dbref# must be an integer (no # preceeding) [optional attribute-name]\r\n");
00062          fclose(f_muxflat);
00063          exit(1);
00064       }
00065       pt1++;
00066    }
00067    if ( (f_mymuxfile = fopen("mymuxfile.dat", "w")) == NULL ) {
00068       fprintf(stderr, "ERROR: Unable to open output file for attribute header information (mymuxfile.dat)\r\n");
00069       fclose(f_muxflat);
00070       exit(1);
00071    }
00072    memset(s_attrib, '\0', sizeof(s_attrib));
00073    if ( (argc >= 4) && *argv[3] )
00074       strncpy(s_attrib, argv[3], SBUFSIZE-1);
00075    spt2=malloc(MALSIZE);
00076    memset(spt2, '\0', MALSIZE);
00077    memset(s_attr, '\0', sizeof(s_attr));
00078    memset(s_attrval, '\0', sizeof(s_attr));
00079    while ( !feof(f_muxflat) ) {
00080       fgets(spt2, (MALSIZE-2), f_muxflat);
00081       pt2 = spt2;
00082       if ( i_chk ) {
00083          i_chk = 0;
00084          strtok(pt2, ":");
00085          sprintf(s_attr, "%s", strtok(NULL, ":"));
00086          s_attr[strlen(s_attr)-2]='\0';
00087          fprintf(f_mymuxfile, "%s %d \r\n", s_attr, atoi(s_attrval));
00088       }
00089       if ( (strlen(pt2) > 3) && (*pt2 == '+') && (*(pt2+1) == 'A') && isdigit(*(pt2+2)) ) {
00090          i_chk = 1;
00091          sprintf(s_attrval, "%s", pt2+2);
00092       }
00093       if ( *pt2 == '!' )
00094          break;
00095    }
00096    fclose(f_mymuxfile);
00097    if ( (f_mymuxfile = fopen("mymuxfile.dat", "r")) == NULL ) {
00098       fclose(f_muxflat);
00099       fprintf(stderr, "ERROR: Unable to open attribute header information (mymuxfile.dat)\r\n");
00100       free(spt2);
00101       exit(1);
00102    }
00103    if ( (f_muxattrs = fopen("muxattrs.dat", "r")) == NULL ) {
00104       fclose(f_muxflat);
00105       fclose(f_mymuxfile);
00106       fprintf(stderr, "ERROR: Unable to open attribute header information (muxattrs.dat)\r\n");
00107       free(spt2);
00108       exit(1);
00109    }
00110    memset(s_filename, '\0', sizeof(s_filename));
00111    sprintf(s_filename, "muxout_%d.txt", atoi(argv[2]));
00112    if ( (f_muxout = fopen(s_filename, "w")) == NULL ) {
00113       fclose(f_muxflat);
00114       fclose(f_mymuxfile);
00115       fclose(f_muxattrs);
00116       fprintf(stderr, "ERROR: Unable to open output file (%s)\r\n", s_filename);
00117       free(spt2);
00118       exit(1);
00119    }
00120    if ( (f_muxlock = fopen("muxlocks.dat", "r")) == NULL ) {
00121       fclose(f_muxflat);
00122       fclose(f_mymuxfile);
00123       fclose(f_muxattrs);
00124       fclose(f_muxout);
00125       fprintf(stderr, "ERROR: Unable to open mux lock file (muxlocks.dat)\r\n");
00126       free(spt2);
00127       exit(1);
00128    }
00129    memset(spt2, '\0', MALSIZE);
00130    spt3=malloc(MALSIZE);
00131    memset(spt3, '\0', MALSIZE);
00132    pt3 = spt3;
00133    fseek(f_muxflat, 0L, SEEK_SET);
00134    fprintf(stderr, "Step 1: Quering for dbref #%d\n", atoi(argv[2]));
00135    i_chk = 0;
00136    while ( !feof(f_muxflat) ) {
00137       fgets(spt2, (MALSIZE-2), f_muxflat);
00138       if ( i_pullname ) {
00139          i_pullname = 0;
00140          fprintf(f_muxout, "@@ %s\n", spt2);
00141       }
00142       pt2 = spt2;
00143       if ( (*pt2 == '<') && i_chk ) {
00144          break;
00145       }
00146       if ( *pt2 == '!' && (atoi(pt2+1) == atoi(argv[2])) ) {
00147          i_chk = 1;
00148          i_pullname = 1;
00149          continue;
00150       }
00151       if ( i_chk && *pt2 == '>' && isdigit(*(pt2+1)) ) {
00152          i_chk = 2;
00153          i_atrcntr++;
00154          sprintf(s_attrval, " %d ", atoi(pt2+1));
00155          memset(spt2, '\0', MALSIZE);
00156          memset(s_finattr, '\0', sizeof(s_finattr));
00157          fseek(f_muxattrs, 0L, SEEK_SET);
00158          while ( !feof(f_muxattrs) ) {
00159             fgets(spt2, (MALSIZE-2), f_muxattrs);
00160             if ( strstr(spt2, s_attrval) != NULL ) {
00161                strcpy(s_finattr, (char *)strtok(spt2, " "));
00162                break;
00163             }
00164          }
00165          if ( strlen(s_finattr) == 0 ) {
00166             fseek(f_mymuxfile, 0L, SEEK_SET);
00167             while ( !feof(f_mymuxfile) ) {
00168                fgets(spt2, (MALSIZE-2), f_mymuxfile);
00169                if ( strstr(spt2, s_attrval) != NULL ) {
00170                   strcpy(s_finattr, (char *)strtok(spt2, " "));
00171                   break;
00172                }
00173             }
00174          }
00175          if ( strlen(s_finattr) == 0 ) {
00176             fprintf(stderr, "ERROR: Unknown error in attribute handler.");
00177             exit(1);
00178          }
00179          fseek(f_muxlock, 0L, SEEK_SET);
00180          i_lck = 0;
00181          while ( !feof(f_muxlock) ) {
00182             fgets(spt2, (MALSIZE-2), f_muxlock);
00183             if ( strstr(spt2, s_attrval) != NULL ) {
00184                i_lck = 1;
00185                break;
00186             }
00187          }
00188          if ( !*s_attrib || !stricmp(s_finattr, s_attrib) || strstr(s_finattr, s_attrib) ) {
00189             i_atrcntr2++;
00190             if ( i_lck )
00191                fprintf(f_muxout, "@lock/%s #%s=", s_finattr, argv[2]);
00192             else if ( atoi(s_attrval) < 256 )
00193                fprintf(f_muxout, "@%s #%s=", s_finattr, argv[2]);
00194             else
00195                fprintf(f_muxout, "&%s #%s=", s_finattr, argv[2]);
00196          }
00197       } else if ( i_chk == 2) {
00198          if ( *pt2 == '"' )
00199             pt2++;
00200          if ( *pt2 == '\001' ) {
00201             while ( *pt2 && *pt2 != ':' ) pt2++;
00202             pt2++;
00203             while ( *pt2 && *pt2 != ':' ) pt2++;
00204             pt2++;
00205          }
00206          memset(spt3, '\0', MALSIZE);
00207          pt3 = spt3;
00208          while ( *pt2 ) {
00209             if ( *pt2 == '\\' ) {
00210                pt2++;
00211             }
00212             if ( *pt2 == '\t' ) {
00213                pt2++;
00214                *pt3 = '%';
00215                pt3++;
00216                *pt3 = 't';
00217                pt3++;
00218             }
00219             *pt3 = *pt2; 
00220             pt2++;
00221             pt3++;
00222          }
00223          *pt3 = '\0';
00224          if ( strlen(spt3) > 2) {
00225             if ( *(pt3-2) == '"' ) {
00226                *(pt3-2) = '\n';
00227                *(pt3-1) = '\0';
00228             } else if ( *(pt3-2) == '\r' ) {
00229                *(pt3-2) = '%';
00230                *(pt3-1) = 'r';
00231                *pt3 = '\0';
00232             }
00233          }
00234          if ( *spt3 == '\r' && (strlen(spt3) <= 2) ) {
00235              strcpy(spt3, "%r");
00236          }
00237          if ( !*s_attrib || !stricmp(s_finattr, s_attrib) || strstr(s_finattr, s_attrib) )
00238             fprintf(f_muxout, "%s", spt3);
00239       }
00240    }
00241    if ( !*s_attrib )
00242       fprintf(stderr, "Step 2: Writing %d attributes\n", i_atrcntr);
00243    else
00244       fprintf(stderr, "Step 2: Writing %d (of %d) attributes\n", i_atrcntr2, i_atrcntr);
00245    fclose(f_muxlock);
00246    fclose(f_muxout);
00247    fclose(f_muxattrs);
00248    fclose(f_mymuxfile);
00249    fclose(f_muxflat);
00250    free(spt2);
00251    free(spt3);
00252    fprintf(stderr, "Step 3: Completed (file is: %s).\n", s_filename);
00253    return 0;
00254 }

Generated on Mon May 28 04:40:12 2007 for MUX by  doxygen 1.4.7