home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / smapp100.zip / sm10.zip / smgfuncr.c < prev    next >
C/C++ Source or Header  |  2000-05-14  |  13KB  |  415 lines

  1. /* ------------------------------------------------------------------------
  2.  *
  3.  *        File: smgfuncr.c
  4.  *     Project: Source Mapper.
  5.  *     Created: April 4, 1993.
  6.  * Description: Source code to include index of functins in map.
  7.  *
  8.  * Copyright (C) 2000 Leif-Erik Larsen.
  9.  * This file is part of the Source Mapper source package.
  10.  * Source Mapper is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published
  12.  * by the Free Software Foundation, in version 2 as it comes in the
  13.  * "COPYING" file of the XWorkplace main distribution.
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * ------------------------------------------------------------------------ */
  20.  
  21.  
  22.  
  23.  
  24. #include "smg.h"
  25.  
  26.  
  27.  
  28.  
  29. static struct
  30. {
  31.    long      fnr;                      /* Number of functions in project     */
  32.    funcTYPE *buff;                     /* Points to buffer of functions info */
  33. }
  34.    funcs;
  35.  
  36.  
  37.  
  38.  
  39. int FuncRIncludeToMap ( FILE *mapf )
  40. /*
  41.     Function: Include index of functions to map file.
  42.         Date: April, 26. 1993. 11:42. By LEL.
  43.    Interface: mapf = Map file to include index of functions.
  44.      Returns: E/O.
  45. */
  46. {
  47.    int ok;
  48.  
  49.    if (!prjstat.prjon)                 /* If no active project               */
  50.    {
  51.       return (ERROR);
  52.    }
  53.  
  54.    if (prjstat.filc == 1)              /* If only one source in project      */
  55.    {
  56.       /* Make index of one source */
  57.       ok = FuncRIncludeToMap1 (mapf);
  58.    }
  59.    else                                /* If several sources in project      */
  60.    {
  61.       /* Make index of several sources */
  62.       ok = FuncRIncludeToMap2 (mapf);
  63.    }
  64.  
  65.    return (ok);
  66. } /* FuncRIncludeToMap (); */
  67.  
  68.  
  69.  
  70.  
  71. int FuncRBuffConstruct ( void )
  72. /*
  73.     Function: Construct buffer of functions in project.
  74.         Date: April, 26. 1993. 12:00. By LEL.
  75.    Interface:
  76.      Returns: E/O.
  77. */
  78. {
  79.    FILE     *objf;
  80.    finfTYPE  finf;
  81.    fposTYPE  fpos;
  82.    funcTYPE *func;
  83.    prjfeTYPE fdata;
  84.  
  85.    if (prjstat.filc == 1)              /* If only one source in project      */
  86.    {
  87.       /* Get name of normal objectfile */
  88.       if (!GetFDataFromPrj (1, &fdata))
  89.          RETURN_ERR;
  90.  
  91.       /* Use normal objfile if only 1 source */
  92.       if ((objf = FOpen (fdata.objn, "r+b")) == NULL)
  93.       {
  94.          /* Failed on open file! */
  95.          RETERR (16, fdata.objn);
  96.       }
  97.    }
  98.    else
  99.    {
  100.       /* Open main objectfile */
  101.       if ((objf = FOpen (tmpn.manobj, "r+b")) == NULL)
  102.       {
  103.          /* Failed on open temporary file! */
  104.          RETERR (11, tmpn.manobj);
  105.       }
  106.    }
  107.  
  108.    GetFInfTYPE (objf, &finf);          /* Get main info about project sources*/
  109.    if (!GetFPosTYPE (objf, &fpos))     /* Get file position indexes          */
  110.    {
  111.       FClose (objf);                   /* Close main objectfile if error     */
  112.       RETURN_ERR;
  113.    }
  114.  
  115.    FSeek (objf, fpos.funreg, SEEK_SET);/* Start of block of function data    */
  116.  
  117.    funcs.fnr = finf.func;
  118.  
  119.    if (funcs.fnr <= 0)                 /* If this module has no functions */
  120.    {
  121.       FClose (objf);
  122.       return (OK);                     /* Just do nothing, but return OK */
  123.    }
  124.  
  125.    /* Get memory to function buffer */
  126.    funcs.buff = (funcTYPE *) Alloc (finf.func * sizeof (funcTYPE));
  127.    if (funcs.buff == NULL)
  128.    {
  129.       FClose (objf);
  130.       RETERR (14, NULL);               /* Out of memory! */
  131.    }
  132.  
  133.    for (func = funcs.buff;             /* Point to first element in buffer   */
  134.         finf.func-- > 0;               /* Read all functions into buffer     */
  135.         func += 1)                     /* Point to next element              */
  136.    {
  137.       /* Read next function */
  138.       if (FRead (func, sizeof (funcTYPE), 1, objf) != 1)
  139.       {
  140.          Free ((char *) funcs.buff);   /* Give allocated memory back to heap */
  141.          FClose (objf);                /* Close main objectfile if error     */
  142.          return (ERROR);
  143.       }
  144.    }
  145.  
  146.    FClose (objf);                      /* Close main objectfile              */
  147.  
  148.    return (OK);                        /* OK construct                       */
  149. } /* FuncRBuffConstruct (); */
  150.  
  151.  
  152.  
  153.  
  154. int FuncRBuffDestruct ( void )
  155. /*
  156.     Function: Destruct buffer of functions in project.
  157.         Date: April, 26. 1993. 12:00. By LEL.
  158.    Interface:
  159.      Returns: E/O.
  160. */
  161. {
  162.    Free  ((char *) funcs.buff);        /* Give allocated memory back to heap */
  163.    funcs.fnr  = 0L;
  164.    funcs.buff = NULL;
  165.  
  166.    return (OK);                        /* OK destruct                        */
  167. } /* FuncRBuffDestruct (); */
  168.  
  169.  
  170.  
  171.  
  172. int FuncRBuffIndxGet ( long indx, funcTYPE *func )
  173. /*
  174.     Function: Get function info from buffer, with respect to specified index.
  175.         Date: April, 26. 1993. 12:20. LEL.
  176.    Interface: indx = Index of which function to get info about (0..).
  177.               func = Buffer of where to store the information.
  178.      Returns: 1 if index is within legal area, else 0.
  179. */
  180. {
  181.    if (indx >= funcs.fnr)
  182.       return (0);                      /* Index is outside legal area        */
  183.  
  184.    memcpy (func,
  185.            ADRESS (funcs.buff, indx, sizeof (funcTYPE)),
  186.            sizeof (funcTYPE));
  187.  
  188.    return (1);                         /* Index is within legal area         */
  189. } /* FuncRBuffIndxGet (); */
  190.  
  191.  
  192.  
  193.  
  194. int FuncRBuffLNrGet ( long lnr, funcTYPE *func )
  195. /*
  196.     Function: Get function info from buffer, in respect to
  197.               specified global line number. Which function do this line
  198.               belong to?
  199.         Date: April, 26. 1993. 12:30. By LEL.
  200.    Interface: lnr  = Which function do this global line number belong to?
  201.               func = Returns info about owner.
  202.      Returns: 1 if the actual function was found, else 0.
  203. */
  204. {
  205.    long           fnr;
  206.    funcTYPE huge *ptr = funcs.buff;
  207.  
  208.    for (fnr = 0;                       /* See trough all functions           */
  209.         fnr < funcs.fnr;
  210.         fnr++, ptr++)
  211.    {
  212.       if (lnr >= ptr->blin &&          /* Line number within this function?  */
  213.           lnr <= ptr->blin + ptr->lines)
  214.       {
  215.          /* Yes, so copy info about function */
  216.          memcpy (func, ptr, sizeof (funcTYPE));
  217.          return (1);                   /* Found a function with that line nr */
  218.       }
  219.    }
  220.  
  221.    return (0);                         /* No function with that line number  */
  222. } /* FuncRBuffLNrGet (); */
  223.  
  224.  
  225.  
  226.  
  227. long FuncRBuffGetFNr ( void )
  228. /*
  229.     Function: Get number of functions currently in buffer.
  230.         Date: Mai, 12. 1993. 18:12. By LEL.
  231.    Interface:
  232.      Returns: Number of functions currently in buffer.
  233.      Comment:
  234. */
  235. {
  236.    return (funcs.fnr);
  237. } /* FuncRBuffGetFNr (); */
  238.  
  239.  
  240.  
  241.  
  242. int FuncRBuffIsFunc ( const char *fname )
  243. /*
  244.         Function: Test if function is in buffer.
  245.    Programmed by: LEL
  246.             Date: Mai, 13. 1993. 04:35.
  247.        Interface: fname = Name of function to test if is in buffer.
  248.          Returns: 1 if the function is in buffer, else 0.
  249. */
  250. {
  251.    long           fnr;                 /* Count number of function in buffer */
  252.    funcTYPE huge *ptr = funcs.buff;    /* Init pointer to buffer             */
  253.  
  254.    for (fnr = 0;                       /* See trough all functions           */
  255.         fnr < funcs.fnr;
  256.         fnr++, ptr++)
  257.    {
  258.       if (strcmp (fname, ptr->name) == 0)
  259.          return (1);                   /* Yes, function is in buffer         */
  260.    }
  261.  
  262.    return (0);                         /* No, function is not in buffer      */
  263. } /* FuncRBuffIsFunc (); */
  264.  
  265.  
  266.  
  267.  
  268. int FuncRIncludeToMap1 ( FILE *mapf )
  269. /*
  270.     Function: Include index of functions in single source project.
  271.         Date: April, 26. 1993. 13:45. By LEL.
  272.    Interface: mapf = Map file to include index of functions in single
  273.                      source in project.
  274.      Returns: E/O.
  275. */
  276. {
  277.    long     fnr;                       /* Index to current function          */
  278.    funcTYPE func;                      /* Info about current function        */
  279.  
  280.    if (funcs.fnr == 0)                 /* Any functions in project?          */
  281.       return (OK);                     /* No, do nothing but return OK       */
  282.  
  283.    /* =======================================================================*/
  284.    /* Index of functions:                                                    */
  285.    /*                                                                        */
  286.    /*                                                                        */
  287.  
  288.    WrtStdLinComStart (mapf, stdwidth, linechr2);
  289.    FPrintF (mapf, "%s:\n\n", _StrMSGFUNCREGSTR);
  290.  
  291.    if (errno != 0)                     /* Any error in last file I/O?        */
  292.    {
  293.       return (ERROR);
  294.    }
  295.  
  296.    /* Including index of functions */
  297.    Display (D_HEIGH, "%s", _StrMSGINCLFUNCREG);
  298.  
  299.    for (fnr = 0;
  300.         fnr < funcs.fnr;               /* All functions in project           */
  301.         fnr += 1)
  302.    {
  303.       FuncRBuffIndxGet (fnr, &func);   /* Get info of this function */
  304.  
  305.       if (fnr % 17 == 0)               /* Oppdater brukerinfo for hver 17. */
  306.       {
  307.          Display (D_LOW, "%s: %d%%", _StrMSGINCLFUNCREG,
  308.                                      (fnr * 100) / funcs.fnr);
  309.       }
  310.  
  311.       /* F-nn   NameOfFunction ()    L-xxx */
  312.  
  313.       FPrintF   (mapf, "%c-%ld\t%s ()\t%c-%ld",
  314.                         FUNCTIONCHR,
  315.                         fnr + 1,
  316.                         func.name,
  317.                         LOCLINCHR,
  318.                         func.blin);
  319.  
  320.       if (o_various.gencom &&          /* Insert SMG-info in comments?       */
  321.           fnr+1 == funcs.fnr)          /* Is this the last function in list? */
  322.       {
  323.          WrtComEnd (mapf);             /* Mark end of comment                */
  324.       }
  325.  
  326.       FPrintF (mapf, "\n");            /* New line after each function index */
  327.  
  328.       if (errno != 0)                  /* Any error in last file I/O?        */
  329.       {
  330.          return (ERROR);
  331.       }
  332.    }
  333.  
  334.    FPrintF (mapf, "\n\n\n");
  335.  
  336.    /* Including index of functions: 100% */
  337.    Display (D_LOW, "%s: 100%%", _StrMSGINCLFUNCREG);
  338.  
  339.    return (OK);
  340. } /* FuncRIncludeToMap1 (); */
  341.  
  342.  
  343.  
  344.  
  345. int FuncRIncludeToMap2 ( FILE *mapf )
  346. /*
  347.     Function: Include index of functions in sources in project.
  348.         Date: April, 26. 1993. 13:50. By LEL.
  349.    Interface: mapf = Map file to include index of functions in sources
  350.                      in project.
  351.      Returns: E/O.
  352. */
  353. {
  354.    long      fnr;                      /* Index to current function          */
  355.    long      slin;                     /* Starting line number of source     */
  356.    funcTYPE  func;                     /* Info about current function        */
  357.    prjfeTYPE fdata;                    /* Data about current source          */
  358.  
  359.    if (funcs.fnr == 0)                 /* Any functions in project?          */
  360.       return (OK);                     /* No, do nothing but return          */
  361.  
  362.    /* =======================================================================*/
  363.    /* Index of functions:                                                    */
  364.    /*                                                                        */
  365.  
  366.    WrtStdLinComStart (mapf, stdwidth, linechr2);
  367.    FPrintF (mapf, "%s:\n\n", _StrMSGFUNCREGSTR);
  368.  
  369.    Display (D_HEIGH, "%s", _StrMSGINCLFUNCREG); /* Including index of functions */
  370.  
  371.    for (fnr = 0; fnr < funcs.fnr; fnr++) /* All functions in project */
  372.    {
  373.       FuncRBuffIndxGet (fnr, &func);   /* Get info of this function */
  374.  
  375.       if (fnr % 17 == 0)               /* Oppdater brukerinfo for hver 17. */
  376.          Display (D_LOW, "%s: %d%%", _StrMSGINCLFUNCREG, (fnr * 100) / funcs.fnr);
  377.  
  378.       /* To which source do the line belong? */
  379.       if (!GetFDataOfGLin (func.blin, &slin, &fdata))
  380.       {
  381.          /* A rare error due to a bug in the program! */
  382.          RETERR (5, NULL);             /* Line belongs to no source! */
  383.       }
  384.  
  385.       /* F-nnn   NameOfFunction ()  G-xxx  (L-nn: filename.c) */
  386.       FPrintF (mapf, "%c-%ld\t%s ()\t%c-%ld\t(%c-%ld:\t%s)",
  387.                      FUNCTIONCHR, fnr + 1,
  388.                      func.name,
  389.                      GLOBLINCHR, func.blin,
  390.                      LOCLINCHR,
  391.                      func.blin - slin + 1,
  392.                      GetFName (fdata.name));
  393.  
  394.       if (o_various.gencom &&          /* Insert SMG-info in comments? */
  395.           fnr + 1 == funcs.fnr)        /* Is this the last function in list? */
  396.       {
  397.          WrtComEnd (mapf);             /* Mark end of comment */
  398.       }
  399.  
  400.       FPrintF (mapf, "\n");            /* New line after each function index */
  401.  
  402.       if (errno != 0)                  /* Any error in last file I/O? */
  403.          return (ERROR);
  404.    }
  405.  
  406.    FPrintF (mapf, "\n\n\n");
  407.  
  408.    /* Including index of functions: 100% */
  409.    Display (D_LOW, "%s: 100%%", _StrMSGINCLFUNCREG);
  410.  
  411.    return (OK);
  412. } /* FuncRIncludeToMap2 (); */
  413.  
  414.  
  415.