"Fossies" - the Fresh Open Source Software Archive

Member "asp2php-2008-08-13/src/general.c" (13 Aug 2008, 6957 Bytes) of package /linux/www/old/asp2php-2008-08-13.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "general.c" see the Fossies "Dox" file reference documentation.

    1 #include <stdio.h>
    2 #include <stdlib.h>
    3 #include <string.h>
    4 #include <ctype.h>
    5 
    6 #include "globals.h"
    7 #include "functions.h"
    8 
    9 /* error() - output error messages */
   10 
   11 void error(char *token,char *expected,char *keyword)
   12 {
   13   if (supress==0)
   14   {
   15     printf("Parse Error: Expected '%s' and got '%s' in '%s' on line %d\n",expected,token,keyword,line);
   16   }
   17 }
   18 
   19 void unsupported(char *keyword)
   20 {
   21   printf("Not Supported: '%s' on line %d.\n            -- asp2php attemps work around it.\n",keyword,line);
   22 }
   23 
   24 void comment_out(char *tmp_token)
   25 {
   26 char token[TOKENSIZE];
   27 int tokentype;
   28 
   29   fprintf(out,"// %s",tmp_token);
   30   while(1)
   31   {
   32     tokentype=gettoken(token);
   33     if (tokentype==TOK_EOF || tokentype==TOK_EOL)
   34     { fprintf(out,"\n"); break; }
   35     fprintf(out," %s",token);
   36   }
   37 }
   38 
   39 /* push() - pushback a token (for looking ahead) */
   40 
   41 void push(char *token,int tokentype)
   42 {
   43   if (tokenpush[0]!=0)
   44   {
   45     strcpy(tokenpush2,token);
   46     tokentypepush2=tokentype;
   47     return;
   48   }
   49 
   50   strcpy(tokenpush,token);
   51   tokentypepush=tokentype;
   52 }
   53 
   54 void push2(char *token,int tokentype)
   55 {
   56   strcpy(tokenpush2,token);
   57   tokentypepush2=tokentype;
   58 }
   59 
   60 /* isnum() - check and see if current string is a number */
   61 
   62 int isnum(char *s)
   63 {
   64 int l,t;
   65 
   66   l=strlen(s);
   67 
   68   for(t=0; t<l; t++)
   69   { if (s[t]<'0' || s[t]>'9') return 0; }
   70 
   71   return 1;
   72 }
   73 
   74 /* strcasestr_m - case insensitive version of strstr */
   75 /* returns a pointer to the matching section of hejstack, or 0 if no match */
   76 char *strcasestr_m(char *hejstack, char *needle)
   77 {
   78 int hejlen,neelen;
   79 int t;
   80 
   81   hejlen=strlen(hejstack);
   82   neelen=strlen(needle);
   83 
   84   for(t=0; t<=hejlen-neelen; t++)
   85   {
   86     if (strncasecmp(&hejstack[t],needle,neelen)==0)
   87     { return &hejstack[t]; }
   88   }
   89 
   90   return (char *)0;
   91 }
   92 
   93 /* Decide the case of an asp variable and change */
   94 
   95 void strcase(char *s)
   96 {
   97 int l,r;
   98 
   99   if (casesense==0) return;
  100 
  101   l=strlen(s);
  102 
  103   for(r=0; r<l; r++)
  104   {
  105     if (casesense==1)
  106     { s[r]=tolower(s[r]); }
  107       else
  108     { s[r]=toupper(s[r]); }
  109   }
  110 }
  111 
  112 int array_insert_value(char *buffer, int buffer_len, char *token)
  113 {
  114 int p;
  115 
  116   p=0;
  117 
  118   while (buffer[p]!=0)
  119   {
  120     while (buffer[p]!=0)
  121     { p++; }
  122 
  123     p++;
  124   }
  125 
  126   if (p+strlen(token)+2>=buffer_len) return -1;
  127 
  128   strcpy(buffer+p,token);
  129   p=p+strlen(token)+1;
  130   buffer[p]=0;
  131 
  132   return 0;
  133 }
  134 
  135 int array_get_index(char *buffer, char *token)
  136 {
  137 int p,i;
  138 
  139   p=0;
  140   i=0;
  141 
  142   while (buffer[p]!=0)
  143   {
  144     if (strcmp(&buffer[p], token)==0)
  145     {
  146       return i;
  147     }
  148 
  149     while (buffer[p]!=0)
  150     { p++; }
  151     p++;
  152 
  153     i++;
  154   }
  155 
  156   return -1;
  157 }
  158 
  159 int array_get_length(char *buffer)
  160 {
  161 int p,i;
  162 
  163   p=0;
  164   i=0;
  165 
  166   while (buffer[p]!=0)
  167   {
  168     while (buffer[p]!=0)
  169     { p++; }
  170     p++;
  171 
  172     i++;
  173   }
  174 
  175   return i;
  176 }
  177 
  178 char *array_get_value(char *buffer, int index)
  179 {
  180 int p,i;
  181 
  182   p=0;
  183   i=0;
  184 
  185   while (buffer[p]!=0)
  186   {
  187     if (index==i) return &buffer[p];
  188 /*
  189     if (strcmp(&buffer[p], token)==0)
  190     {
  191       return i;
  192     }
  193 */
  194 
  195     while (buffer[p]!=0)
  196     { p++; }
  197     p++;
  198 
  199     i++;
  200   }
  201 
  202   return 0;
  203 }
  204 
  205 void array_print_all(char *buffer) /* for debugging */
  206 {
  207 int p;
  208 
  209   p=0;
  210   while (buffer[p]!=0)
  211   {
  212     printf("%s\n",&buffer[p]);
  213 
  214     while (buffer[p]!=0)
  215     { p++; }
  216 
  217     p++;
  218   }
  219 }
  220 
  221 void array_print_value(char *buffer, int index) /* for debugging */
  222 {
  223 int p,k;
  224 
  225   p=0;
  226   k=0;
  227 
  228   while (buffer[p]!=0)
  229   {
  230     if (k==index) printf("%s\n",&buffer[p]);
  231 
  232     while (buffer[p]!=0)
  233     { p++; }
  234 
  235     p++;
  236     k++;
  237   }
  238 }
  239 
  240 /* returns 1 if filename ends with one of the extensions to be converted */
  241 int is_asp_filename(char *filename)
  242 {
  243 int t;
  244 char *s;
  245 
  246   t=0;
  247   while(1)
  248   {
  249     s=array_get_value(aspextensions,t);
  250     if (s==0) break;
  251 
  252     if (strlen(filename)>strlen(s))
  253     {
  254       if (strcasecmp(filename+(strlen(filename)-strlen(s)),s)==0)
  255       { return 1; }
  256     }
  257 
  258     t++;
  259   }
  260 
  261   return 0;
  262 }
  263 
  264 int extcmp(char *filename, char *extension)
  265 {
  266   if (strlen(filename)>strlen(extension))
  267   {
  268     if (strcasecmp(filename+(strlen(filename)-strlen(extension)),extension)==0)
  269     { return 1; }
  270   }
  271 
  272   return 0;
  273 }
  274 
  275 void change_extension(char *filename, char *extension)
  276 {
  277 int t,r;
  278 
  279   t=strlen(filename);
  280 
  281   for (r=t; r>=0; r--)
  282   {
  283     if (filename[r]=='.')
  284     {
  285       strcpy(filename+r,extension);
  286       return;
  287     }
  288   }
  289 }
  290 
  291 /* autoindent() - this sets up proper clean Kohnian style
  292    programming indenting in the outputted php code */
  293 
  294 int autoindent()
  295 {
  296 int r;
  297 char spaces[1024];
  298 
  299   /* if (column!=0) return 0; */
  300   strcpy(spaces,"");
  301 
  302   for (r=0; r<indent; r++)
  303   { strcat(spaces,"  "); }
  304 
  305   fprintf(out,"%s",spaces);
  306 
  307   return 0;
  308 }
  309 
  310 int autoindent_string(char *newtoken)
  311 {
  312 int r;
  313 char spaces[1024];
  314 
  315   /* if (column!=0) return 0; */
  316   strcpy(spaces,"");
  317 
  318   for (r=0; r<indent; r++)
  319   { strcat(spaces,"  "); }
  320 
  321   strcat(newtoken,spaces);
  322 
  323   return 0;
  324 }
  325 
  326 void add_indent(char *newtoken)
  327 {
  328 int r;
  329 
  330   for (r=0; r<indent; r++)
  331   { strcat(newtoken,"  "); }
  332 }
  333 
  334 void strtolower(char *s)
  335 {
  336 int r;
  337 
  338   r=0;
  339   while(1)
  340   {
  341     if (s[r]==0) break;
  342     s[r]=tolower(s[r]);
  343     r++;
  344   }
  345 
  346 }
  347 
  348 void insert_isnum()
  349 {
  350   fprintf(out,"\nfunction IsNumeric($n)\n{\n");
  351   fprintf(out,"  $p=trim($n);\n  $l=strlen($p);\n\n");
  352   fprintf(out,"  for ($t=0; $t<$l; $t++)\n");
  353   fprintf(out,"  {\n    $c=substr($p,$t,1);\n");
  354   fprintf(out,"    if ($c<'0' || $c>'9')\n");
  355   fprintf(out,"    { if ($c!='.') return 0; }\n");
  356   fprintf(out,"  }\n\n  return 1;\n}\n");
  357 
  358   add_isnum=0;
  359 }
  360 
  361 int is_endscript()
  362 {
  363 char token[1024];
  364 int tokentype;
  365 char molecule[4096];
  366 
  367   strcpy(molecule,"<");
  368   tokentype=gettoken(token);
  369   strcat(molecule,token);
  370   tokentype=gettoken(token);
  371   strcat(molecule,token);
  372   tokentype=gettoken(token);
  373   strcat(molecule,token);
  374 
  375   if (strcasecmp("</script>",molecule)==0)
  376   { return 1; }
  377 
  378   return 0;
  379 }
  380 
  381 void eval_funct_or_molecule(char *newtoken)
  382 {
  383 char token[TOKENSIZE];
  384 int tokentype;
  385 
  386   tokentype=gettoken(token);
  387   push(token,tokentype);
  388   strcpy(newtoken,"");
  389 
  390   if (strcmp(token,"(")==0)
  391   { eval_funct(newtoken); }
  392     else
  393   { eval_molecule(newtoken); }
  394 }
  395 
  396 void remove_quotes(char *s)
  397 {
  398 int t,l;
  399 
  400   if (s[0]=='\'' || s[0]=='\"')
  401   {
  402     l=strlen(s);
  403 
  404     for (t=0; t<l; t++)
  405     {
  406       s[t]=s[t+1];
  407     }
  408 
  409     s[l-2]=0;
  410   }
  411 }
  412 
  413 void fixwinpath(char *filename)
  414 {
  415 int t;
  416 
  417   t=0;
  418   while(filename[t]!=0)
  419   {
  420     if (filename[t]=='\\') filename[t]='/';
  421     t++;
  422   }
  423 }
  424 
  425 int copy_file(char *source, char *dest)
  426 {
  427 FILE *fin,*fout;
  428 unsigned char buffer[8193];
  429 int t;
  430 
  431   fin=fopen(source,"r");
  432   if (fin==0)
  433   {
  434     printf("Error: Unable to open file for reading: %s\n",source);
  435     return 1;
  436   }
  437 
  438   fout=fopen(dest,"w");
  439   if (fout==0)
  440   {
  441     fclose(fin);
  442     printf("Error: Unable to open file for writing: %s\n",dest);
  443     return 1;
  444   }
  445 
  446   while((t=fread(buffer,1,8192,fin))!=0)
  447   { fwrite(buffer,1,t,fout); }
  448 
  449   fclose(fin);
  450   fclose(fout);
  451 
  452   return 0;
  453 }
  454 
  455 /* return the directory portion of a path */
  456 void dirname_kohn(char *s)
  457 {
  458 int l;
  459 
  460   l=strlen(s)-1;
  461 
  462   while(l>=0 && (s[l]!='\\' && s[l]!='/')) l--;
  463 
  464   if (l>=0)
  465   {
  466     s[l+1]=0;
  467     /* strcat(s,"/"); */
  468   }
  469     else
  470   { s[0]=0; }
  471 }
  472 
  473 
  474