Back to index

im-sdk  12.3.91
IMNSMapConf.cpp
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <string.h>
00003 #include <stdlib.h>
00004 #include <ctype.h>
00005 #include <sys/stat.h>
00006 #include "IMLog.hh"
00007 #include "IMNSMapConf.hh"
00008 
00009 int
00010 IMNSMapConf::get_line(
00011     char *buf,
00012     int size,
00013     char **curptr,
00014     char *line
00015 )
00016 {
00017     int line_ptr, n;
00018     char line_buf[MAX_LINE_LEN];
00019     char *ptr;
00020 
00021     char *p = ((0 == *curptr)?*curptr=buf:*curptr);
00022     char *pp = p;
00023 
00024     line_ptr = 0;
00025     line[0] = '\0';
00026 
00027     n = *curptr - buf;
00028     if (n >= size) { /* No more data to read */
00029         return 0;
00030     }
00031     /* get line with no space */
00032     while(1) {
00033         if (*p == '\n' || *p == '\0') {
00034             *p++;
00035             continue;
00036         }
00037 
00038         /* chop one line */
00039         while (*p != '\n') p++ ;
00040         /* pp to p is one line, just as fgets returns */
00041         memcpy(line_buf, pp, p-pp);
00042         line_buf[p-pp]='\0';
00043 
00044         ptr = line_buf;
00045 
00046         /* skip preceding space keys */
00047         while(*ptr && isspace(*ptr)) ptr++;
00048 
00049         /* if is space line, get new line */
00050         if (*ptr == '\n' || *ptr == '\0')
00051             continue;
00052 
00053         while(*ptr != '\n' && *ptr != '\0' && line_ptr < MAX_LINE_LEN)
00054             line[line_ptr++] = *ptr++;
00055 
00056         /* trim right space */
00057         while (isspace(line[line_ptr-1])) line_ptr--;
00058         line[line_ptr] = '\0';
00059 
00060         /* if the line end with '\', then continue read the next line */
00061         if (line[line_ptr-1] == '\\') {
00062             line_ptr--;
00063             line[line_ptr] = '\0';
00064             continue;
00065         }
00066         break;
00067     }
00068 
00069     *curptr = p+1 ;
00070 
00071     if (line[0] == '\0') return 0;
00072     if (line[0] == COMMENT) return -1;
00073     return line_ptr;
00074 }
00075 
00076 void
00077 IMNSMapConf::print_core(
00078     IMNsMapStruct *nsm
00079 )
00080 {
00081     int i;
00082 
00083     for (i=0; i<count; i++) {
00084         LOG_DEBUG("if_name [%s] \n", nsm[i].if_name);
00085         LOG_DEBUG("default repository [%s] \n", nsm[i].src_entry);
00086         LOG_DEBUG("real repository [%s] \n", nsm[i].dest_entry);
00087         LOG_DEBUG("location [%s] \n", nsm[i].location);
00088         LOG_DEBUG(" =====================================\n");
00089     }
00090 }
00091 
00092 bool
00093 IMNSMapConf::read_ns_map_config(
00094     char *buf, 
00095     int fsize
00096 )
00097 {
00098     char   line[MAX_LINE_LEN];
00099     char   *kptr, *ptr;
00100     int    len;
00101     char   *curr_ptr;
00102 
00103     curr_ptr = 0;
00104     do {
00105         len = get_line(buf, fsize, &curr_ptr, line);
00106 
00107         if (!len) break;
00108         if (len < 0) continue;
00109 
00110         if (!count){
00111            nsmp = (IMNsMapStruct *)calloc(1, sizeof(IMNsMapStruct));
00112         } else {
00113            nsmp = (IMNsMapStruct *) realloc((IMNsMapStruct *)nsmp,
00114                                              (count+1)*sizeof(IMNsMapStruct));
00115         }
00116 
00117         if (!nsmp)
00118            return false;
00119 
00120         kptr = 0;
00121 
00122         ptr = line;
00123         kptr = line;
00124 
00125         /* Get the if_name */
00126         while (*ptr && !isspace(*ptr)) ptr++;
00127         if (*ptr) {
00128             *ptr = '\0';
00129             ptr++;
00130         }
00131         nsmp[count].if_name = (char *)strdup(kptr);
00132 
00133         kptr = 0;
00134         /* Get the default repository */
00135         while (*ptr && isspace(*ptr)) ptr++;
00136         kptr = ptr;
00137         while (*ptr && !isspace(*ptr)) ptr++;
00138         if (*ptr) {
00139             *ptr = '\0';
00140             ptr++;
00141         }
00142         nsmp[count].src_entry = (char *)strdup(kptr);
00143 
00144         kptr = 0;
00145         /* Get the real repository */
00146         while (*ptr && isspace(*ptr)) ptr++;
00147         kptr = ptr;
00148         while (*ptr && !isspace(*ptr)) ptr++;
00149         if (*ptr) {
00150             *ptr = '\0';
00151             ptr++;
00152         }
00153         nsmp[count].dest_entry = (char *)strdup(kptr);
00154 
00155         while (*ptr && isspace(*ptr)) ptr++;
00156         nsmp[count].location = (char *)strdup(ptr);
00157 
00158         count++;
00159     } while(1);
00160 
00161     // print_core(nsmp);
00162 
00163     return true;
00164 }
00165 
00166 bool
00167 IMNSMapConf::load()
00168 {
00169     FILE *fp;
00170     int fd, size, nfread;
00171     struct stat stat_buf;
00172     char *file_buf;
00173 
00174     fp = fopen(fname.c_str(), "r");
00175     if (!fp)
00176        return false;
00177 
00178     fd = fileno((FILE *)fp);
00179     if (fstat(fd, &stat_buf) < 0) {
00180        return false;
00181     }
00182     size = stat_buf.st_size;
00183     file_buf = (char *)calloc(size, sizeof(char));
00184     nfread = fread(file_buf, size, 1, fp);
00185 
00186     if (!read_ns_map_config(file_buf, size))
00187        return false;
00188 
00189     fclose(fp);
00190 
00191     return true;
00192 }
00193 
00194 IMNSMapConf::IMNSMapConf(
00195     const char *nsm_fname
00196 )
00197 {
00198     fname = nsm_fname;
00199     nsmp = NULL;
00200     count = 0;
00201 }
00202 
00203 IMNSMapConf::~IMNSMapConf()
00204 {
00205     int i;
00206 
00207     for (i=0; i<count; i++) {
00208         if (nsmp[i].if_name) free (nsmp[i].if_name);
00209         if (nsmp[i].src_entry) free (nsmp[i].src_entry);
00210         if (nsmp[i].dest_entry) free (nsmp[i].dest_entry);
00211         if (nsmp[i].location) free (nsmp[i].location);
00212     }
00213     free ((IMNsMapStruct *) nsmp);
00214 }
00215 
00216 /* Local Variables: */
00217 /* c-file-style: "iiim-project" */
00218 /* End: */