Subversion Repositories NedoOS

Rev

Rev 2192 | Rev 2194 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <oscalls.h>
  5. #include <osfs.h>
  6. #include <intrz80.h>
  7. #include <graphic.h>
  8. #include <../common/terminal.c>
  9. #include <tcp.h>
  10.  
  11. #define true 1
  12. #define false 0
  13.  
  14. unsigned int RBR_THR = 0xf8ef;
  15. unsigned int IER = 0xf9ef;
  16. unsigned int IIR_FCR = 0xfaef;
  17. unsigned int LCR = 0xfbef;
  18. unsigned int MCR = 0xfcef;
  19. unsigned int LSR = 0xfdef;
  20. unsigned int MSR = 0xfeef;
  21. unsigned int SR = 0xffef;
  22. unsigned int divider = 1;
  23. unsigned char comType = 0;
  24. unsigned int espType = 32;
  25.  
  26. unsigned char picture[16384];
  27. unsigned char netbuf[2048];
  28.  
  29. unsigned char minRating[] = "0000000000";
  30. struct fileStruct
  31. {
  32.   long picId;
  33.   unsigned int picYear;
  34.   unsigned long totalAmount;
  35.   unsigned char picRating[8];
  36.   unsigned char picName[256];
  37.   unsigned char picType[64];
  38.   unsigned char authorIds[64];
  39.   unsigned char authorTitle[64];
  40.   unsigned char authorRealName[64];
  41.   unsigned char afn[128];
  42.   unsigned char pfn[128];
  43.   unsigned char fileName[128];
  44. } curFileStruct;
  45.  
  46. struct window
  47. {
  48.   unsigned char x;
  49.   unsigned char y;
  50.   unsigned char w;
  51.   unsigned char h;
  52.   unsigned char text;
  53.   unsigned char back;
  54.   unsigned char tittle[80];
  55. } curWin;
  56.  
  57. struct sockaddr_in dnsaddress;
  58. struct sockaddr_in targetadr;
  59. struct readstructure readStruct;
  60.  
  61. unsigned char ver[] = "3.4";
  62. const unsigned char sendOk[] = "SEND OK";
  63. const unsigned char gotWiFi[] = "WIFI GOT IP";
  64. unsigned char buffer[] = "0000000000";
  65. unsigned char userAgent[] = " HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS; GetPic)\r\n\r\n\0";
  66. unsigned char zxart[] = "zxart.ee";
  67. unsigned char keypress, verbose, randomPic, slideShow, netDriver;
  68.  
  69. unsigned long contLen;
  70. unsigned long count = 0;
  71. unsigned int headlng;
  72. unsigned int slideShowTime = 0;
  73. unsigned int loaded;
  74.  
  75. unsigned char crlf[2] = {13, 10};
  76. unsigned char cmd[512];
  77. unsigned char link[512];
  78. unsigned char fileIdChar[10];
  79.  
  80. void clearStatus(void)
  81. {
  82. }
  83.  
  84. void emptyKeys(void)
  85. {
  86.   unsigned char loop, key;
  87.   do
  88.   {
  89.     key = OS_GETKEY();
  90.     if (loop > 64)
  91.     {
  92.       break;
  93.     }
  94.     loop++;
  95.   } while (key != 0);
  96. }
  97.  
  98. void spaces(unsigned char number)
  99. {
  100.   while (number > 0)
  101.   {
  102.     putchar(' ');
  103.     number--;
  104.   }
  105. }
  106.  
  107. void infoBox(struct window w, unsigned char *message)
  108. {
  109.   unsigned char wcount, tempx, tittleStart;
  110.  
  111.   w.h++;
  112.   OS_SETXY(w.x, w.y - 1);
  113.   BDBOX(w.x, w.y, w.w + 1, w.h, w.back, 32);
  114.   OS_SETXY(w.x, w.y);
  115.   OS_SETCOLOR(w.text);
  116.   putchar(201);
  117.   for (wcount = 0; wcount < w.w; wcount++)
  118.   {
  119.     putchar(205);
  120.   }
  121.   putchar(187);
  122.   OS_SETXY(w.x, w.y + w.h);
  123.   putchar(200);
  124.   for (wcount = 0; wcount < w.w; wcount++)
  125.   {
  126.     putchar(205);
  127.   }
  128.   putchar(188);
  129.  
  130.   tempx = w.x + w.w + 1;
  131.   for (wcount = 1; wcount < w.h; wcount++)
  132.   {
  133.     OS_SETXY(w.x, w.y + wcount);
  134.     putchar(186);
  135.     OS_SETXY(tempx, w.y + wcount);
  136.     putchar(186);
  137.   }
  138.   tittleStart = w.x + (w.w / 2) - (strlen(w.tittle) / 2);
  139.   OS_SETXY(tittleStart, w.y);
  140.   printf("[%s]", w.tittle);
  141.  
  142.   OS_SETXY(w.x + 1, w.y + 1);
  143.   OS_SETCOLOR(w.back);
  144.   tittleStart = w.x + (w.w / 2) - (strlen(message) / 2);
  145.   OS_SETXY(tittleStart, w.y + 1);
  146.   printf("%s", message);
  147. }
  148.  
  149. void printHelp(void)
  150. {
  151.   OS_SETCOLOR(67);
  152.   printf("   GETPIC [%s] zxart.ee picture viewer for nedoNET\n\r", ver);
  153.   OS_SETCOLOR(6);
  154.   printf("-------------------------------------------------------\n\r");
  155.   printf(" Управление:\n\r");
  156.   printf(" 'ESC' - выход из программы;\n\r");
  157.   printf(" '<-' или 'B' к последним картинкам;\n\r");
  158.   printf(" '->' или 'Пробел' к более старым картинкам\n\r");
  159.   printf(" 'J' Прыжок на  указанную по счету картинку\n\r");
  160.   printf(" 'I' Просмотр экрана информации о картинках\n\r");
  161.   printf(" 'S' Сохранить картинку на диск в текущую папку\n\r");
  162.   printf(" 'V' не выводить информацию об авторах\n\r");
  163.   printf(" 'R' переход в режим  случайная картинка с рейтингом 4+\n\r");
  164.   printf(" 'A' переход в режим  слайд-шоу\n\r");
  165.   printf(" 'D' Переключение режима ZXNETUSB/ESP-AT\n\r");
  166.   printf(" 'H' Данная справочная информация\n\r");
  167.   printf("-----------------Нажмите любую кнопку------------------\n\r");
  168.   OS_SETCOLOR(70);
  169.   keypress = getchar();
  170.   OS_CLS(0);
  171. }
  172.  
  173. void delay(unsigned long counter)
  174. {
  175.   unsigned long start, finish;
  176.   counter = counter / 20;
  177.   if (counter < 1)
  178.   {
  179.     counter = 1;
  180.   }
  181.   start = time();
  182.   finish = start + counter;
  183.  
  184.   while (start < finish)
  185.   {
  186.     start = time();
  187.   }
  188. }
  189.  
  190. ///////////////////////////
  191. #include <../common/esp-com.c>
  192. #include <../common/network.c>
  193. //////////////////////////
  194.  
  195. int cutHeader(unsigned int todo)
  196. {
  197.   unsigned int err;
  198.   unsigned char *count1;
  199.  
  200.   err = httpError();
  201.   if (err != 200)
  202.   {
  203.     printf("\r\nHTTP response:[%u]\r\n", err);
  204.     printf("^^^^^^^^^^^^^^^^^^^^^\r\n");
  205.     puts(netbuf);
  206.     getchar();
  207.   }
  208.   count1 = strstr(netbuf, "Content-Length:");
  209.   if (count1 == NULL)
  210.   {
  211.     printf("contLen  not found \r\n");
  212.     contLen = 0;
  213.   }
  214.   else
  215.   {
  216.     contLen = atol(count1 + 15);
  217.     // printf("Content-Length: %lu \n\r", contLen);
  218.   }
  219.  
  220.   count1 = strstr(netbuf, "\r\n\r\n");
  221.   if (count1 == NULL)
  222.   {
  223.     printf("header not found\r\n");
  224.   }
  225.   else
  226.   {
  227.     headlng = ((unsigned int)count1 - (unsigned int)netbuf + 4);
  228.     // printf("header %u bytes\r\n", headlng);
  229.   }
  230.   return todo - headlng;
  231. }
  232.  
  233. char *str_replace(char *dst, int num, const char *str,
  234.                   const char *orig, const char *rep)
  235. {
  236.   const char *ptr;
  237.   size_t len1 = strlen(orig);
  238.   size_t len2 = strlen(rep);
  239.   char *tmp = dst;
  240.  
  241.   num -= 1;
  242.   while ((ptr = strstr(str, orig)) != NULL)
  243.   {
  244.     num -= (ptr - str) + len2;
  245.     if (num < 1)
  246.       break;
  247.  
  248.     strncpy(dst, str, (size_t)(ptr - str));
  249.     dst += ptr - str;
  250.     strncpy(dst, rep, len2);
  251.     dst += len2;
  252.     str = ptr + len1;
  253.   }
  254.  
  255.   for (; (*dst = *str) && (num > 0); --num)
  256.   {
  257.     ++dst;
  258.     ++str;
  259.   }
  260.   return tmp;
  261. }
  262.  
  263. char fillPictureEsp(void)
  264. {
  265.   unsigned char sizeLink = 0;
  266.   unsigned long downloaded = 0;
  267.   unsigned char byte, count = 0;
  268.   unsigned int todo = 0;
  269.   unsigned char *count1;
  270.  
  271.   strcpy(link, netbuf);
  272.   sizeLink = strlen(link);
  273.   do
  274.   {
  275.     sendcommand("AT+CIPSTART=\"TCP\",\"zxart.ee\",80");
  276.     getAnswer2(); // CONNECT or ERROR or link is not valid
  277.     count1 = strstr(netbuf, "CONNECT");
  278.   } while (count1 == NULL);
  279.  
  280.   getAnswer2();                                   // OK
  281.   sprintf(netbuf, "AT+CIPSEND=%u", sizeLink + 2); // second CRLF in send command
  282.   sendcommand(netbuf);
  283.   getAnswer2();
  284.   do
  285.   {
  286.     byte = uart_readBlock();
  287.     // putchar(byte);
  288.   } while (byte != '>');
  289.   sendcommand(link);
  290.   count = 0;
  291.   do
  292.   {
  293.     byte = uart_readBlock();
  294.     if (byte == sendOk[count])
  295.     {
  296.       count++;
  297.     }
  298.     else
  299.     {
  300.       count = 0;
  301.     }
  302.   } while (count < strlen(sendOk));
  303.   uart_readBlock(); // CR
  304.   uart_readBlock(); // LF
  305.   downloaded = 0;
  306.   do
  307.   {
  308.     headlng = 0;
  309.     todo = recvHead();
  310.     getdataEsp(todo); // Requested size
  311.     if (downloaded == 0)
  312.     {
  313.       todo = cutHeader(todo);
  314.     }
  315.  
  316.     if (downloaded + todo > sizeof(picture))
  317.     {
  318.       printf("dataBuffer overrun... %u reached \n\r", downloaded + todo);
  319.       return false;
  320.     }
  321.  
  322.     memcpy(picture + downloaded, netbuf + headlng, todo);
  323.     downloaded = downloaded + todo;
  324.   } while (downloaded < contLen);
  325.   sendcommand("AT+CIPCLOSE");
  326.   getAnswer2(); // CLOSED
  327.   getAnswer2(); // OK
  328.   return true;
  329. }
  330.  
  331. char fillPictureNet(void)
  332. {
  333.   int todo;
  334.   unsigned int downloaded = 0;
  335.   char socket, retry;
  336.   picture[0] = 0;
  337.   retry = 3;
  338.   socket = OpenSock(AF_INET, SOCK_STREAM);
  339.   testOperation("OS_NETSOCKET", socket);
  340.   todo = netConnect(socket, retry);
  341.   testOperation("OS_NETCONNECT", todo);
  342.   todo = tcpSend(socket, (unsigned int)&netbuf, strlen(netbuf), retry);
  343.   testOperation("OS_WIZNETWRITE", todo);
  344.  
  345.   do
  346.   {
  347.     headlng = 0;
  348.     todo = tcpRead(socket, retry);
  349.     testOperation("OS_WIZNETREAD", todo); // Quit if too many retries
  350.  
  351.     if (downloaded == 0)
  352.     {
  353.       todo = cutHeader(todo);
  354.     }
  355.  
  356.     if (downloaded + todo > sizeof(picture))
  357.     {
  358.       printf("dataBuffer overrun... %u reached \n\r", downloaded + todo);
  359.       return false;
  360.     }
  361.     memcpy(picture + downloaded, netbuf + headlng, todo);
  362.     downloaded = downloaded + todo;
  363.   } while (downloaded != contLen);
  364.  
  365.   netShutDown(socket, 0);
  366.   picture[downloaded + 1] = 0;
  367.   return true;
  368. }
  369.  
  370. void nameRepair(unsigned char *pfn, unsigned int tfnSize)
  371. {
  372.  
  373.   str_replace(pfn, tfnSize, pfn, "\\", "_");
  374.   str_replace(pfn, tfnSize, pfn, "/", "_");
  375.   str_replace(pfn, tfnSize, pfn, ":", "_");
  376.   str_replace(pfn, tfnSize, pfn, "*", "_");
  377.   str_replace(pfn, tfnSize, pfn, "?", "_");
  378.   str_replace(pfn, tfnSize, pfn, "<", "_");
  379.   str_replace(pfn, tfnSize, pfn, ">", "_");
  380.   str_replace(pfn, tfnSize, pfn, "|", "_");
  381.   str_replace(pfn, tfnSize, pfn, " ", "_");
  382.   str_replace(pfn, tfnSize, pfn, "&#039;", "'");
  383.   str_replace(pfn, tfnSize, pfn, "&amp;", "&");
  384.   str_replace(pfn, tfnSize, pfn, "&quot;", "'");
  385.   str_replace(pfn, tfnSize, pfn, "&gt;", ")");
  386.   str_replace(pfn, tfnSize, pfn, "&lt;", "(");
  387.   str_replace(pfn, tfnSize, pfn, "\"", "'");
  388. }
  389.  
  390. void stringRepair(unsigned char *pfn, unsigned int tSize)
  391. {
  392.   str_replace(pfn, tSize, pfn, "&#039;", "'");
  393.   str_replace(pfn, tSize, pfn, "&amp;", "&");
  394.   str_replace(pfn, tSize, pfn, "&gt;", ">");
  395.   str_replace(pfn, tSize, pfn, "&lt;", "<");
  396.   str_replace(pfn, tSize, pfn, "&quot;", "\"");
  397.   str_replace(pfn, tSize, pfn, "\\/", "/");
  398. }
  399.  
  400. void ncReplace(void)
  401. {
  402.   unsigned char len;
  403.   for (len = 0; len < strlen(curFileStruct.afn); len++)
  404.   {
  405.     if ((curFileStruct.afn[len] < ' ') || (curFileStruct.afn[len] > 0xef) || (curFileStruct.afn[len] > 0x7e && curFileStruct.afn[len] < 0xb0))
  406.     {
  407.       curFileStruct.afn[len] = '_';
  408.     }
  409.   }
  410.  
  411.   for (len = 0; len < strlen(curFileStruct.pfn); len++)
  412.   {
  413.     if ((curFileStruct.pfn[len] < ' ') || (curFileStruct.pfn[len] > 0xef) || (curFileStruct.pfn[len] > 0x7e && curFileStruct.pfn[len] < 0xb0))
  414.     {
  415.       curFileStruct.pfn[len] = '_';
  416.     }
  417.   }
  418. }
  419.  
  420. unsigned char savePic(unsigned long fileId)
  421. {
  422.   FILE *fp2;
  423.   unsigned char afnSize, tfnSize;
  424.  
  425.   afnSize = sizeof(curFileStruct.afn) - 1;
  426.   tfnSize = sizeof(curFileStruct.pfn) - 1;
  427.  
  428.   strcpy(curFileStruct.afn, curFileStruct.authorTitle);
  429.   nameRepair(curFileStruct.afn, afnSize);
  430.  
  431.   strcpy(curFileStruct.pfn, curFileStruct.picName);
  432.   nameRepair(curFileStruct.pfn, tfnSize);
  433.   ncReplace();
  434.  
  435.   sprintf(curFileStruct.fileName, "%s-%s-%ld.scr", curFileStruct.afn, curFileStruct.pfn, fileId);
  436.   if (strlen(curFileStruct.fileName) > 62)
  437.   {
  438.     sprintf(fileIdChar, "-%ld", fileId);
  439.     str_replace(curFileStruct.fileName, sizeof(curFileStruct.fileName) - 1, curFileStruct.fileName, fileIdChar, "");
  440.     curFileStruct.fileName[50] = '\0';
  441.     strcat(curFileStruct.fileName, fileIdChar);
  442.     strcat(curFileStruct.fileName, ".scr");
  443.   }
  444.   OS_SETSYSDRV();
  445.   OS_MKDIR("../downloads");        // Create if not exist
  446.   OS_MKDIR("../downloads/getpic"); // Create if not exist
  447.   OS_CHDIR("../downloads/getpic");
  448.   fp2 = OS_CREATEHANDLE(curFileStruct.fileName, 0x80);
  449.   if (((int)fp2) & 0xff)
  450.   {
  451.     printf("%s creating error\r\n", curFileStruct.fileName);
  452.     getchar();
  453.     exit(0);
  454.   }
  455.   OS_WRITEHANDLE(picture, fp2, 6912);
  456.   OS_CLOSEHANDLE(fp2);
  457.   return 0;
  458. }
  459.  
  460. int pos(unsigned char *s, unsigned char *c, unsigned int n, unsigned int startPos)
  461. {
  462.   unsigned int i, j;
  463.   unsigned int lenC, lenS;
  464.  
  465.   for (lenC = 0; c[lenC]; lenC++)
  466.     ;
  467.   for (lenS = 0; s[lenS]; lenS++)
  468.     ;
  469.  
  470.   for (i = startPos; i <= lenS - lenC; i++)
  471.   {
  472.     for (j = 0; s[i + j] == c[j]; j++)
  473.       ;
  474.  
  475.     if (j - lenC == 1 && i == lenS - lenC && !(n - 1))
  476.       return i;
  477.     if (j == lenC)
  478.       if (n - 1)
  479.         n--;
  480.       else
  481.         return i;
  482.   }
  483.   return -1;
  484. }
  485.  
  486. const char *parseJson(unsigned char *property)
  487. {
  488.   unsigned int w, lng, lngp1, findEnd, listPos;
  489.   unsigned char terminator;
  490.   int n;
  491.   n = -1;
  492.   // netbuf[0] = '\0';
  493.   n = pos(picture, property, 1, 0);
  494.   if (n == -1)
  495.   {
  496.     strcpy(netbuf, "-");
  497.     // printf("Property %s not found", property);
  498.     return netbuf;
  499.   }
  500.   lng = n - 1 + strlen(property);
  501.   if (picture[lng] == ':')
  502.   {
  503.     terminator = '\0';
  504.   }
  505.   if (picture[lng] == '\"')
  506.   {
  507.     terminator = '\"';
  508.   }
  509.   if (picture[lng] == '[')
  510.   {
  511.     terminator = ']';
  512.   }
  513.  
  514.   findEnd = 1;
  515.   lngp1 = lng + 1;
  516.  
  517.   while (42)
  518.   {
  519.  
  520.     if ((picture[lngp1 + findEnd] == ','))
  521.     {
  522.       if (terminator == '\0')
  523.       {
  524.         break;
  525.       }
  526.       if ((picture[lng + findEnd] == terminator))
  527.       {
  528.         findEnd--;
  529.         break;
  530.       }
  531.     }
  532.     findEnd++;
  533.   }
  534.   listPos = 0;
  535.   for (w = lngp1; w < findEnd + lngp1; w++)
  536.   {
  537.     netbuf[listPos] = picture[w];
  538.     listPos++;
  539.   }
  540.   netbuf[listPos] = '\0';
  541.   return netbuf;
  542. }
  543.  
  544. void convert866(void)
  545. {
  546.   unsigned int lng, targetPos, w, q = 0;
  547.   unsigned char buffer[8], one, two;
  548.   unsigned int decVal;
  549.   lng = strlen(netbuf);
  550.   targetPos = lng + 1;
  551.  
  552.   while (q < lng)
  553.   {
  554.     one = netbuf[q];
  555.     two = netbuf[q + 1];
  556.     if (one == 92 && two == 117)
  557.     {
  558.       q = q + 2;
  559.       for (w = 0; w < 4; w++)
  560.       {
  561.         buffer[w] = netbuf[q + w];
  562.       }
  563.       q = q + 4;
  564.       buffer[4] = '\0';
  565.       decVal = (unsigned int)strtol(buffer, NULL, 16);
  566.  
  567.       if (decVal < 1088)
  568.       {
  569.         decVal = decVal - 912;
  570.       }
  571.       if (decVal > 1087)
  572.       {
  573.         decVal = decVal - 864;
  574.       }
  575.       if (decVal == 1025)
  576.       {
  577.         decVal = 240;
  578.       }
  579.       if (decVal == 1105)
  580.       {
  581.         decVal = 241;
  582.       }
  583.  
  584.       netbuf[targetPos] = decVal;
  585.     }
  586.     else
  587.     {
  588.       netbuf[targetPos] = netbuf[q];
  589.       q++;
  590.     }
  591.     targetPos++;
  592.   }
  593.   netbuf[targetPos] = 0;
  594.  
  595.   for (w = lng + 1; w < targetPos + 1; w++)
  596.   {
  597.     netbuf[w - lng - 1] = netbuf[w];
  598.   }
  599. }
  600.  
  601. long processJson(unsigned long startPos, unsigned char limit, unsigned char queryNum)
  602. {
  603.   unsigned int tSize;
  604.   unsigned char *count1, result;
  605.   switch (queryNum)
  606.   {
  607.   case 0:
  608.     sprintf(netbuf, "GET /api/export:zxPicture/filter:zxPictureType=standard/limit:%u/start:%lu/order:date,desc%s", limit, startPos, userAgent);
  609.     break;
  610.   case 1:
  611.     sprintf(netbuf, "GET /api/types:zxPicture/export:zxPicture/language:eng/start:0/limit:1/order:rand/filter:zxPictureMinRating=%s;zxPictureType=standard%s", minRating, userAgent);
  612.     break;
  613.   case 3: // /api/export:author/filter:authorId=2202
  614.     sprintf(netbuf, "GET /api/export:author/filter:authorId=%lu%s", startPos, userAgent);
  615.     break;
  616.   case 99: // GET /jsonElementData/elementId:182798
  617.     sprintf(netbuf, "GET /jsonElementData/elementId:%lu%s", startPos, userAgent);
  618.     break;
  619.   }
  620.  
  621.   switch (netDriver)
  622.   {
  623.   case 0:
  624.     result = fillPictureNet();
  625.     break;
  626.   case 1:
  627.     result = fillPictureEsp();
  628.     break;
  629.   }
  630.   count1 = strstr(picture, "responseStatus\":\"success");
  631.   if (count1 == NULL)
  632.   {
  633.     OS_CLS(0);
  634.     OS_SETCOLOR(66);
  635.     puts("Picture[]:");
  636.     puts(picture);
  637.     puts("---------------");
  638.     printf("PROCESS JSON: [ERROR: Bad responseStatus.] [Query:%u][Pic:%lu]\r\n", queryNum, startPos);
  639.     YIELD();
  640.     getchar();
  641.     return -1;
  642.   }
  643.  
  644.   count1 = strstr(picture, "\"id\":");
  645.   if (count1 == NULL)
  646.   {
  647.     OS_CLS(0);
  648.     OS_SETCOLOR(66);
  649.     puts("Picture[]:");
  650.     puts(picture);
  651.     puts("---------------");
  652.     printf("PROCESS JSON: [ERROR: ID not found.] [Query:%u][Pic:%lu]\r\n", queryNum, startPos);
  653.     YIELD();
  654.     return -2;
  655.   }
  656.  
  657.   netbuf[0] = 0;
  658.   if (queryNum < 3)
  659.   {
  660.     parseJson("\"id\":");
  661.     curFileStruct.picId = atol(netbuf);
  662.     parseJson(",\"title\":\"");
  663.     convert866();
  664.     strcpy(curFileStruct.picName, netbuf);
  665.  
  666.     tSize = sizeof(curFileStruct.picName);
  667.     stringRepair(curFileStruct.picName, tSize);
  668.  
  669.     parseJson(",\"type\":\"");
  670.     strcpy(curFileStruct.picType, netbuf);
  671.     parseJson("\"rating\":\"");
  672.     strcpy(curFileStruct.picRating, netbuf);
  673.     parseJson("\"year\":\"");
  674.     curFileStruct.picYear = atoi(netbuf);
  675.     parseJson("\"totalAmount\":");
  676.     curFileStruct.totalAmount = atol(netbuf);
  677.     parseJson("\"authorIds\":[");
  678.     strcpy(curFileStruct.authorIds, netbuf);
  679.   }
  680.   if (queryNum == 99)
  681.   {
  682.     parseJson(",\"title\":\"");
  683.     convert866();
  684.     strcpy(curFileStruct.authorTitle, netbuf);
  685.     parseJson(",\"realName\":\"");
  686.     convert866();
  687.     strcpy(curFileStruct.authorRealName, netbuf);
  688.   }
  689.   return curFileStruct.picId;
  690. }
  691.  
  692. void printData(void)
  693. {
  694.   OS_SETCOLOR(70);
  695.   printf(" #: ");
  696.   OS_SETCOLOR(71);
  697.   printf("%lu", count);
  698.   OS_SETCOLOR(70);
  699.   printf(" ID: ");
  700.   OS_SETCOLOR(71);
  701.   printf("%lu ", curFileStruct.picId);
  702.   OS_SETCOLOR(70);
  703.   printf(" Total Pics: ");
  704.   OS_SETCOLOR(71);
  705.   printf("%lu\r\n", curFileStruct.totalAmount);
  706.   OS_SETCOLOR(70);
  707.   printf(" Author: ");
  708.   OS_SETCOLOR(69);
  709.   printf("%s\r\n", curFileStruct.authorTitle);
  710.   OS_SETCOLOR(70);
  711.   printf(" TITLE: ");
  712.   OS_SETCOLOR(67);
  713.   printf("%s\r\n", curFileStruct.picName);
  714.   OS_SETCOLOR(70);
  715.   printf(" RATING: ");
  716.   OS_SETCOLOR(71);
  717.   printf("%s", curFileStruct.picRating);
  718.   OS_SETCOLOR(70);
  719.   printf(" YEAR: ");
  720.   OS_SETCOLOR(71);
  721.   printf("%u\r\n", curFileStruct.picYear);
  722.   OS_SETCOLOR(70);
  723.   printf(" AuthorsIDs ");
  724.   OS_SETCOLOR(71);
  725.   printf("%s", curFileStruct.authorIds);
  726.   OS_SETCOLOR(70);
  727.   printf(" Real name: ");
  728.   OS_SETCOLOR(71);
  729.   printf("%s\r\n", curFileStruct.authorRealName);
  730.   OS_SETCOLOR(69);
  731.   printf("\r\n");
  732.   // YIELD();
  733. }
  734.  
  735. unsigned char inputBox(struct window w, unsigned char *prefilled)
  736. {
  737.   unsigned char wcount, tempx, tittleStart;
  738.   unsigned char byte, counter;
  739.   w.h++;
  740.   OS_SETXY(w.x, w.y - 1);
  741.   BDBOX(w.x, w.y, w.w + 1, w.h, w.back, 32);
  742.   OS_SETXY(w.x, w.y);
  743.   OS_SETCOLOR(w.text);
  744.   putchar(201);
  745.   for (wcount = 0; wcount < w.w; wcount++)
  746.   {
  747.     putchar(205);
  748.   }
  749.   putchar(187);
  750.   OS_SETXY(w.x, w.y + w.h);
  751.   putchar(200);
  752.   for (wcount = 0; wcount < w.w; wcount++)
  753.   {
  754.     putchar(205);
  755.   }
  756.   putchar(188);
  757.  
  758.   tempx = w.x + w.w + 1;
  759.   for (wcount = 1; wcount < w.h; wcount++)
  760.   {
  761.     OS_SETXY(w.x, w.y + wcount);
  762.     putchar(186);
  763.     OS_SETXY(tempx, w.y + wcount);
  764.     putchar(186);
  765.   }
  766.   tittleStart = w.x + (w.w / 2) - (strlen(w.tittle) / 2);
  767.   OS_SETXY(tittleStart, w.y);
  768.   printf("[%s]", w.tittle);
  769.   OS_SETXY(w.x + 1, w.y + 1);
  770.   OS_SETCOLOR(w.back);
  771.   putchar(219);
  772.  
  773.   cmd[0] = 0;
  774.  
  775.   counter = strlen(prefilled);
  776.   if (counter != 0)
  777.   {
  778.     strcpy(cmd, prefilled);
  779.     goto skipKeys;
  780.   }
  781.  
  782.   do
  783.   {
  784.     byte = OS_GETKEY();
  785.     if (byte != 0)
  786.     {
  787.       switch (byte)
  788.       {
  789.       case 0x08:
  790.         if (counter > 0)
  791.         {
  792.           counter--;
  793.           cmd[counter] = 0;
  794.         }
  795.         break;
  796.       case 0x0d:
  797.  
  798.         if (counter == 0)
  799.         {
  800.           return false;
  801.         }
  802.         else
  803.         {
  804.           return true;
  805.         }
  806.  
  807.       case 31:
  808.         break;
  809.       case 250:
  810.         break;
  811.       case 249:
  812.         break;
  813.       case 248:
  814.         break;
  815.       case 251: // Right
  816.         break;
  817.       case 252: // Del
  818.         OS_SETXY(w.x + 1, w.y + 1);
  819.         spaces(counter + 1);
  820.         cmd[0] = 0;
  821.         counter = 0;
  822.         break;
  823.       case 27:
  824.         cmd[0] = 0;
  825.         return false;
  826.       default:
  827.         if (counter < w.w - 1)
  828.         {
  829.           cmd[counter] = byte;
  830.           counter++;
  831.           cmd[counter] = 0;
  832.         }
  833.         break;
  834.       }
  835.     skipKeys:
  836.       OS_SETXY(w.x + 1, w.y + 1);
  837.       printf("%s", cmd);
  838.       putchar(219);
  839.       if (byte == 0x08)
  840.       {
  841.         putchar(' ');
  842.       }
  843.     }
  844.     YIELD();
  845.   } while (42);
  846.   return false;
  847. }
  848.  
  849. void safeKeys(unsigned char keypress)
  850. {
  851.   switch (keypress)
  852.   {
  853.   case 27:
  854.     OS_SETCOLOR(70);
  855.     printf("Good bye...\r\n");
  856.     delayLong(500);
  857.     exit(0);
  858.     break;
  859.   case 'j':
  860.   case 'J':
  861.     curWin.w = 13;
  862.     curWin.x = 80 / 2 - curWin.w / 2 - 2;
  863.     curWin.y = 11;
  864.     curWin.h = 1;
  865.     curWin.text = 103;
  866.     curWin.back = 103;
  867.     strcpy(curWin.tittle, "№ картинки:");
  868.     if (inputBox(curWin, ""))
  869.     {
  870.       sscanf(cmd, "%lu", &count);
  871.       if (count > curFileStruct.totalAmount - 1)
  872.       {
  873.         count = curFileStruct.totalAmount - 1;
  874.       }
  875.     }
  876.     break;
  877.   case 'v':
  878.   case 'V':
  879.     verbose = !verbose;
  880.  
  881.     if (verbose == 0)
  882.     {
  883.       BOX(1, 1, 80, 25, 40, ' ');
  884.       AT(1, 1);
  885.     }
  886.     break;
  887.   case 'h':
  888.   case 'H':
  889.     printHelp();
  890.     break;
  891.   case 'r':
  892.   case 'R':
  893.     randomPic = !randomPic;
  894.     OS_SETCOLOR(70);
  895.     if (verbose == 1)
  896.     {
  897.       if (randomPic == 1)
  898.       {
  899.         printf("    Random mode enabled...\r\n");
  900.         count = 0;
  901.         delayLong(500);
  902.       }
  903.       else
  904.       {
  905.         printf("    Sequental mode enabled...\r\n");
  906.         count = 0;
  907.         delayLong(500);
  908.       }
  909.     }
  910.     break;
  911.   case 'a':
  912.   case 'A':
  913.     slideShow = !slideShow;
  914.     OS_SETCOLOR(70);
  915.     if (slideShow == 1)
  916.     {
  917.       if (verbose == 1)
  918.         printf("    SlideShow mode enabled...\r\n\r\n");
  919.       slideShowTime = 50;
  920.       delayLong(500);
  921.     }
  922.     else
  923.     {
  924.       if (verbose == 1)
  925.         printf("    Manual mode enabled...\r\n\r\n");
  926.       slideShowTime = 0;
  927.       delayLong(500);
  928.     }
  929.     break;
  930.   case 'd':
  931.   case 'D':
  932.     netDriver = !netDriver;
  933.     OS_SETCOLOR(70);
  934.     if (netDriver == 1)
  935.     {
  936.       printf("    ESP-COM mode enabled...\r\n");
  937.       loadEspConfig();
  938.       uart_init(divider);
  939.       espReBoot();
  940.       printf("    ESP-COM inited...\r\n");
  941.       delayLong(500);
  942.     }
  943.     else
  944.     {
  945.       if (verbose == 1)
  946.         printf("    NedoNET mode enabled...");
  947.       delayLong(500);
  948.     }
  949.     break;
  950.   case 'm':
  951.   case 'M':
  952.     curWin.w = 22;
  953.     curWin.x = 80 / 2 - curWin.w / 2 - 2;
  954.     curWin.y = 1;
  955.     curWin.h = 1;
  956.     curWin.text = 103;
  957.     curWin.back = 103;
  958.     strcpy(curWin.tittle, "Минимальная оценка:");
  959.  
  960.     if (inputBox(curWin, ""))
  961.     {
  962.       char counter;
  963.       for (counter = 0; counter < strlen(cmd); counter++)
  964.       {
  965.         if ((((cmd[counter] < '0') || (cmd[counter] > '9'))) && cmd[counter] != '.')
  966.         {
  967.           counter = 0;
  968.           break;
  969.         }
  970.       }
  971.       if (counter != 0)
  972.       {
  973.         strncpy(minRating, cmd, 5);
  974.         count = 0;
  975.       }
  976.     }
  977.   default:
  978.     break;
  979.   }
  980. }
  981.  
  982. void init(void)
  983. {
  984.   OS_SETSYSDRV();
  985.   OS_MKDIR("../downloads");        // Create if not exist
  986.   OS_MKDIR("../downloads/getpic"); // Create if not exist
  987.   OS_CHDIR("../downloads/getpic");
  988.  
  989.   count = 0;
  990.   verbose = 1;
  991.   randomPic = 0;
  992.   slideShow = 0;
  993.   netDriver = 0;
  994.   strcpy(minRating, "4.0");
  995.  
  996.   targetadr.family = AF_INET;
  997.   targetadr.porth = 00;
  998.   targetadr.portl = 80;
  999.   targetadr.b1 = 217; // D9
  1000.   targetadr.b2 = 146; // 92
  1001.   targetadr.b3 = 69;  // 45
  1002.   targetadr.b4 = 13;  // 0D
  1003. }
  1004.  
  1005. C_task main(void)
  1006. {
  1007.   long iddqd, idkfa;
  1008.   char result;
  1009.  
  1010.   OS_HIDEFROMPARENT();
  1011.   OS_SETGFX(0x86);
  1012.   OS_CLS(0);
  1013.  
  1014.   init();
  1015.  
  1016.   printHelp();
  1017.   safeKeys(keypress);
  1018.  
  1019.   if (netDriver == 0)
  1020.   {
  1021.  
  1022.     get_dns();
  1023.     dnsResolve("zxart.ee");
  1024.     printf("\r\n");
  1025.   }
  1026.  
  1027. start:
  1028.   keypress = 0;
  1029.   switch (randomPic)
  1030.   {
  1031.   case 0:
  1032.  
  1033.     iddqd = processJson(count, 1, 0);
  1034.     break;
  1035.   case 1:
  1036.     iddqd = processJson(0, 1, 1);
  1037.     break;
  1038.   }
  1039.  
  1040.   if (iddqd < 0)
  1041.   {
  1042.     count++;
  1043.     keypress = getchar();
  1044.     safeKeys(keypress);
  1045.     goto start;
  1046.   }
  1047.  
  1048.   if (verbose == 1)
  1049.   {
  1050.     idkfa = processJson(atol(curFileStruct.authorIds), 0, 99);
  1051.     if (idkfa < 0)
  1052.     {
  1053.       printf(" Cant parse curFileStruct.authorIds = %s \r\n\r\n", curFileStruct.authorIds);
  1054.       count++;
  1055.       getchar();
  1056.       goto start;
  1057.     }
  1058.   }
  1059.  
  1060.   if (strcmp(curFileStruct.picType, "standard") != 0)
  1061.   {
  1062.     printf("  >>Format '%s' not supported, skipped \n\r", curFileStruct.picType);
  1063.     count++;
  1064.     goto start;
  1065.   }
  1066.  
  1067.   sprintf(netbuf, "GET /file/id:%lu%s", iddqd, userAgent);
  1068.  
  1069.   switch (netDriver)
  1070.   {
  1071.   case 0:
  1072.     result = fillPictureNet();
  1073.     break;
  1074.   case 1:
  1075.     result = fillPictureEsp();
  1076.     break;
  1077.   }
  1078.  
  1079. review:
  1080.   OS_CLS(0);
  1081.   YIELD();
  1082.   keypress = viewScreen6912((unsigned int)&picture, slideShowTime);
  1083.   emptyKeys();
  1084.  
  1085.   ///// Keys only for pictures
  1086.   if (keypress == 's' || keypress == 'S')
  1087.   {
  1088.     savePic(iddqd);
  1089.     printf("%s saved.", curFileStruct.fileName);
  1090.     delayLong(500);
  1091.     count++;
  1092.   }
  1093.  
  1094.   if (keypress == 248 || keypress == 'b' || keypress == 'B')
  1095.   {
  1096.     if (count > 0)
  1097.     {
  1098.       count--;
  1099.     }
  1100.   }
  1101.   if (keypress == 251 || keypress == 32)
  1102.   {
  1103.     count++;
  1104.     goto start;
  1105.   }
  1106.   if (keypress == 'i' || keypress == 'I')
  1107.   {
  1108.     printData();
  1109.     getchar();
  1110.     goto review;
  1111.   }
  1112.   safeKeys(keypress);
  1113.   goto start;
  1114. }
  1115.