Code Files

editcsv.awk

Generated on Tue Dec 05 17:39:06 Eastern Standard Time 2006 from editcsv.awk


# Program   : EDITCSV.AWK
# Purpose   : Use a Web interface to edit a CSV file
# Author    : Bob Jonkman <bjonkman@sobac.com>

# Copyright 2008 Bob Jonkman and/or SOBAC Microcomputer Services

#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Date      : 22 March 2006


# Usage     : gawk -f library.awk -f editcsv.awk [-v HEADERS=(0|1)]  filename.csv
#             http:\\host.example?FILENAME=filename.csv[&HEADERS=1]

# Variables : HEADERS = 1 - Include CSV headers for edit

BEGIN   {   QUOTE   = "\""
            COMMA   = ","
            OFS     = COMMA

            parsecgi(ENVIRON["QUERY_STRING"],qstring)



# print("Content-type: text/html\n")
# print("<!-- ##### DEBUG ##### ")
# print("qstring:")
# for(i in qstring)
#     print(i,qstring[i])
# 
# print("arguments")
# for(i=0; i<=ARGC; i++)
#     print(i,ARGV[i])
# 
# 
# print("-->")

            if(ARGV[1] == "")
            {    
                ARGV[1] = qstring["FILENAME"]
                ARGC = 2
            }

            if(!HEADERS)
                HEADERS = qstring["HEADERS"]

# print("<!-- ##### DEBUG ##### ")
# print("qstring:")
# for(i in qstring)
#     print(i,qstring[i])
# 
# print("arguments")
# for(i=0; i<=ARGC; i++)
#     print(i,ARGV[i])
# 
# 
# print("-->")

            printhtmlhead("EditCSV: " txt2html(ARGV[1]))

            print("<body>")
            print("<h1>" ARGV[1] "</h1>")

            print("<form action=" QUOTE "envtest.awk?FILENAME=" txt2uri(ARGV[1]) QUOTE " method=" QUOTE "post" QUOTE ">")

            print("<p>")
            print(" <input type=" QUOTE "submit" QUOTE " value=" QUOTE "Done" QUOTE " />")
            print(" <input type=" QUOTE "reset" QUOTE " />")
            print("</p>")

            print("<table summary=" QUOTE txt2html(ARGV[1]) QUOTE " border=" QUOTE 1 QUOTE ">")

}


(FNR==1)    {   numheaders = parsecsv($0,headers)

                print(" <thead>")
                print("  <tr>")
                print("   <th>Delete</th>")

                if(HEADERS)
                {
                    for(i=1; i <= numheaders; i++)
                    {
                        print("   <td><input type=" QUOTE "checkbox" QUOTE "name=" QUOTE "c" i "del" QUOTE " /></td>")
                    }
                }
                else
                    print("  <td colspan=" QUOTE numheaders QUOTE ">&nbsp;</td>")

                print("  </tr>")

}



($0 == "=====") {   footerflag=1
                    print("  <input name=" QUOTE "r" NR "c1" QUOTE " type=" QUOTE "hidden" QUOTE " value=" QUOTE "=====" QUOTE " />")
                    print(" </tbody>")
                    print(" <tfoot>")
                    getline
                        
}

##### main() #####

    {       numfields = max(parsecsv($0,currentline),numfields)

            print("  <tr>")

            if(!HEADERS && (FNR == 1 || footerflag))
                print("    <td>&nbsp;</td>")
            else
                print("    <th><input type=" QUOTE "checkbox" QUOTE " name=" QUOTE "r" NR "del" QUOTE " /></th>")

            for(i=1; i<= numfields; i++)
            {
                print("    <td>")
                print("     <input name=" QUOTE "r" NR "c" i QUOTE)
                print("            size=" QUOTE max(20,length(currentline[i])) QUOTE)
                print("            value=" QUOTE txt2html(currentline[i]) QUOTE)

                if(!HEADERS && (FNR==1 || footerflag))
                {
                    print("            type=" QUOTE "hidden" QUOTE "/>")
                    print(txt2html(currentline[i]))
                }
                else
                    print("            type=" QUOTE "text" QUOTE "/>")

                print("    </td>")
                
            }

            print("  </tr>")
    }

##### end main() #####



(FNR==1)    {   print(" </thead>")
                print(" <tbody>")
}



END {
            print("  <tr>")
            print("    <th>Add row</th>")

            for(i=1; i<=numfields; i++)
            {   print("    <td><input type=" QUOTE "text" QUOTE)
                print("               name=" QUOTE "r" NR+1  "c" i "add" QUOTE " />")
                print("    </td>")
            }
            print("  </tr>")

            if (footerflag)
                print(" </tfoot>")
            else
                print(" </tbody>")


            print("</table>")

            print("<p>")
            print(" <input type=" QUOTE "submit" QUOTE " value=" QUOTE "Done" QUOTE " />")
            print(" <input type=" QUOTE "reset" QUOTE " />")
            print("</p>")

            print("</form>")

            print("</body>")
            print("</html>")
}

# EOF: EDITCSV.AWK
            
   

1 files processed.